diff --git a/Problem1.java b/Problem1.java new file mode 100644 index 0000000..473467c --- /dev/null +++ b/Problem1.java @@ -0,0 +1,50 @@ +//TC - O(m * n) +//SC - O(n) + +import java.util.LinkedList; +import java.util.Queue; + +public class Problem1 { + int[][] dirs = {{0,1}, {0,-1}, {1,0}, {-1,0}}; + public int orangesRotting(int[][] grid) { + int freshOranges = 0; + Queue q = new LinkedList<>(); + for(int i =0;i= 0 && nc >= 0 && nr < grid.length && nc < grid[0].length && grid[nr][nc] == 1) { + freshOranges--; + grid[nr][nc] = 2; + q.add(new int[]{nr, nc}); + } + } + } + + + } + + return freshOranges == 0 ? time-1 : -1; + } +} \ No newline at end of file diff --git a/Problem2.java b/Problem2.java new file mode 100644 index 0000000..58d0ea2 --- /dev/null +++ b/Problem2.java @@ -0,0 +1,28 @@ +//TC = O(n) +//SC - O(n) + +import java.util.HashMap; +import java.util.List; + +public class Problem2 { + public int getImportance(List employees, int id) { + HashMap hmap = new HashMap<>(); + for(Employee e : employees) { + hmap.put(e.id, e); + } + + return dfs(id, hmap); + } + + private int dfs(int id, HashMap hmap) { + + + Employee e = hmap.get(id); + int result = e.importance; + for(Integer i : e.subordinates) { + result += dfs(i, hmap); + } + + return result; + } +}