Skip to content

Commit f3e7703

Browse files
authored
js solution 994 (#189)
* added js solution to _3 * js solution to _17 * added js solution links to readme * updated links to js solutions * added js solution 994 * added solution link to readme --------- Co-authored-by: sambabib <adekyte.gmail.com>
1 parent f38d991 commit f3e7703

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

javascript/_994.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
function orangesRotting(grid) {
2+
const rows = grid.length;
3+
const cols = grid[0].length;
4+
let queue = [];
5+
let freshOranges = 0;
6+
7+
// Initialize the queue with all rotten oranges and count fresh oranges
8+
for (let r = 0; r < rows; r++) {
9+
for (let c = 0; c < cols; c++) {
10+
if (grid[r][c] === 2) {
11+
queue.push([r, c]);
12+
} else if (grid[r][c] === 1) {
13+
freshOranges++;
14+
}
15+
}
16+
}
17+
18+
// If there are no fresh oranges, return 0
19+
if (freshOranges === 0) return 0;
20+
21+
let minutes = 0;
22+
const directions = [
23+
[0, 1], // right
24+
[1, 0], // down
25+
[0, -1], // left
26+
[-1, 0] // up
27+
];
28+
29+
// Step 2: Perform BFS
30+
while (queue.length > 0) {
31+
let size = queue.length;
32+
let newRotten = false;
33+
34+
for (let i = 0; i < size; i++) {
35+
let [x, y] = queue.shift();
36+
37+
for (let [dx, dy] of directions) {
38+
let nx = x + dx;
39+
let ny = y + dy;
40+
41+
// Check if the neighboring cell is a fresh orange
42+
if (nx >= 0 && ny >= 0 && nx < rows && ny < cols && grid[nx][ny] === 1) {
43+
grid[nx][ny] = 2; // Make it rotten
44+
freshOranges--; // Decrease count of fresh oranges
45+
queue.push([nx, ny]); // Add it to the queue
46+
newRotten = true;
47+
}
48+
}
49+
}
50+
51+
// If rotten oranges exist, increment minutes
52+
if (newRotten) minutes++;
53+
}
54+
55+
// Check if there are any fresh oranges left
56+
return freshOranges === 0 ? minutes : -1;
57+
}

paginated_contents/algorithms/1st_thousand/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
| 991 | [Broken Calculator](https://leetcode.com/problems/broken-calculator/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_991.java) | | Medium | Math, Greedy
55
| 981 | [Time Based Key-Value Store](https://leetcode.com/problems/time-based-key-value-store/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_981.java) | [:tv:](https://youtu.be/eVi4gDimCoo) | Medium |
66
| 997 | [Find the Town Judge](https://leetcode.com/problems/find-the-town-judge/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_997.java) | | Easy |
7-
| 994 | [Rotting Oranges](https://leetcode.com/problems/rotting-oranges/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_994.java) | | Medium | BFS
7+
| 994 | [Rotting Oranges](https://leetcode.com/problems/rotting-oranges/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_994.java) [Javascript](https://github.com/fishercoder1534/Leetcode/blob/master/javascript/_994.js) | | Medium | BFS
88
| 993 | [Cousins in Binary Tree](https://leetcode.com/problems/cousins-in-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_993.java) | | Easy | Tree, BFS
99
| 989 | [Add to Array-Form of Integer](https://leetcode.com/problems/add-to-array-form-of-integer/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_989.java) | | Easy | Array
1010
| 988 | [Smallest String Starting From Leaf](https://leetcode.com/problems/smallest-string-starting-from-leaf/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_988.java) | | Medium | Tree, DFS

0 commit comments

Comments
 (0)