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[][]} intervals
* @param {number[]} newInterval
* @return {number[][]}
*/
var insert = function(intervals, newInterval) {
for(let i=0; i<intervals.length; i++){
if(fitsBefore(newInterval, i)){
/**
* @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
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++;
}
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);
// 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;
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)):
# 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:
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:
# 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
/**
* @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];