This file contains 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 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) { |
This file contains 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
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) { |
This file contains 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
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]; |
This file contains 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
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 = []; |
This file contains 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
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]}} |
This file contains 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
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); |
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title></title> | |
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fetch/1.0.0/fetch.js"> | |
</script> | |
<style> |
OlderNewer