This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function countdown(num) { | |
| console.log(num) | |
| // Base Case with return inside | |
| if (num === 0) | |
| return | |
| // recursive call with a different input | |
| countdown(num - 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function egg() { | |
| chicken() | |
| } | |
| function chicken() { | |
| egg() | |
| } | |
| chicken() // your PC explodes and a new universe is born |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function maxKAdjacentSum(array, k) { | |
| if (array.length < k) | |
| return null | |
| let runningSum = 0 | |
| for (let i = 0; i < k; i++) | |
| runningSum += array[i] | |
| let highestSum = runningSum | |
| for (let j = 0; j < array.length - k; j++) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function countUnique(iterable) { | |
| if (iterable.length < 1) | |
| return 0 | |
| let i = 0 | |
| let j = 1 | |
| let count = 1 | |
| for (; j < iterable.length; j++) { | |
| if (iterable[i] !== iterable[j]) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function sumZero(array) { | |
| let left = 0 | |
| let right = array.length - 1 | |
| while (left < right) { | |
| let sum = array[left] + array[right] | |
| if (sum === 0) | |
| return true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function same(a1, a2) { | |
| if (a1.length !== a2.length) | |
| return false | |
| let fc1 = frequencyCounter(a1) | |
| let fc2 = frequencyCounter(a2) | |
| for (let key in fc1) | |
| if (!key in fc2 || fc1[key] !== fc2[key]) | |
| return false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function anagrams(s1, s2) { | |
| if (s1.length !== s2.length) | |
| return false | |
| let fc1 = frequencyCounter(s1) | |
| let fc2 = frequencyCounter(s2) | |
| for (let key in fc1) | |
| if (!key in fc2 || fc1[key] !== fc2[key]) | |
| return false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function frequencyCounter(iterable) { | |
| let fc = {} | |
| for (let item of iterable) | |
| fc[item] = fc[item] + 1 || 1 | |
| return fc | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let string = "hello world" | |
| let frequencyCounter = { | |
| " ": 1 | |
| d: 1 | |
| e: 1 | |
| h: 1 | |
| l: 3 | |
| o: 2 | |
| r: 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Percolation { | |
| constructor(UnionFind, SquareGrid) {} | |
| init(gridSize) { | |
| // init union-find | |
| this.uf = new UnionFind(gridSize ** 2 + 2); | |
| // init top virtual node | |
| let top = 0; | |
| for (let i = 1; i <= gridSize; i++) { |