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) { | |
const graph = new Array(numCourses).fill(null).map(() => []); | |
prerequisites.forEach(([course, req]) => graph[course].push(req)); |
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[]} nums | |
* @param {number} k | |
* @return {number[]} | |
*/ | |
var topKFrequent = function(nums, k) { | |
const counter = new Map(); | |
nums.forEach((n) => { | |
if(!counter.has(n)) counter.set(n, 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
import random | |
from collections import defaultdict | |
# quickSelect | |
# n = len(list) | |
# k = discintElementsInList | |
# O(n + k) | |
class Solution: | |
def topKFrequent(self, nums: List[int], k: int) -> List[int]: | |
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} x | |
* @param {number} n | |
* @return {number} | |
*/ | |
/* | |
Ideas: | |
1. x^y === x^(y/2) * x^(y/2) | |
2. x^y === x * x^(y-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
/** | |
* @param {string} s | |
* @return {string} | |
*/ | |
// snazzy one liner | |
// o(n) time + o(n) space | |
var reverseWords = function(s) { | |
return s.trim().replace(/\s\s+/g, ' ').split(' ').reverse().join(' '); | |
}; |
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} hour | |
* @param {number} minutes | |
* @return {number} | |
*/ | |
var angleClock = function(hour, minutes) { | |
const mVal = (minutes === 60 ? 0 : minutes) / 60; | |
const hVal = (hour === 12 ? 0 : hour) / 12 + mVal / 12; | |
const smallerVal = Math.min( | |
Math.abs(hVal - mVal), |
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[]} nums | |
* @return {number[][]} | |
*/ | |
var subsets = function(nums) { | |
const [result, wc] = [[], []]; | |
const backtrack = (i) => { | |
if(i === nums.length) return result.push(wc.slice()); |
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} rowIndex | |
* @return {number[]} | |
*/ | |
var getRow = function(index) { | |
if(index === 0) return [1]; | |
let [result, next] = [[1], []]; | |
for(let row=1; row<=index; row++){ | |
for(let j=0; j<=result.length; 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
/** | |
* // Definition for a Node. | |
* function Node(val,prev,next,child) { | |
* this.val = val; | |
* this.prev = prev; | |
* this.next = next; | |
* this.child = 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
/* | |
Idea: We need to find a way to know the | |
horizontal position of a given node. Then | |
we can track the max and min horizontal | |
position at each depth, and subtract them | |
to get the max horizontal distance between | |
two nodes. | |
Note: Since leetcode says the width between | |
just one node and itself is 1 (not zero) we |