Skip to content

Commit 940a387

Browse files
committed
[leet] Evaluate Division
1 parent fbefc03 commit 940a387

File tree

4 files changed

+160
-0
lines changed

4 files changed

+160
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
var calcEquation = function(equations, values, queries) {
2+
const graph = new Map();
3+
4+
const addEdge = (from, to, w) => {
5+
if (!graph.has(from)) graph.set(from, []);
6+
7+
graph.get(from).push([to, w]);
8+
};
9+
10+
for (let i = 0; i < equations.length; i++) {
11+
const [a, b] = equations[i];
12+
const v = values[i];
13+
addEdge(a, b, v);
14+
addEdge(b, a, 1 / v);
15+
}
16+
17+
const bfs = (start, end) => {
18+
if (!graph.has(start) || !graph.has(end)) return -1;
19+
if (start === end) return 1;
20+
21+
const q = [[start, 1]];
22+
const visited = new Set([start]);
23+
24+
while (q.length) {
25+
const [cur, prod] = q.shift();
26+
if (cur === end) return prod;
27+
28+
for (const [next, w] of graph.get(cur)) {
29+
if (visited.has(next)) continue;
30+
visited.add(next);
31+
q.push([next, prod * w]);
32+
}
33+
}
34+
return -1;
35+
};
36+
37+
const ans = new Array(queries.length);
38+
for (let i = 0; i < queries.length; i++) {
39+
const [s, t] = queries[i];
40+
ans[i] = bfs(s, t);
41+
}
42+
return ans;
43+
};
44+
45+
calcEquation([["a","b"],["b","c"]], [2.0,3.0], [["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]])
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
var calcEquation = function(equations, values, queries) {
2+
const graph = new Map()
3+
4+
const addEdgs = (from, to, w) =>{
5+
if(!graph.has(from)) graph.set(from, [])
6+
graph.get(from).push([to,w])
7+
}
8+
for(let i = 0; i < equations.length; i++){
9+
const [from, to] = equations[i]
10+
const val = values[i]
11+
addEdgs(from, to, val)
12+
addEdgs(to, from, 1/val)
13+
}
14+
const bfs = (start, end) =>{
15+
if(!graph.has(start) || !graph.has(end)) return -1
16+
if(start === end) return 1
17+
const q = [[start, 1]]
18+
19+
const visited = new Set([start])
20+
21+
while(q.length){
22+
const [cur, prod] = q.shift()
23+
if(cur === end) return prod
24+
for(const [next, w] of graph.get(cur)){
25+
if(visited.has(next)) continue
26+
visited.add(next)
27+
q.push([next, prod*w])
28+
}
29+
}
30+
return -1
31+
}
32+
33+
const ans = new Array(queries.length)
34+
for(let i = 0 ; i < queries.length; i++){
35+
const [s,t] = queries[i]
36+
ans[i] = bfs(s,t)
37+
}
38+
console.log(graph)
39+
console.log(ans)
40+
return ans
41+
42+
43+
};
44+
calcEquation([["a","b"],["b","c"]], [2.0,3.0], [["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]])
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
var calcEquation = function(equations, values, queries) {
2+
const graph = new Map()
3+
4+
const addNode = (from, to, w)=>{
5+
if(!graph.has(from)) graph.set(from, [])
6+
graph.get(from).push(to, w)
7+
}
8+
for(let i = 0 ; i < equations.length; i++){
9+
const [from, to] = equstions[i]
10+
const val = values[i]
11+
addNode(from, to, val)
12+
addNode(to, from, 1/val)
13+
}
14+
const bfs = (start, end) =>{
15+
if(start === end) return 1
16+
if(!graph.has(start) || !graph.has(end)) return -1
17+
const q= [[start ,1]]
18+
const visit = new Set()
19+
while(q.length){
20+
const [cur, w] = q.shift()
21+
if(cur === end) return w
22+
23+
for(const [next, cw] of graph.get(cur)){
24+
if(visit.has(next)) continue
25+
visit.add(next)
26+
q.push([next, w*cw])
27+
}
28+
}
29+
return -1
30+
}
31+
32+
const ans = [];
33+
for(let i = 0; i <queries.length; i++){
34+
const [s,t] =queries[i]
35+
ans.push(bfs(s,t))
36+
}
37+
return ans
38+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @param {character[][]} grid
3+
* @return {number}
4+
*/
5+
var numIslands = function(grid) {
6+
const map = grid.map((e) => e.map((e2) => Number(e2)))
7+
const dir =[[1,0], [-1,0], [0,1], [0,-1]]
8+
const bfs = (row, col) =>{
9+
map[row][col] = 0
10+
const q = [[row, col]]
11+
while(q.length){
12+
const [curY, curX] = q.shift()
13+
for(let i = 0; i < 4; i++){
14+
const ny = curY + dir[i][0]
15+
const nx = curX + dir[i][1]
16+
if(ny >= 0 && ny < map.length && nx >= 0 && nx < map[0].length&& map[ny][nx] === 1){
17+
map[ny][nx] = 0
18+
q.push([ny,nx])
19+
}
20+
}
21+
}
22+
}
23+
let ans = 0
24+
for(let i = 0 ; i < map.length; i++){
25+
for(let j = 0; j< map[0].length; j++){
26+
if(map[i][j] === 1){
27+
ans++
28+
bfs(i,j)
29+
}
30+
}
31+
}
32+
return ans
33+
};

0 commit comments

Comments
 (0)