Skip to content

problems solved#630

Open
harkhanid wants to merge 2 commits into
super30admin:mainfrom
harkhanid:main
Open

problems solved#630
harkhanid wants to merge 2 commits into
super30admin:mainfrom
harkhanid:main

Conversation

@harkhanid
Copy link
Copy Markdown

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

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:

  1. Incomplete Implementation: The while loop for BFS is incomplete. You need to dequeue each element in the current level and process its neighbors. Inside the inner for loop, you should poll the queue and check all four directions for fresh oranges. If found, mark them as rotten, add them to the queue, and decrement the fresh count.

  2. Missing Logic: You have not implemented the logic to track the time (levels) and return the time when all fresh oranges are rotten. Also, after the BFS, you need to check if there are any fresh oranges left (if count > 0) and return -1 in that case.

  3. Syntax Error: The class declaration has an extra bracket: class Solution { should be class Solution { (without the extra bracket at the end of the line). Also, the inner for loop in the while has no body, which is invalid.

  4. Edge Cases: Consider edge cases like no fresh oranges at the start (return 0) and unreachable fresh oranges (return -1). Your code should handle these.

  5. Code Structure: It's good practice to include all necessary imports. In Java, you need to import java.util.Queue and java.util.LinkedList.

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:

  • Avoid using global variables (map and sum). Instead, you can pass the map as a parameter or use a helper method that returns the total importance for a given ID. This makes your code more modular and avoids state that might be shared across multiple calls.
  • Consider using an iterative approach (BFS or DFS with a stack) to avoid potential stack overflow for very deep hierarchies. Although the constraints make recursion acceptable, iterative methods are generally more scalable.
  • In your DFS method, you are traversing all subordinates. This is correct. However, if you avoid global state, you can write the DFS to return the sum for the current node. For example:
    private int dfs(int id, Map<Integer, Employee> map) {
        Employee e = map.get(id);
        int total = e.importance;
        for (int subId : e.subordinates) {
            total += dfs(subId, map);
        }
        return total;
    }
    
    Then you can call this without needing a global sum.
  • Your code is well-commented and clear. Keep up the good work!

VERDICT: PASS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants