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: | |
If we know the depth (d) of a full binary tree, we know it has | |
- (1 + 2^d) nodes in the layers above the bottom layer | |
- Somewhere between 1 and 2^d nodes in the bottom layer | |
If we let n be the number of nodes, We can guess and check the | |
number of nodes in the bottom layer in log(n) time. | |
So do a binary search to count the number of nodes in the bottom |
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[]} stones | |
* @return {boolean} | |
*/ | |
var canCross = function(stones) { | |
if(stones[1] !== 1) return false; | |
const map = new Map(); | |
stones.forEach((distance, index) => { |
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 | |
* @param {string} p | |
* @return {boolean} | |
*/ | |
var isMatch = function(s, p) { | |
const memo = new Array(s.length+1).fill(0).map(() => new Array(p.length).fill(-1)); | |
const r = (i, 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
/** | |
* @param {number} n | |
* @param {number[][]} flights | |
* @param {number} src | |
* @param {number} dst | |
* @param {number} K | |
* @return {number} | |
*/ | |
var findCheapestPrice = function(n, flights, src, dst, K) { | |
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 largestDivisibleSubset = function(nums) { | |
if(!nums.length) return []; // because leetcode... | |
/* | |
Sort ascending. |
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[]} | |
*/ | |
// IDEA: Inserting into a BST is log(n) time | |
// So for each val, starting from the right, | |
// insert it into a BST and count the number | |
// of nodes to the left of it. | |
// The number of nodes to the left === the |
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 | |
* @param {string} t | |
* @return {boolean} | |
*/ | |
var isSubsequence = function(s, t) { | |
if(!s.length) return true; | |
let i = 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
/** | |
* Initialize your data structure here. | |
*/ | |
var Twitter = function() { | |
this.tweets = {}; // 1: [[5,0]] | |
this.following = {}; | |
this.tweetCount = 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 | |
* @return {boolean} | |
*/ | |
// Idea: if it's a power of two, there must | |
// be only one bit set. | |
var isPowerOfTwo = function(n) { | |
if(n<=0) return false; // it could be negative. Return false; | |
n&=(n-1); // unset the lowest set bit | |
return n === 0; // If there are any bits left, return false. Otherwise |
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 {boolean} | |
*/ | |
var isPowerOfTwo = function(n) { | |
if(n<=0) return false; | |
n&=(n-1); | |
return n === 0; | |
}; |