a blog about nothing
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.
HackerRank: New Year Chaos
Here is my solution to New Years Chaos problem from HackerRank. The first solution had a bug:
<pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-js" data-lang="js">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:
…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.
/**
* @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);
}
}
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 ;
};
LeetCode 1089: Duplicate Zeros
LeetCode 1089: Duplicate Zeros solution in PHP.
/**
* @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);
}
}
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 ;
};
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 PHP.
…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);
}
}
}
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.
…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.
…