From 30e69e24aa0b4f1c144a1e3f155342a5cf1e7fd5 Mon Sep 17 00:00:00 2001 From: takudzwa Date: Thu, 5 Mar 2026 17:38:06 -0500 Subject: [PATCH 1/2] [getImportance && RottingOranges ] --- EmployeeImportance.java | 21 +++++++++++++++++ RottingOranges.java | 51 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 EmployeeImportance.java create mode 100644 RottingOranges.java diff --git a/EmployeeImportance.java b/EmployeeImportance.java new file mode 100644 index 0000000..d1d460b --- /dev/null +++ b/EmployeeImportance.java @@ -0,0 +1,21 @@ + +class Solution { + public int getImportance(List employees, int id) { + int total = 0 ; + HashMap map = new HashMap<>(); + Queue q = new LinkedList<>(); + for (int i = 0; i < employees.size(); i++) { + map.put(employees.get(i).id, employees.get(i)); + } + q.add(map.get(id)); + + while(!q.isEmpty()) { + Employee curr = q.poll(); + total = curr.importance + total; + for (int sub:curr.subordinates) { + q.add(map.get(sub)); + } + } + return total; + } +} \ No newline at end of file diff --git a/RottingOranges.java b/RottingOranges.java new file mode 100644 index 0000000..8025a8b --- /dev/null +++ b/RottingOranges.java @@ -0,0 +1,51 @@ +class Solution { + // TimeComplexity: O(M * N) + // SpaceComplexity: O(M * N) + // Ran succesfully on Leetcode: yes + public int orangesRotting(int[][] grid) { + // keep track of the fresh oranges + int m = grid.length; + int n = grid[0].length; + int fresh = 0; + int count = 0; + int [][] dir = new int[][] {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; + Queue q = new LinkedList<>(); + + // find index of rotten oranges and also count the 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}); + } + if(grid[i][j] == 1) { + fresh++; + } + } + } + + // check for new rotten oranges in the queue while keeping track of the fresh count + while(!q.isEmpty()) { + // take a snapshot of the size of the queue + int size = q.size(); + if(fresh == 0) return count; + count++; + for (int i = 0; i < size; i++) { + int curr[] = q.poll(); + // go through all possible directions + for (int j = 0; j < dir.length; j++) { + int r = curr[0] + dir[j][0]; + int c = curr[1] + dir[j][1]; + if (r >= 0 && c >=0 && r < m && c < n && grid[r][c] == 1) { + fresh--; + grid[r][c] = 2; + q.add(new int [] {r, c}); + } + + } + + } + } + // if still fresh left that cant be rotten return -1 + return fresh == 0 ? count : -1; + } +} \ No newline at end of file From aa5b70e411c3b84de1f7b1cb421ffa332a251836 Mon Sep 17 00:00:00 2001 From: takudzwa Date: Thu, 5 Mar 2026 17:42:44 -0500 Subject: [PATCH 2/2] [getImportance && RottingOranges ] comments --- EmployeeImportance.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/EmployeeImportance.java b/EmployeeImportance.java index d1d460b..d838565 100644 --- a/EmployeeImportance.java +++ b/EmployeeImportance.java @@ -1,17 +1,22 @@ class Solution { + // TimeComplexity: O(N) + // SpaceComplexity: O(N) + // Ran succesfully on Leetcode: yes public int getImportance(List employees, int id) { int total = 0 ; HashMap map = new HashMap<>(); Queue q = new LinkedList<>(); + // map add all ids to respctive Employee object for (int i = 0; i < employees.size(); i++) { map.put(employees.get(i).id, employees.get(i)); } q.add(map.get(id)); - + // iterate till they are no more subordinates linked to employee while(!q.isEmpty()) { Employee curr = q.poll(); total = curr.importance + total; + // add curr empl subordinates to the queue for (int sub:curr.subordinates) { q.add(map.get(sub)); }