problems solved#630
Conversation
Oranges getting rotten (RottenOrange.java)Your code shows a good start by initializing the necessary variables and setting up the queue and directions. However, there are several issues to address:
Here is a corrected version of your code in Java: import java.util.Queue;
import java.util.LinkedList;
class Solution {
public int orangesRotting(int[][] grid) {
if (grid.length == 0 || grid[0].length == 0) {
return 0;
}
int m = grid.length;
int n = grid[0].length;
Queue<int[]> q = new LinkedList<>();
int count = 0;
int[][] dirs = {{-1,0}, {1,0}, {0,1}, {0,-1}};
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1) {
count++;
} else if (grid[i][j] == 2) {
q.add(new int[]{i, j});
}
}
}
if (count == 0) return 0;
int time = 0;
while (!q.isEmpty()) {
int size = q.size();
time++;
for (int i = 0; i < size; i++) {
int[] curr = q.poll();
for (int[] dir : dirs) {
int r = curr[0] + dir[0];
int c = curr[1] + dir[1];
if (r >= 0 && c >= 0 && r < m && c < n && grid[r][c] == 1) {
grid[r][c] = 2;
q.add(new int[]{r, c});
count--;
if (count == 0) return time;
}
}
}
}
return -1;
}
}Note: The time variable starts at 0, but we increment it at the beginning of each level. However, we return time immediately when count becomes 0 inside the level. This ensures we return the correct minute. Also, we check for count==0 at the start to return 0 if there are no fresh oranges. Keep up the good work on setting up the initial structure, but make sure to complete the implementation and test with the provided examples. VERDICT: NEEDS_IMPROVEMENT Importance of Employee (Employee.java)Your solution is correct and efficient. You've correctly identified the time and space complexity. Here are some suggestions for improvement:
VERDICT: PASS |
No description provided.