diff --git a/EmployeeImportance.java b/EmployeeImportance.java new file mode 100644 index 0000000..af850d4 --- /dev/null +++ b/EmployeeImportance.java @@ -0,0 +1,44 @@ +// Time Complexity : O(n) +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : no + +// Your code here along with comments explaining your approach +/* +Feed the combination of employee id and their respective object into a map for optimized search. Now, have +a queue to start with the given input employee and then iteratively traverse all his subordinates by +summing up the importance at each traversal and we can return it. + */ +/* +// Definition for Employee. +class Employee { + public int id; + public int importance; + public List subordinates; +}; +*/ + +class Solution { + Map map; + public int getImportance(List employees, int id) { + this.map = new HashMap<>(); + for(Employee e : employees) { + map.put(e.id, e); + } + + Queue q = new LinkedList<>(); + q.add(map.get(id)); + int totalImportance = 0; + + while(!q.isEmpty()) { + Employee e = q.poll(); + totalImportance += e.importance; + if(e.subordinates != null) { + for(int subordinate : e.subordinates) { + q.add(map.get(subordinate)); + } + } + } + return totalImportance; + } +} \ No newline at end of file diff --git a/RottingOranges.java b/RottingOranges.java new file mode 100644 index 0000000..5a76ebc --- /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 +// Any problem you faced while coding this : no + +// Your code here along with comments explaining your approach +/* +The idea is to first identify the rotten orange positions and push them into queue.Now, traverse its +nieghbours using a directions array and mark those fresh oranges(if present) as rotten and push them into +queue again as they get the eligibility because of being rotten.We can keep track of fresh oranges at the +start and at end of traversal to compare and see if all fresh oranges are rotten or not.We can leverage +time variable and increment it at each level of traversal. + */ +class Solution { + int[][] dirs; + int m, n; + public int orangesRotting(int[][] grid) { + this.dirs = new int[][] {{-1,0}, {0, -1} , {1, 0}, {0, 1}}; + int time = 0; + Queue q = new LinkedList<>(); + this.m = grid.length; + this.n = grid[0].length; + int fresh = 0; + + 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}); + else if(grid[i][j] == 1) + fresh++; + } + } + + if(fresh == 0) + return time; + + while(!q.isEmpty()) { + int size = q.size(); + time++; + 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 < m && c < n && grid[r][c] == 1) { + grid[r][c] = 2; + q.add(new int[]{r, c}); + fresh--; + + if(fresh == 0) + return time; + } + } + + } + } + + return -1; + } +} \ No newline at end of file