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
26 changes: 26 additions & 0 deletions EmployeeImportance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

class Solution {
// TimeComplexity: O(N)
// SpaceComplexity: O(N)
// Ran succesfully on Leetcode: yes
public int getImportance(List<Employee> employees, int id) {
int total = 0 ;
HashMap<Integer, Employee> map = new HashMap<>();
Queue<Employee> 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));
}
}
return total;
}
}
51 changes: 51 additions & 0 deletions RottingOranges.java
Original file line number Diff line number Diff line change
@@ -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<int[]> 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;
}
}