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
| // Time complexity: O(n) where n is the number of characters. | |
| // Space complexity: O(1) for a few variables. | |
| function alternatingCharacters(x) { | |
| const len = x.length; | |
| if (len < 2) return 0; | |
| let lastChar = x[0]; | |
| let count = 0; | |
| let countA = lastChar === 'a' ? 1 : 0; |
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
| // Time complexity: O(n ^ 2) where is the number of levels and | |
| // another n is for the number of elements in each level. | |
| // Space complexity: O(2n + 1) to hold all elements, which is linear space. | |
| function pascalTriangle(level) { | |
| if (level === 0) return []; | |
| if (level === 1) return [1]; | |
| if (level === 2) return [1, 1]; | |
| const d = Array.from(Array(level), () => []); |
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
| // Given an integer n, find the minimum number of steps | |
| // to reach integer 1. | |
| // At each step, you can: | |
| // * n - 1 | |
| // * n / 2, if it is divisble by 2 | |
| // * n / 3, if it is divisble by 3 | |
| // n = 0: 0 // base case 0. | |
| // n = 1: 0 // base case 1. | |
| // n = 2: 1 // base case 2. |
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
| // i | |
| // j | |
| // 4 2 1 3 6 0 | |
| // i | |
| // j | |
| // 4 2 1 3 6 0 | |
| // 2 4 1 3 6 0 # 2 < 4 | |
| // i |
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
| { | |
| // Reference: https://bit.ly/2yjWDeY | |
| // Time complexity: O(n log n) on average or O(n^2) for worst-case | |
| // Space complexity: O(n) | |
| function quickSort(list) { | |
| if (list.length < 2) return list; | |
| const left = []; | |
| const right = []; |
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
| // http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3 | |
| // http://es5.github.io/#x11.9.3 | |
| // Abstract/ loose equality comparison will do type coercion | |
| // While strict equality comparison will compare the types | |
| null == undefined; // 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 bsr(arr, m) { | |
| let l = 0; | |
| let r = arr.length - 1; | |
| while (l <= r) { | |
| const mid = (l + (r - l) / 2) | 0; | |
| const val = arr[mid]; | |
| if (val === m) return mid; | |
| if (m > val) l = mid + 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 bfs<T>(tree: BSTNode<T>, fn: (n: T) => void) { | |
| const queue = [tree]; | |
| while (queue.length) { | |
| const cur = queue.shift(); | |
| if (cur.left) queue.push(cur.left); | |
| if (cur.right) queue.push(cur.right); | |
| fn(cur.value); |