diff --git a/EmployeeImportance.java b/EmployeeImportance.java new file mode 100644 index 0000000..340506e --- /dev/null +++ b/EmployeeImportance.java @@ -0,0 +1,36 @@ +import java.util.*; + +// BFS O(n) time, O(n) space +class Solution { + public int getImportance(List employees, int id) { + Map map = new HashMap<>(); + + for (Employee emp : employees) { + map.put(id, emp); + } + + int ans = 0; + + Queue q = new LinkedList<>(); + q.offer(map.get(id)); + + while (!q.isEmpty()) { + Employee curr = q.poll(); + + ans += curr.importance; + + for (int subId : curr.subordinates) { + q.offer(map.get(subId)); + } + } + return ans; + } +} + + +// Definition for Employee. +class Employee { + public int id; + public int importance; + public List subordinates; +}; \ No newline at end of file diff --git a/RottingOranges.java b/RottingOranges.java new file mode 100644 index 0000000..b2b31a9 --- /dev/null +++ b/RottingOranges.java @@ -0,0 +1,49 @@ +import java.util.*; + +// BFS O(m * n) time, O(m * n) space +class Solution { + public int orangesRotting(int[][] grid) { + int n = grid.length; + int m = grid[0].length; + int[][] dirs = new int[][] { + {0,1}, {1,0}, {-1,0}, {0,-1} + }; + + Queue q = new LinkedList<>(); + + int freshCount = 0; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if (grid[i][j] == 2) { + q.offer(new int[] {i, j}); + } + else if (grid[i][j] == 1) { + freshCount++; + } + } + } + + 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 r = curr[0] + dir[0]; + int c = curr[1] + dir[1]; + + if (r >= 0 && c >= 0 && r < n && c < n && grid[r][c] == 1) { + q.offer(new int[] {r, c}); + grid[r][c] = 2; // mark rotten + freshCount--; + } + } + } + time++; // increment time after each level + } + if (freshCount == 0) return time-1; + + return -1; + } +}