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
| const nums = [3, 5, 2, 1, 7] | |
| const getMinSum = (nums, k) => { | |
| let min = Number.MAX_VALUE | |
| let curr = 0 | |
| let start = 0 | |
| let end = 0 | |
| for (let windowEnd = 0; windowEnd < nums.length; windowEnd++) { |
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
| const counter = (num, duration) => { | |
| let current = 1; | |
| const increment = () => { | |
| console.log(current); | |
| current >= num ? clearInterval(timer) : current++ | |
| }; | |
| const timer = setInterval(increment, duration); | |
| }; |
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
| //https://pokeapi.co/api/v2/pokemon?limit=100 | |
| //https://pokeapi.co/api/v2/gender/1 | |
| //https://pokeapi.co/api/v2/pokemon/1000 | |
| const fetcher = async (url) => { | |
| try { | |
| const res = await fetch(url) | |
| if (res.ok || res.status === 200) return await res.json() | |
| } | |
| catch (e) { |
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 Node { | |
| constructor(val = 0, left = null, right = null) { | |
| this.val = val; | |
| this.left = left; | |
| this.right = right; | |
| } | |
| } | |
| const root = new Node(6) | |
| root.left = new Node(3) |
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 TrieNode { | |
| constructor() { | |
| this.children = {} | |
| this.eow = false | |
| this.word = "" | |
| } | |
| } | |
| class Trie { |
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
| const quickSelect = (nums, k, kthMax) => { | |
| if (!nums.length) return; | |
| const P = nums[Math.floor(Math.random() * nums.length)]; | |
| const left = []; | |
| const mid = []; | |
| const right = []; | |
| nums.forEach(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
| const getAllKeys = (obj) => { | |
| const result = []; | |
| const queue = [{ path: [], obj }]; | |
| while (queue.length) { | |
| const { path, obj: currentObj } = queue.shift(); | |
| for (const [key, value] of Object.entries(currentObj)) { | |
| const newPath = path.concat(key); | |
| if (typeof value === 'object' && value !== null) { | |
| queue.push({ path: newPath, obj: value }); |
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 Node { | |
| constructor(val) { | |
| this.val = val; | |
| this.children = []; | |
| } | |
| addChild = (child) => this.children.push(child) | |
| } | |
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 Twitter { | |
| constructor() { | |
| this.users = {}; | |
| this.tweets = []; | |
| this.followers = {}; | |
| } | |
| postTweet(userId, content) { | |
| const tweet = { | |
| id: this.tweets.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
| function partition(a, lo, hi) { | |
| /* | |
| //Improve partition choice for the worst case | |
| const randomIndex = Math.floor(Math.random() * (hi - lo + 1)) + lo; | |
| swap(a, lo, randomIndex); // Move the random pivot to the start | |
| */ | |
| let i = lo; | |
| let j = hi + 1; | |
| let P = a[lo]; |