a blog about nothing

LeetCode 605. Can Place Flowers


/**
 * @param {number[]} flowerbed
 * @param {number} n
 * @return {boolean}
 */
 var  canPlaceFlowers   = function (flowerbed , n ) {
     for  ( let  i   =   0 ; i  < flowerbed .length ; i  ++ ) {
         if  (flowerbed [i ]  ==   0
            &&  (i   ==   0   ||  flowerbed [i  -  1 ]  ==   0 )
            &&  (i   ==  flowerbed .length  -  1   ||  flowerbed [i  +  1 ]  ==   0 )) {
            n  --
            flowerbed [i ]  =   1
             if  (n   <   1 )
                 return true
        }
    }

     return  n   <=   0
};
Read more ⟶

LeetCode 1268. Search Suggestions System


/**
 * @param {string[]} products
 * @param {string} searchWord
 * @return {string[][]}
 */
 var  suggestedProducts   = function (products , searchWord ) {
    products .sort ((a , b ) => {
         if  (a   >  b )  return   1
         if  (a   <  b )  return   -  1
         return   0
    })

     const  r   =  []

     for  ( let  i   =   1 ; i  <= searchWord .length ; i  ++ ) {
         const  arr   =  products .filter ((s ) => s .startsWith (searchWord .substring ( 0 , i ))).slice ( 0 ,  3 )
       //console.log(searchWord.substring(0, i))
         r .push (arr )
    }

     return  r
};
Read more ⟶

LeetCode 253. Meeting Rooms II


Lack of built-in Priority Queues in JavaScript makes it a bit hard to do problems like this. I end looking at the solution:

/**
 * @param {number[][]} intervals
 * @return {number}
 */
 var  minMeetingRooms   = function (intervals ) {
     if  (intervals .length   <   1 )  return   0

     const  startTimes   =  []
     const  endTimes   =  []

     for  ( let  i   =   0 ; i  < intervals .length ; i  ++ ) {
        startTimes [i ]  =  intervals [i ][ 0 ]
        endTimes [i ]  =  intervals [i ][ 1 ]
    }

    startTimes .sort ((a , b ) => a  - b )
    endTimes .sort ((a , b ) => a  - b )

     let  startPtr   =   0
     let  endPtr   =   0
     let  rooms   =   0

     for  ( let  i  =  0 ; i  < intervals .length ; i  ++ ) {
         if  (startTimes [i ]  >=  endTimes [endPtr ]) {
            endPtr  ++
        }  else  {
            rooms  ++
        }
    }

     return  rooms
};
Read more ⟶

LeetCode 937. Reorder Data in Log Files


/**
 * @param {string[]} logs
 * @return {string[]}
 */
var reorderLogFiles = function(logs) {

    const sorted = []
    const numLogs = []
    const charLogs = []

    for (let i = 0; i<logs.length; i++) {
        const log = logs[i]
        const tokenized = log.split(&#34; &#34;)
        if (Number.isInteger(parseInt(tokenized[1]))) {
            numLogs.push(log)
        } else {
            charLogs.push(log)
        }
    }

    charLogs.sort(function (a, b) {
        const stra = a.substr(a.indexOf(' ') + 1)
        const strb = b.substr(b.indexOf(' ') + 1)
        if (stra > strb) {
            return 1
        }
        if (stra < strb) {
            return -1
        }

        const ida = a.substr(0, a.indexOf(' '))
        const idb = b.substr(0, b.indexOf(' '))
        //console.log({ida})
        if (ida > idb) {
            return 1
        }
        if (ida < idb) {
            return -1
        }

        return 0
    })

    return [...charLogs, ...numLogs]

};
Read more ⟶

LeetCode 49. Group Anagrams


/**
 * @param {string[]} strs
 * @return {string[][]}
 */
var groupAnagrams = function(strs) {
    const map = []

    for (let i = 0; i<strs.length; i++) {
        const str = strs[i].split('').sort().join('')
        if (!map[str]) map[str] = []
        map[str].push(strs[i])
    }
    const ans = []
    for (let k in map) {
        ans.push(map[k])
    }
    return ans
};
Read more ⟶

LeetCode 71. Simplify Path


/**
 * @param {string} path
 * @return {string}
 */
var simplifyPath = function(path) {
    const simple = [];
    const splitPath = path.split(&#34;/&#34;).filter(x => x.length>0 && x !== '.')

    for (let i = 0; i<splitPath.length; i++) {
        if (splitPath[i] === '..') {
            simple.pop()
        } else {
            simple.push(splitPath[i])
        }

    }

    return `/${simple.join('/')}`
};
Read more ⟶

Ask HN: What is your money-making side project outside programming?


Read more ⟶

An Algorithm for Passing Programming Interviews | Hacker News


An Algorithm for Passing Programming Interviews | Hacker News — Read on news.ycombinator.com/item

Read more ⟶

Avicii – the Nights


Lyrics:

Once upon a younger year
When all our shadows disappeared
The animals inside came out to play
Went face to face with all our fears
Learned our lessons through the tears
Made memories we knew would never fade

One day my father, he told me
"Son, don't let it slip away"
He took me in his arms, I heard him say
"When you get older
Your wild heart will live for younger days
Think of me if ever you're afraid"

He said: "One day you'll leave this world behind
So live a life you will remember"
My father told me when I was just a child
"These are the nights that never die"
My father told me

When thunder clouds start pouring down
Light a fire they can't put out
Carve your name into those shining stars
He said: "Go venture far beyond the shores
Don't forsake this life of yours
I'll guide you home, no matter where you are"

One day my father, he told me
"Son, don't let it slip away"
When I was just a kid, I heard him say
"When you get older
Your wild heart will live for younger days
Think of me if ever you're afraid"

He said: "One day you'll leave this world behind
So live a life you will remember"
My father told me when I was just a child
"These are the nights that never die"
My father told me

"These are the nights that never die"
My father told me
My father told me
Read more ⟶

LeetCode 50. Pow(x, n)


I got stuck on this problem, even after looking at answers, I could not follow algorithm. Following video really helped me understand it:

Here is my solution after watching this video:

/**
 * @param {number} x
 * @param {number} n
 * @return {number}
 */
 var  myPow   = function (x , n ) {
     if  (x   ==   0 )  return   0

     if  (n   ==   0 )  return   1

     if  (n   <   0 ) {
        x   =   1  / x
        n   =   -  1   *  n
    }

     const  half   =  myPow (x , Math.floor (n  /  2 ))
     let  ans   =  half   *  half

     if  (n  %  2   ==   1 ) ans   =  x   *  ans

     return  ans
};
Read more ⟶