Skip to content

Instantly share code, notes, and snippets.

View RP-3's full-sized avatar

Rohan Rogers RP-3

  • San Francisco Bay Area
View GitHub Profile
/*
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
/**
* @param {number[]} stones
* @return {boolean}
*/
var canCross = function(stones) {
if(stones[1] !== 1) return false;
const map = new Map();
stones.forEach((distance, index) => {
/**
* @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) => {
/**
* @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) {
/**
* @param {number[]} nums
* @return {number[]}
*/
var largestDivisibleSubset = function(nums) {
if(!nums.length) return []; // because leetcode...
/*
Sort ascending.
/**
* @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
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isSubsequence = function(s, t) {
if(!s.length) return true;
let i = 0;
/**
* Initialize your data structure here.
*/
var Twitter = function() {
this.tweets = {}; // 1: [[5,0]]
this.following = {};
this.tweetCount = 0;
};
/**
/**
* @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
/**
* @param {number} n
* @return {boolean}
*/
var isPowerOfTwo = function(n) {
if(n<=0) return false;
n&=(n-1);
return n === 0;
};