Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions EmployeeImportance.java
Original file line number Diff line number Diff line change
@@ -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<Integer> subordinates;
};
*/

class Solution {
Map<Integer, Employee> map;
public int getImportance(List<Employee> employees, int id) {
this.map = new HashMap<>();
for(Employee e : employees) {
map.put(e.id, e);
}

Queue<Employee> 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;
}
}
60 changes: 60 additions & 0 deletions RottingOranges.java
Original file line number Diff line number Diff line change
@@ -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<int[]> 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;
}
}