diff --git a/EmployeeImportance.java b/EmployeeImportance.java new file mode 100644 index 0000000..3519492 --- /dev/null +++ b/EmployeeImportance.java @@ -0,0 +1,34 @@ +// Time Complexity : O(n) +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : Yes + +//Description: +// - Store all employees in a HashMap for O(1) lookup by employee ID. +// - Perform BFS starting from the given employee, accumulating importance. +// - Visit each subordinate exactly once and enqueue their subordinates for traversal. + +class EmployeeImportance { + public int getImportance(List employees, int id) { + Map employeeMap = new HashMap<>(); + + for(Employee employee : employees) { + employeeMap.put(employee.id, employee); + } + + Queue queue = new LinkedList<>(); + queue.offer(id); + + int importance = 0; + while(!queue.isEmpty()) { + int employeeId = queue.poll(); + Employee employee = employeeMap.get(employeeId); + importance += employee.importance; + + for(int subOrdinateId : employee.subordinates) { + queue.offer(subOrdinateId); + } + } + + return importance; + } +} \ No newline at end of file diff --git a/RottingOranges.java b/RottingOranges.java new file mode 100644 index 0000000..e8b08b8 --- /dev/null +++ b/RottingOranges.java @@ -0,0 +1,60 @@ +// Time Complexity : O(m * n) +// Space Complexity : O(m * n) +// Did this code successfully run on Leetcode : Yes + +//Description: +// - Enqueue all initially rotten oranges and count fresh oranges. +// - Perform multi-source BFS, rotting adjacent fresh oranges level by level. +// - Track elapsed time for each BFS level and return -1 if fresh oranges remain. + +class RottingOranges { + public int orangesRotting(int[][] grid) { + int[][] directions = new int[][] { + {-1, 0}, //up + {1, 0}, //down + {0, -1}, //left + {0,1} //right + }; + int rowSize = grid.length; + int colSize = grid[0].length; + + Queue cells = new LinkedList<>(); + int freshCount = 0; + for(int i = 0; i < rowSize; i++) { + for(int j = 0; j < colSize; j++) { + if(grid[i][j] == 2) { + cells.offer(new int[]{i, j}); + } else if(grid[i][j] == 1) { + freshCount++; + } + } + } + + int time = 0; + while(!cells.isEmpty() & freshCount > 0) { + int size = cells.size(); + + for(int i = 0; i < size; i++) { + int[] cell = cells.poll(); + for(int[] direction : directions){ + int newRow = cell[0] + direction[0]; + int newCol = cell[1] + direction[1]; + + if(newRow >= 0 && newRow < rowSize && newCol >=0 && newCol < colSize && grid[newRow][newCol] == 1) { + grid[newRow][newCol] = 2; + freshCount--; + cells.offer(new int[]{newRow, newCol}); + } + } + } + + time++; + } + + if(freshCount > 0) { + return -1; + } + + return time; + } +} \ No newline at end of file