Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf3e7703

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 parentf38d991 commitf3e7703

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

‎javascript/_994.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
functionorangesRotting(grid){
2+
constrows=grid.length;
3+
constcols=grid[0].length;
4+
letqueue=[];
5+
letfreshOranges=0;
6+
7+
// Initialize the queue with all rotten oranges and count fresh oranges
8+
for(letr=0;r<rows;r++){
9+
for(letc=0;c<cols;c++){
10+
if(grid[r][c]===2){
11+
queue.push([r,c]);
12+
}elseif(grid[r][c]===1){
13+
freshOranges++;
14+
}
15+
}
16+
}
17+
18+
// If there are no fresh oranges, return 0
19+
if(freshOranges===0)return0;
20+
21+
letminutes=0;
22+
constdirections=[
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+
letsize=queue.length;
32+
letnewRotten=false;
33+
34+
for(leti=0;i<size;i++){
35+
let[x,y]=queue.shift();
36+
37+
for(let[dx,dy]ofdirections){
38+
letnx=x+dx;
39+
letny=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+
returnfreshOranges===0 ?minutes :-1;
57+
}

‎paginated_contents/algorithms/1st_thousand/README.md

Lines changed: 1 addition & 1 deletion
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)

[8]ページ先頭

©2009-2025 Movatter.jp