From b373099f114b2fcd78f146a30b93416cacbdde7e Mon Sep 17 00:00:00 2001 From: MeghaN28 Date: Sat, 9 May 2026 23:47:56 -0700 Subject: [PATCH] BFS 1-2 --- EmployeeImportanceDFS.java | 57 +++++++++++++++++++++++++ RottenOrangeBFS.java | 85 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 EmployeeImportanceDFS.java create mode 100644 RottenOrangeBFS.java diff --git a/EmployeeImportanceDFS.java b/EmployeeImportanceDFS.java new file mode 100644 index 0000000..fac26d7 --- /dev/null +++ b/EmployeeImportanceDFS.java @@ -0,0 +1,57 @@ +// Time Complexity : O(n) where n is the number of employees in the list +// Space Complexity : O(n) where n is the number of employees in the list +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No +// Your code here along with comments explaining your approach +// We can use a depth first search approach to solve this problem. +// We can start from the given employee id and then we can add the importance of the employee to the result and then we can call the dfs function for all the subordinates of the employee. +// We can keep track of the importance of the employees in a map and then we can return the result at the end. +// We can also use a breadth first search approach to solve this problem. We can use a queue to keep track of the employees and then we can add the importance of the employees to the result and then we can add the subordinates of the employees to the queue. We can keep track of the importance of the employees in a map and then we can return the result at the end. DFS could also be used to solve this problem. + + +// Your code here along with comments explaining your approach +public import java.util.*; + +import java.util.HashMap; + + +// Definition for Employee. +class Employee { + public int id; + public int importance; + public List subordinates; +}; + + +class Solution { + + Map map; + + int result; + + public int getImportance(List employees, int id) { + + map = new HashMap<>(); + + result = 0; + + for (Employee emp : employees) { + map.put(emp.id, emp); + } + + dfs(id); + + return result; + } + + private void dfs(int id) { + + Employee emp = map.get(id); + + result = result + emp.importance; + + for (int ids : emp.subordinates) { + dfs(ids); + } + } +} \ No newline at end of file diff --git a/RottenOrangeBFS.java b/RottenOrangeBFS.java new file mode 100644 index 0000000..eb7f900 --- /dev/null +++ b/RottenOrangeBFS.java @@ -0,0 +1,85 @@ +// Time Complexity : O(m*n) where m is the number of rows and n is the number of columns in the grid +// Space Complexity : O(m*n) where m is the number of rows and n is the number of columns in the grid +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : +//No +// Your code here along with comments explaining your approach +// We can use a breadth first search approach to solve this problem. +// We can start from all the rotten oranges and then we can rot the adjacent fresh oranges. +// We can keep track of the time taken to rot all the oranges and return the time taken at the end. +// If there are still fresh oranges left, we can return -1. +// We can use a queue to keep track of the rotten oranges and a variable to keep track of the time taken to rot all the oranges. +// We can also keep track of the number of fresh oranges and decrement it every time we rot a fresh orange. +// DFS could also be used to solve this problem. + +// Your code here along with comments explaining your approach +import java.util.*; + +class Solution { + + public int orangesRotting(int[][] grid) { + + int[][] directions = new int[][] + { + {-1, 0}, + {1, 0}, + {0, -1}, + {0, 1} + }; + + Queue que = new LinkedList<>(); + + int m = grid.length; + int n = grid[0].length; + + int fresh = 0; + + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + + if (grid[i][j] == 2) { + que.offer(new int[] { i, j }); + } + else if (grid[i][j] == 1) { + fresh++; + } + } + } + + if (fresh == 0) return 0; + + int time = 0; + + while (!que.isEmpty() && fresh > 0) { + + int size = que.size(); + + for (int i = 0; i < size; i++) { + + int[] curr = que.poll(); + + int row = curr[0]; + int col = curr[1]; + + for (int[] dir : directions) { + + int nr = row + dir[0]; + int nc = col + dir[1]; + + if (nr >= 0 && nr < m && + nc >= 0 && nc < n && + grid[nr][nc] == 1) { + + grid[nr][nc] = 2; + fresh--; + que.offer(new int[] { nr, nc }); + } + } + } + + time++; + } + + return fresh == 0 ? time : -1; + } +} \ No newline at end of file