diff --git a/EmployeeImportance.java b/EmployeeImportance.java new file mode 100644 index 0000000..038cf98 --- /dev/null +++ b/EmployeeImportance.java @@ -0,0 +1,42 @@ +/* +// Definition for Employee. +class Employee { + public int id; + public int importance; + public List subordinates; +}; +*/ + +class Solution { + // TC - O(n) + // SC - O(n) + /* + This solution uses DFS to calculate the total importance of an employee and all of their subordinates. + A HashMap is first created to map each employee's ID to their Employee object, allowing O(1) lookups. + Starting from the given employee ID, the DFS adds the employee's own importance to the total. + It then recursively visits each subordinate and adds their importance as well. + This process continues until all direct and indirect subordinates have been visited. + The final result is the sum of the importance values of the employee and everyone under them. + + */ + public int getImportance(List employees, int id) { + HashMap map = new HashMap<>(); + for (Employee e : employees) { + map.put(e.id, e); + } + + return dfs(map, id); + } + + private int dfs(HashMap map, int id) { + Employee e = map.get(id); + int result = 0; + result += e.importance; + + for (int eid : e.subordinates) { + result += dfs(map, eid); + } + + return result; + } +} \ No newline at end of file diff --git a/RottingOranges.java b/RottingOranges.java new file mode 100644 index 0000000..61cb709 --- /dev/null +++ b/RottingOranges.java @@ -0,0 +1,59 @@ +class Solution { + /* + TC - O(m * n) + SC - O(m * n) + This solution uses multi-source BFS to simulate how the oranges rot minute by minute. + First, all initially rotten oranges are added to a queue, and the number of fresh oranges is counted. + If there are no fresh oranges, the answer is 0. + The BFS processes the queue level by level, where each level represents one minute. + For every rotten orange, its four neighboring cells are checked. If a neighboring orange is fresh, it becomes rotten, is added to the queue, and the fresh orange count is decreased. + After processing one level of the queue, one minute has passed, so time is incremented. + When the BFS finishes: + If any fresh oranges remain, they could not be reached, so return -1. + Otherwise, return time - 1, since the last increment happens after processing the final level. + */ + public int orangesRotting(int[][] grid) { + Queue q = new LinkedList<>(); + int freshCount = 0; + + for (int i = 0; i < grid.length; i++) { + for (int j = 0; j < grid[0].length; j++) { + if (grid[i][j] == 2) { + q.add(new int[]{i, j}); + } + if (grid[i][j] == 1) { + freshCount++; + } + } + } + + if (freshCount == 0) { + return 0; + } + + int[][] dirs = new int[][]{{1, 0}, {0, 1}, {0, -1}, {-1, 0}}; + int time = 0; + + while (!q.isEmpty()) { + int size = q.size(); + for (int i = 0; i < size; i++) { + int[] curr = q.poll(); + for (int[] dir : dirs) { + int nr = curr[0] + dir[0]; + int nc = curr[1] + dir[1]; + + if (nr >= 0 && nc >= 0 && nr < grid.length && nc < grid[0].length && grid[nr][nc] == 1) { + q.add(new int[]{nr, nc}); + grid[nr][nc] = 2; + freshCount--; + } + } + } + time++; + } + + if (freshCount != 0) return -1; + + return time - 1; + } +} \ No newline at end of file