Hello world!

Welcome back. Starting over with blogging. I am usually focused on transferring my old post to new blogging platform before I start writing but this time, I will focus on new writings and bring old posts once in a while.

This blog has changed its focus many times, these days my number 1 priority is to be a good father and that will be focus of this blog.

Circle of Life

Lyrics

Nants ingonyama bagithi baba
Sithi uhm ingonyama
Nants ingonyama bagithi baba
Sithi uhhmm ingonyama
Ingonyama
Siyo Nqoba
Ingonyama
Ingonyama nengw' enamabala
Ingonyama nengw' enamabala
Ingonyama nengw' enamabala
Ingonyama nengw' enamabala
Ingonyama nengw' enamabala
Ingonyama nengw' enamabala (se-to-kwa!)
Ingonyama nengw' enamabala (asana)
Ingonyama nengw' enamabala (se-to-kwa!)
Ingonyama nengw' enamabala (se-to-kwa!)
From the day we arrive on the planet
And, blinking, step into the sun
There's more to see than can ever be seen
More to do than can ever be done
There's far too much to take in here
More to find than can ever be found
But the sun rolling high
Through the sapphire sky
Keeps great and small on the endless round
It's the circle of life
And it moves us all
Through despair and hope
Through faith and love
'Til we find our place
On the path unwinding
In the circle
The circle of life
Ingonyama nengw' enamabala
Ingonyama nengw' enamabala
Ingonyama nengw' enamabala (se-to-kwa!)
Ingonyama nengw' enamabala (se-to-kwa!)
Ingonyama nengw' enamabala (se-to-kwa!)
Ingonyama nengw' enamabala (se-to-kwa!)
Ingonyama nengw' enamabala (se-to-kwa!)
Ingonyama nengw' enamabala (se-to-kwa!)
Ingonyama nengw' enamabala (se-to-kwa!)
Ingonyama nengw' enamabala (se-to-kwa!)
Ingonyama nengw' enamabala (se-to-kwa!)
Ingonyama nengw' enamabala (se-to-kwa!)
Ingonyama nengw' enamabala (se-to-kwa!)
Ingonyama nengw' enamabala (se-to-kwa!)
Ingonyama nengw' enamabala (se-to-kwa!)
Ingonyama nengw' enamabala (se-to-kwa!)
It's the circle of life
And it moves us all
Through despair and hope
Through faith and love
'Til we find our place
On the path unwinding
In the circle
The circle of life

Source: LyricFind

Songwriters: Elton John / Tim Rice

What do you want to be when you grow up

Yesterday, after I picked up Ayden from his daycare, he said, he didn’t know what he wants to be. I guess they asked him this question in his class. I told him he has plenty of time to figure it out. There is no rush.

Then later at night he said, “Baba, I know what I want to be. I want to be a Tow Truck Driver.” He said it would be so much fun to tow cars.

Yesterday, after I picked up Ayden from his daycare, he said, he didn’t know what he wants to be. I guess they asked him this question in his class. I told him he has plenty of time to figure it out. There is no rush.

Then later at night he said, “Baba, I know what I want to be. I want to be a Tow Truck Driver.” He said it would be so much fun to tow cars.

LeetCode 92. Reverse Linked List II

I copied solution for Reverse Linked List II from this Udemy course. It was a little hard to understand all these pointers in second loop, I had to use pen and paper to draw each step to finally understand how each pointer works.

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} left
 * @param {number} right
 * @return {ListNode}
 */
var reverseBetween = function(head, left, right) {
    let currPos = 1;
    let currNode = head;
    let startNode = head;
    let endNode = null;
    
    while(currPos < left) {
        startNode = currNode;
        currNode = currNode.next;
        currPos++;
    }
    
    let newList = null;
    let tail = currNode;
    
    while (currPos <= right) {
        const next = currNode.next;
        currNode.next = newList;
        newList = currNode;
        currNode = next;
        currPos++;
    }
    
    startNode.next = newList;
    tail.next = currNode;
    
    if (left > 1) {
        return head;    
    }
    
    return newList;
    
    
};

LeetCode 206. Reverse Linked List

Here is a quick solution to Reverse Linked List problem.

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var reverseList = function(head) {
    let curr = head;
    let prev = null;

    while (curr) {
        let next = curr.next;
        curr.next = prev;
        prev = curr;
        curr = next;
    }

    return prev;
};

LeetCode 560. Subarray Sum Equals K

Brute Force Solution to Subarray Sum Equals K problem from LeetCode:

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number}
 */
var subarraySum = function(nums, k) {
    let ans = 0;

    for (let i=0; i<nums.length; i++) {
        let currSum = nums[i];
        if (currSum === k) ans++;
        for (let j=i+1; j<nums.length; j++) {
            currSum += nums[j];
            if (currSum === k) ans++;

        }
    }

    return ans;
};

Optimized solution after cheating a bit:

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number}
 */
var subarraySum = function(nums, k) {
    let ans = 0;
    const m = new Map();
    let sum=0;
    m.set(sum, 1);

    for (let i=0; i<nums.length; i++) {
        sum += nums[i];

        if (m.has(sum-k)) {
            ans += m.get(sum-k);
        }

        if (!m.has(sum)) {
            m.set(sum, 1);
        } else {
            m.set(sum, m.get(sum) + 1);
        }

    }

    return ans;
};

Runtime: 96 ms, faster than 94.76% of JavaScript online submissions for Subarray Sum Equals K.

Memory Usage: 46.6 MB, less than 81.14% of JavaScript online submissions for Subarray Sum Equals K.