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
/**
* @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));
/**
* @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);
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]:
/**
* @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)
/**
* @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(' ');
};
/**
* @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),
/**
* @param {number[]} nums
* @return {number[][]}
*/
var subsets = function(nums) {
const [result, wc] = [[], []];
const backtrack = (i) => {
if(i === nums.length) return result.push(wc.slice());
/**
* @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++){
/**
* // Definition for a Node.
* function Node(val,prev,next,child) {
* this.val = val;
* this.prev = prev;
* this.next = next;
* this.child = child;
* };
*/
/*
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