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
| /** | |
| * @param {number[]} cost | |
| * @return {number} | |
| */ | |
| var minCostClimbingStairs = function(cost) { | |
| // recursive traverse array | |
| cost.unshift(0, 0) | |
| for (var step = 2; step < cost.length; step++) { | |
| // recalculate min cost | |
| cost[step] += Math.min(cost[step - 1], cost[step - 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
| /** | |
| * @param {number} n | |
| * @return {number} | |
| */ | |
| var climbStairs = function(n) { | |
| if (n <= 3) { | |
| return n; | |
| } | |
| let prev = 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
| /** | |
| * @param {string} beginWord | |
| * @param {string} endWord | |
| * @param {string[]} wordList | |
| * @return {number} | |
| */ | |
| var ladderLength = function(beginWord, endWord, wordList) { | |
| // edge case | |
| if(wordList.indexOf(endWord) === -1) { | |
| return 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
| /** | |
| * @param {number} n | |
| * @param {number[][]} edges | |
| * @return {boolean} | |
| */ | |
| var validTree = function(n, edges) { | |
| if (!n) { | |
| 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
| class UnionFind { | |
| constructor() { | |
| this.parent = {} | |
| } | |
| findParent(x) { | |
| if (this.parent[x] === undefined) { | |
| this.parent[x] = x | |
| } |
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
| /** | |
| * @param {number[][]} edges | |
| * @return {number[]} | |
| */ | |
| var findRedundantConnection = function(edges) { | |
| const parrents = Array.from({ | |
| length: edges.length + 1 | |
| }, (v, i) => i) | |
| const ranks = Array.from({ |
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
| /** | |
| * @param {number} numCourses | |
| * @param {number[][]} prerequisites | |
| * @return {number[]} | |
| */ | |
| var findOrder = function(numCourses, prerequisites) { | |
| let crsPre = {} | |
| for(let i = 0; i< numCourses; i++){ | |
| crsPre[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
| /** | |
| * @param {number} numCourses | |
| * @param {number[][]} prerequisites | |
| * @return {boolean} | |
| */ | |
| var canFinish = function(numCourses, prerequisites) { | |
| const preMap = new Array(numCourses).fill(0).map(()=>[]) | |
| for(let i = 0; i < prerequisites.length; i++) { | |
| let [crs, pre] = prerequisites[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
| /** | |
| * @param {number[][]} rooms | |
| * @return {void} Do not return anything, modify rooms in-place instead. | |
| */ | |
| var wallsAndGates = function(rooms) { | |
| if(!rooms || rooms.length === 0) { | |
| return; | |
| } | |
| let ROWS = rooms.length; |
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
| /** | |
| * @param {number[][]} grid | |
| * @return {number} | |
| */ | |
| var orangesRotting = function(grid) { | |
| // counter for | |
| let time = 0; | |
| let fresh = 0; // how many fresh oranges | |
| let rotten = [] // cords of rotten oranges |