From 2838276b43d6bbbd7e34eda82a141ca2bda2a98a Mon Sep 17 00:00:00 2001 From: sainathek Date: Thu, 12 Mar 2026 11:03:19 -0400 Subject: [PATCH] Done BFS-2.1 --- problem1.java | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++ problem2.java | 67 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 problem1.java create mode 100644 problem2.java diff --git a/problem1.java b/problem1.java new file mode 100644 index 0000000..c9c84de --- /dev/null +++ b/problem1.java @@ -0,0 +1,84 @@ + /** + Time Complexity : O(M * N) + Explanation: + We scan the grid once to find initial rotten and fresh oranges. + Then each cell is processed at most once during BFS. + + Space Complexity : O(M * N) + Explanation: + Queue may contain many cells in worst case when oranges rot level by level. + + Did this code successfully run on LeetCode : Yes + + Any problem you faced while coding this : + Initially struggled with tracking the time correctly during BFS levels. + Fixed it by processing the queue level by level (each level = 1 minute). + Also needed to keep track of the number of fresh oranges so we know + when all oranges have rotted and can return the time immediately. + */ + + + class Solution { + + + public int orangesRotting(int[][] grid) { + + int m = grid.length; + int n = grid[0].length; + + // Directions: right, left, up, down + int[][] adj = {{0,1}, {0,-1}, {-1,0}, {1,0}}; + + Queue q = new LinkedList<>(); + int fresh = 0; + int time = 0; + + // Step 1: Find all rotten oranges and count fresh oranges + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + + if (grid[i][j] == 2) { + q.add(new int[]{i, j}); + } + else if (grid[i][j] == 1) { + fresh++; + } + } + } + + if (fresh == 0) return 0; + + // Step 2: BFS to rot adjacent fresh oranges + while (!q.isEmpty()) { + + int size = q.size(); + + for (int i = 0; i < size; i++) { + + int[] rotten = q.poll(); + + for (int[] dir : adj) { + + int nr = rotten[0] + dir[0]; + int nc = rotten[1] + dir[1]; + + if (nr >= 0 && nr < m && nc >= 0 && nc < n) { + + if (grid[nr][nc] == 1) { + + grid[nr][nc] = 2; + q.add(new int[]{nr, nc}); + fresh--; + + if (fresh == 0) return time + 1; + } + } + } + } + + time++; + } + + return -1; + } + } \ No newline at end of file diff --git a/problem2.java b/problem2.java new file mode 100644 index 0000000..449bd04 --- /dev/null +++ b/problem2.java @@ -0,0 +1,67 @@ + + /** + Time Complexity : O(N) + Explanation: + We first build a HashMap of employees in O(N). + Then BFS traverses each employee and their subordinates once. + + Space Complexity : O(N) + Explanation: + HashMap stores all employees and queue stores employees during BFS traversal. + + Did this code successfully run on LeetCode : Yes + + Any problem you faced while coding this : + Initially it was difficult to access employees by their id quickly. + Solved it by creating a HashMap that maps employee id to Employee object. + Then used BFS starting from the given id and added the importance + of the employee and all their subordinates. + */ + +/* +// Definition for Employee. +class Employee { + public int id; + public int importance; + public List subordinates; +}; +*/ + +class Solution { + + + public int getImportance(List employees, int id) { + + Queue q = new LinkedList<>(); + int n = employees.size(); + + HashMap hmap = new HashMap<>(); + + // Build id -> Employee map + for (int i = 0; i < n; i++) { + Employee e = employees.get(i); + hmap.put(e.id, e); + + if (id == e.id) { + q.add(e.id); + } + } + + int total = 0; + + // BFS through employee hierarchy + while (!q.isEmpty()) { + + int curr = q.poll(); + Employee emp = hmap.get(curr); + + total += emp.importance; + + for (Integer sub : emp.subordinates) { + q.add(sub); + } + } + + return total; + } +} \ No newline at end of file