Skip to content

Instantly share code, notes, and snippets.

function calcEquation(equations, values, queries) {
const parent = {};
const children = {};
const weight = {};
for (let i = 0; i < values.length; i++) {
union(equations[i][0], equations[i][1], values[i]);
}
const res = [];
function cleanRoom(robot) {
const seen = new Set();
const dirs = [[-1, 0], [0, 1], [1, 0], [0, -1]];
dfs(0, 0, 0);
function dfs(dir, r, c) {
robot.clean();
seen.add(r+':'+c);
for (let i = 0; i < 4; i++) {
const [dr, dc] = dirs[(dir+i)%4];
@linfongi
linfongi / code.js
Created June 22, 2018 05:09
code.js
function shortestPathLength(graph) {
const dp = [...Array(graph.length)].map(r => Array(1<<(graph.length+1)).fill(Math.MAX_VALUE));
const queue = [];
for (let v = 0; v < graph.length; v++) {
dp[v][1<<v] = 0;
queue.unshift([v, 1<<v]);
}
while (queue.length) {
function Heap(n=((n,t)=>n-t)){const t=[];return{push:function(e){t.push(e),function(){let e=t.length-1,o=e-1>>1;for(;0!==e&&n(t[e],t[o])<0;)[t[e],t[o]]=[t[o],t[e]],o=(e=o)-1>>1}()},pop:function(){const o=e(),u=t.pop();t.length>0&&(t[0]=u,function(){let e=0;for(;;){let o=e;for(const u of[2*e+1,2*e+2])u<t.length&&n(t[u],t[o])<0&&(o=u);if(o===e)return;[t[e],t[o]]=[t[o],t[e]],e=o}}());return o},peek:e,size:function(){return t.length}};function e(){return t[0]}}
function Heap(compare = (a, b) => a - b) {
const arr = [];
return { push, pop, peek, size };
function size() {
return arr.length;
}
function push(v) {
arr.push(v);
var LRUCache = function(capacity) {
// beofre <--> { k0, v0 } <--> { k1, v1 }
const map = {}; // map key to node
const before = {};
let count = 0;
let tail = before;
return { get, put };
function get(k) {
@linfongi
linfongi / 400.java
Last active February 16, 2017 06:46
public class Solution {
public int findNthDigit(int n) {
int digitCount = 1;
long threshold = 9 * (long)Math.pow(10, digitCount - 1) * digitCount;
while (n > threshold) {
n -= threshold;
threshold /= digitCount;
threshold *= 10 * (++digitCount);
}
@linfongi
linfongi / Note.md
Created October 9, 2016 22:34
Note

Notes

  • HTML5 history API

variable

prime_counter = 1