File tree 2 files changed +58
-1
lines changed
paginated_contents/algorithms/1st_thousand
2 files changed +58
-1
lines changed Original file line number Diff line number Diff line change
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 neigring 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
+ }
Original file line number Diff line number Diff line change 4
4
| 991 | [ Broken Calculator] ( https://leetcode.com/problems/broken-calculator/ ) | [ Solution] ( https://.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_991.java ) | | Medium | Math, Greedy
5
5
| 981 | [ Time Based Key-Value Store] ( https://leetcode.com/problems/time-based-key-value-store/ ) | [ Solution] ( https://.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_981.java ) | [ :tv : ] ( https://youtu.be/eVi4gDimCoo ) | Medium |
6
6
| 997 | [ Find the Town Judge] ( https://leetcode.com/problems/find-the-town-judge/ ) | [ Solution] ( https://.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://.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://.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_994.java ) [ Javascript ] ( https://.com/fishercoder1534/Leetcode/blob/master/javascript/_994.js ) | | Medium | BFS
8
8
| 993 | [ Cousins in Binary Tree] ( https://leetcode.com/problems/cousins-in-binary-tree/ ) | [ Solution] ( https://.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_993.java ) | | Easy | Tree, BFS
9
9
| 989 | [ Add to Array-Form of Integer] ( https://leetcode.com/problems/add-to-array-form-of-integer/ ) | [ Solution] ( https://.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_989.java ) | | Easy | Array
10
10
| 988 | [ Smallest String Starting From Leaf] ( https://leetcode.com/problems/smallest-string-starting-from-leaf/ ) | [ Solution] ( https://.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_988.java ) | | Medium | Tree, DFS
You can’t perform that action at this time.
0 commit comments