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[][]} intervals | |
* @param {number[]} newInterval | |
* @return {number[][]} | |
*/ | |
var insert = function(intervals, newInterval) { | |
for(let i=0; i<intervals.length; i++){ | |
if(fitsBefore(newInterval, i)){ |
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[][]} equations | |
* @param {number[]} values | |
* @param {string[][]} queries | |
* @return {number[]} | |
*/ | |
var calcEquation = function(equations, values, queries) { | |
const idents = {}; // could use map but longer syntax | |
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
var longestOnes = function(A, K) { | |
let result = 0; | |
let i=0; let j=0; let k=K; | |
while(j < A.length){ | |
if(A[j] === 1){ | |
result = Math.max(result, j-i+1); | |
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
var shortestAlternatingPaths = function(n, red_edges, blue_edges) { | |
const reds = {}; const blues= {}; | |
red_edges.forEach(([from, to]) => { | |
reds[from] = reds[from] || new Set(); | |
reds[from].add(to); | |
}); | |
blue_edges.forEach(([from, to]) => { | |
blues[from] = blues[from] || new Set(); | |
blues[from].add(to); |
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
// Straight-up BFS. Very slow but accepted... | |
var minKnightMoves = function(x, y) { | |
const queue = [[0, 0, 0]]; // x, y, distance | |
const visited = new Map(); // `x:y`: distance | |
while(queue.length){ | |
const [X, Y, dist] = queue.shift(); | |
if(visited.has(`${X}:${Y}`)) continue; | |
visited.set(`${X}:${Y}`, dist); | |
if(X === x && Y === y) return dist; |
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
from collections import deque | |
class Solution: | |
def orangesRotting(self, grid: List[List[int]]) -> int: | |
q = deque() | |
seen = set() | |
maxDay = 0 | |
for row in range(len(grid)): |
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
# First naiive attempt | |
class Solution: | |
""" | |
:type nums: List[int] | |
:rtype: void Do not return anything, modify nums in-place instead. | |
""" | |
def sortColors(self, nums): | |
pl = 0 | |
pr = len(nums)-1 | |
while pl < pr: |
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
from collections import deque | |
class Solution: | |
def criticalConnections(self, n: int, connections: List[List[int]]) -> List[List[int]]: | |
results = [] | |
graph = {} | |
for a, b in connections: |
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 singly-linked list. | |
# class ListNode: | |
# def __init__(self, x): | |
# self.val = x | |
# self.next = None | |
# Definition for a binary tree node. | |
# class TreeNode: | |
# def __init__(self, x): | |
# self.val = 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
/** | |
* @param {string[][]} accounts | |
* @return {string[][]} | |
*/ | |
var accountsMerge = function(accounts) { | |
const accountsWithEmail = new Map(); //<email: set(accountId)> | |
accounts.forEach((account, id) => { | |
for(let i=1; i<account.length; i++){ | |
const email = account[i]; |