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
23 changes: 23 additions & 0 deletions EmployeeImportance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Time Complexity : O(n)
// Spcae Complexity: O(n)
// Explanaytion : I first store every employee in a HashMap using their id so that I can retrieve any employee in O(1) time. Then I use a queue to perform a BFS starting from the given id, adding all subordinates level by level. For each employee I dequeue, I add their importance to the total and push their subordinates into the queue.
class Solution {
public int getImportance(List<Employee> employees, int id) {
Map<Integer, Employee> map = new HashMap<>();
Queue<Integer> q = new LinkedList<>();
for(int i=0; i<employees.size(); i++) {
map.put(employees.get(i).id, employees.get(i));
}
q.add(id);
int sum =0;
while(!q.isEmpty()) {
int currId = q.poll();
Employee curr = map.get(currId);
sum = sum + curr.importance;
for(int i=0; i <curr.subordinates.size(); i++) {
q.add(curr.subordinates.get(i));
}
}
return sum;
}
}
55 changes: 55 additions & 0 deletions RottingOranges.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// TimeComplexity: O(m*n)
// SpaceComplexity: O(m*n)
// Explanation : I first traverse the grid to count all fresh oranges and add the positions of rotten oranges to a queue. Then I perform a BFS level by level, where each level represents one minute of time.
// For every rotten orange, I try to rot its four neighboring cells using the direction array. Whenever a fresh orange becomes rotten,
// I add it to the queue and decrease the fresh count. After BFS finishes, if no fresh oranges remain, I return the total time taken; otherwise, I return -1.

class Solution {
public int orangesRotting(int[][] grid) {
int m = grid.length;
int n = grid[0].length;
int time =0;
int oneCount = 0;
Queue<int[]> q = new LinkedList<>();
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) {
oneCount++;
}
}
}
if(oneCount ==0) {
return 0;
}
int[][] dirs = {{0,-1}, {0,1}, {1,0}, {-1,0}};
while(!q.isEmpty()) {
int size = q.size();
for(int k=0; k<size; k++) {
int[] curr = q.poll();
for(int l =0 ; l<dirs.length; l++) {
int i = dirs[l][0]+curr[0];
int j = dirs[l][1]+curr[1];
if(i>=0 && j>=0 && i<m && j<n) {
if(grid[i][j] == 1){
grid[i][j] =2;
q.add(new int[]{i,j});
oneCount--;
}
}

}

}
time++;
}
if(oneCount ==0) {
return time-1;
}

return -1;

}
}