LeetCode 42. Trapping Rain Water

My solution to LeetCode 42. Trapping Rain Water in JavaScript. /** * @param {number[]} height * @return {number} */ var trap = function (height ) { let maxLeft = 0 ,maxRight = 0 ; let left = 0 ; let right = height .length - 1 ; let total = 0 ; while (left <right ) { if (height [left ] <height [right ]) { if (height [left ] >maxLeft ) { maxLeft = height [left ]; } else { total += maxLeft -height [left ] } left++ ; } else { if (height [right ] >maxRight ) { maxRight = height [right ]; } else { total += maxRight -height [right ] } right -- ; } } return total ; };

May 2, 2021 · 1 min · Amer Khalid

LeetCode 1089: Duplicate Zeros

LeetCode 1089: Duplicate Zeros solution in PHP. class Solution { /** * @param Integer[] $arr * @return NULL */ function duplicateZeros( & $arr) { $len = count($arr); for ($i=0; $i < $len; $i++) { if (0 === $arr[$i]) { array_splice($arr, $i++, 0, 0); } } array_splice($arr, $len); } }

May 2, 2021 · 1 min · Amer Khalid

LeetCode 11. Container With Most Water

Here is my solution to Container with Most Water in JavaScript. /** * @param {number[]} height * @return {number} */ var maxArea = function (height ) { let left = 0 ; let right = height .length - 1 ; let maxWater = 0 ; while (left <right ) { const water = (right -left ) * Math.min (height [left ],height [right ]); if (water > maxWater ) { maxWater = water ; } if (height [left ] <height [right ]) { left++ ; } else { right -- ; } } return maxWater ; };

April 30, 2021 · 1 min · Amer Khalid

LeetCode 1: Two Sum

/** * @param {number[]} nums * @param {number} target * @return {number[]} */ var twoSum = function (nums ,target ) { const numsToFind = {}; for (let i = 0; i<nums.length; i++ ) { const numToFind = nums [i ]; if (numsToFind [numToFind ] >= 0 ) { return [i ,numsToFind [numToFind ]]; } numsToFind [target - numToFind ] = i ; } }; My solution for Two Sum problem in JavaScript.

April 29, 2021 · 1 min · Amer Khalid

LeetCode 88: Merge Sorted Array solution

Here is my solution for Merge Sorted Array problem in PHP. This is not the most efficient solution. class Solution { /** * @param Integer[] $nums1 * @param Integer $m * @param Integer[] $nums2 * @param Integer $n * @return NULL */ function merge(&$nums1, $m, $nums2, $n) { if ($n == 0) { return; } if ($m == 0) { for ($i=0; $i < $n; $i++) { $nums1[$i] = $nums2[$i]; } } $i = 0; $j = 0; while ($i < $m && $j < $n) { if ($nums1[$i] < =$nums2[$j]) { $i++; } else { $this->array_insert($nums1, $i++, $nums2[$j]); $m++; $j++; } } while ($j < $n) { $nums1[$i++] = $nums2[$j++]; } } function array_insert( & $arr, $i, $num) { $endArr = array_slice($arr, $i); $arr[$i] = $num; for ($x=$i+1; $x < count ($arr); $x++) { $arr [$x ] = array_shift ($endArr); } } }

April 25, 2021 · 1 min · Amer Khalid

LeetCode 26 – Remove Duplicates from Sorted Array

Here is my solution to Remove Duplicates from Sorted Array problem in JavaScript /** * @param {number[]} nums * @return {number} */ var removeDuplicates = function(nums) { let i=0; while (i<nums.length-1) { if (nums[i] === nums[i+1]) { nums.splice(i+1, 1); } else { i++; } } return nums.length; }; Results Runtime: 108 ms, faster than 33.69% of JavaScript online submissions for Remove Duplicates from Sorted Array.Memory Usage: 40.6 MB, less than 89.66% of JavaScript online submissions for Remove Duplicates from Sorted Array. ...

April 21, 2021 · 1 min · Amer Khalid

LeetCode #73: Set Matrix Zeroes

Here is my solution to Set Matrix Zeros problem in PHP: class Solution { /** * @param Integer[][] $matrix * @return NULL */ function setZeroes(&$matrix) { $rowsToZeroOut = []; $colsToZeroOut = []; for ($i=0; $i<count($matrix); $i++) { for ($j=0; $j<count($matrix[$i]); $j++) { if ($matrix[$i][$j] === 0) { $rowsToZeroOut[] = $i; $colsToZeroOut[] = $j; } } } foreach($rowsToZeroOut as $r) { for($i=0; $i<count($matrix[$r]); $i++) { $matrix[$r][$i] = 0; } } foreach($colsToZeroOut as $c) { for($i=0; $i<count($matrix); $i++) { $matrix[$i][$c] = 0; } } } } Results Runtime: 52 ms, faster than 13.33% of PHP online submissions for Set Matrix Zeroes. Memory Usage: 19.8 MB, less than 80.00% of PHP online submissions for Set Matrix Zeroes. ...

April 20, 2021 · 1 min · Amer Khalid

WordPress to Hugo to WordPress to Hugo

I moved all my blogs back to Hugo. I have gone back and forth between Wordpress and Hugo several times. WordPress makes it really easy to write and publish content. On this domain, I was using WordPress as a bookmarking service. With PressThis plugin, I was able to use a bookmarklet and publish a new post from any page on the web. But that plugin has not been updated in a while and it seems with every new WordPress release something would break. A few times, I was able to hack it to keep going. But lately, I cannot keep up with it. I will have to figure out a better way to bookmark but for now, I didn’t want to deal with WordPress. And easy bookmarking was the only reason why I was using WordPress. ...

December 23, 2020 · 2 min · Amer Khalid

iPad Pro as main machine after 2 years

I got my iPad Pro at the end of 2017. Before buying it, I had read many blog posts by various developers who were using iPad for programming and web development. But soon after I bought it, I realized that serious web development on iPad is hard. The biggest issue was debugging JavaScript and CSS without web tools. At first, I was having buyer’s remorse but I loved the form factor of iPad and loved drawing on it. I am used to reading on Kindle and iPad was just too heavy for long reading sessions but it was very decent reading device for quick reading sessions. So I kept it. ...

June 26, 2020 · 2 min · Amer Khalid

Git Simplified

One of most common technology that new professional developers struggle with is git. Many junior developers are eager to start using all of the powerful commands of git and usually end up getting more confused. Here are a few commands that I recommend to anyone learning git for the first time: clone This downloads entire git repo from a remote server, usually. You will get all revision history and branches. ...

May 25, 2019 · 3 min · Amer Khalid