Disable Ping in Linux

echo 1 >/proc/sys/net/ipv4/icmp_echo_ignore_all

May 8, 2021 · 1 min · Amer Khalid

LeetCode 1664: Ways to Make a Fair Array

I tried to do this all by myself but got stuck, ended up transpiling this solution in PHP. class Solution { /** * @param Integer[] $nums * @return Integer */ function waysToMakeFair($nums) { $len = count($nums); $ans = 0; $leftOdd = 0; $rightOdd = 0; $leftEven = 0; $rightEven = 0; for ($i=0; $i<$len; $i++) { if ($i%2 === 0) $rightEven += $nums[$i]; else $rightOdd += $nums[$i]; } for ($i=0; $i<$len; $i++) { if ($i%2 === 0) $rightEven -= $nums[$i]; else $rightOdd -= $nums[$i]; if ($leftEven + $rightOdd === $rightEven + $leftOdd) $ans++; if ($i%2 === 0) $leftEven += $nums[$i]; else $leftOdd += $nums[$i]; } return $ans; } }

May 8, 2021 · 1 min · Amer Khalid

LeetCode 20: Valid Parentheses

Here is my solution to this problem in JavaScript: /** * @param {string} s * @return {boolean} */ var isValid = function(s) { const stack = []; const brackets = { '(': ')', '{': '}', '[': ']' }; for (i=0; i<s.length; i++) { if (s.length === 0) return true; const c = s[i]; if (brackets[c]) { stack.push(c); } else { const lastBracket = stack.pop(); if (brackets[lastBracket] !== c) return false; } } if (stack.length > 0) return false; return true; };

May 7, 2021 · 1 min · Amer Khalid

LeetCode 844. Backspace String Compare

Here is my solution Backspace String Compare problem. /** * @param {string} s * @param {string} t * @return {boolean} */ var backspaceCompare = function(s, t) { s = processString(s); t = processString(t); //console.log({s, t}) return s===t; }; let processString = function(s) { const a = []; let skip = 0; for (let i = s.length-1; i >= 0; i--) { if (s[i] === '#') { skip++; } else if (skip > 0) { skip--; } else { a.push(s[i]); } //console.log({a, skip, i}) } let re = ''; //console.log({ a: a.length}) while (a.length > 0) { re += a.pop(); //console.log({i, re, a: a.length}) } return re; } Results: Runtime: 80 ms, faster than 73.76% of JavaScript online submissions for Backspace String Compare. ...

May 6, 2021 · 1 min · Amer Khalid

Leetcode 690: Employee Importance Solution

Here is another LeetCode solution for Employee Importance Problem. /** * Definition for Employee. * class Employee { * public $id = null; * public $importance = null; * public $subordinates = array(); * function __construct($id, $importance, $subordinates) { * $this->id = $id; * $this->importance = $importance; * $this->subordinates = $subordinates; * } * } */ class Solution { /** * @param Employee[] $employees * @param Integer $id * @return Integer */ function getImportance($employees, $id) { if (empty($employees)) return 0; $emap = []; foreach ($employees as $e) { $emap[$e->id] = $e; } return $this->recurse($emap, $id); } function recurse($emap, $id) { $e = $emap[$id]; $ans = $e->importance; foreach ($e->subordinates as $sub) { $ans += $this->recurse($emap, $sub); } return $ans; } }

May 6, 2021 · 1 min · Amer Khalid

LeetCode 1249. Minimum Remove to Make Valid Parentheses

Here is my solution to this problem: /** * @param {string} s * @return {string} */ var minRemoveToMakeValid = function(s) { const stack = []; const charArray = s.split(""); const rightBracketsToRemove = []; for (let i=0;i<charArray.length;i++) { if (charArray[i] === '(') { stack.push(i); } else if (charArray[i] === ')' && stack.length > 0) { stack.pop(); } else if (charArray[i] === ')') { charArray[i] = ''; } } while (stack.length > 0) { const i = stack.pop(); charArray[i] = ''; } return charArray.join(''); };

May 6, 2021 · 1 min · Amer Khalid

LeetCode 13: Roman to Integer

Here is my solution to this problem in PHP: class Solution { private $v = ['M' = > 1000, 'D' = > 500, 'C' = > 100, 'L' = > 50, 'X' = > 10, 'V' = > 5, 'I' = > 1]; /** * @param String $s * @return Integer */ function romanToInt($s) { $n = 0; $last = ''; for ($i=0;$i < strlen ($s ); $i++) { $n += $this- >v[$s[$i]]; if (($s[$i] == 'V' || $s[$i] == 'X') && $last == 'I') { $n = $n - 2; } elseif (($s[$i] == 'L' || $s[$i] == 'C') && $last == 'X') { $n -= 20; } elseif (($s[$i] == 'D' || $s[$i] == 'M') && $last == 'C') { $n -= 200; } $last = $s[$i]; } return $n; } }

May 5, 2021 · 1 min · Amer Khalid

Troubleshooting Kubernetes Ingress

Setting up Ingress is an easy process but when it doesn’t work it gets really painful. First, make sure you have Ingress Controller setup correctly. This is in addition to Ingress resource and should be automatically setup by your cloud provider. When I was trying to setup a Kubernetes cluster on IBM Cloud, I ran into a lot of issues. It seems due to my permissions level something went wrong during provisioning of Kubernetes and Ingress Controller was not setup correctly. I went through a lot of steps including recreating alb , ingress resources, etc. The final fix was to login to IBM Cloud as superuser to change Access Policies for the cluster. Once did that, everything just worked magically. ...

May 4, 2021 · 1 min · Amer Khalid

HackerRank: New Year Chaos

Here is my solution to New Years Chaos problem from HackerRank. The first solution had a bug: function minimumBribes (q ) { let bribes = 0 ; for (let i = 0 ;i <q .length - 2 ;i++ ) { if (q [i ] - (i + 1 ) > 2 ) { console .log ( 'Too chaotic' ); return ; } // compare index to element, works great except when a smaller element is pushed way back if (q [i ] - (i + 1 ) > 0 ) { bribes += q [i ] - (i + 1 ); } } console .log (bribes ); } Second solution had O(n^2) complexity: ...

May 4, 2021 · 2 min · Amer Khalid

LeetCode 200: Number of Islands

I had to watch some YouTube videos for the solution to this problem. I am not sure if this is the best solution though, one day I will revisit it. class Solution { /** * @param String[][] $grid * @return Integer */ function numIslands($grid) { $count = 0; for ($i=0; $i < count ($grid ); $i++) { for ($j = 0; $j <count ($grid [$i ]); $j++) { if ($grid [$i ][$j ] == "1 ") { $count++; // zero out rest of 1s $this- >zeroOut($grid, $i, $j); } } } return $count; } function zeroOut( & $grid, $i, $j) { if ($i < 0 || $i >=count($grid) || $j < 0 || $j >=count($grid[$i]) || $grid[$i][$j] == "0") return; $grid[$i][$j] = "0"; $this- >zeroOut($grid, $i-1, $j); $this- >zeroOut($grid, $i+1, $j); $this- >zeroOut($grid, $i, $j-1); $this- >zeroOut($grid, $i, $j+1); } }

May 3, 2021 · 1 min · Amer Khalid