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

// BFS O(n) time, O(n) space
class Solution {
public int getImportance(List<Employee> employees, int id) {
Map<Integer, Employee> map = new HashMap<>();

for (Employee emp : employees) {
map.put(id, emp);
}

int ans = 0;

Queue<Employee> q = new LinkedList<>();
q.offer(map.get(id));

while (!q.isEmpty()) {
Employee curr = q.poll();

ans += curr.importance;

for (int subId : curr.subordinates) {
q.offer(map.get(subId));
}
}
return ans;
}
}


// Definition for Employee.
class Employee {
public int id;
public int importance;
public List<Integer> subordinates;
};
49 changes: 49 additions & 0 deletions RottingOranges.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import java.util.*;

// BFS O(m * n) time, O(m * n) space
class Solution {
public int orangesRotting(int[][] grid) {
int n = grid.length;
int m = grid[0].length;
int[][] dirs = new int[][] {
{0,1}, {1,0}, {-1,0}, {0,-1}
};

Queue<int[]> q = new LinkedList<>();

int freshCount = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] == 2) {
q.offer(new int[] {i, j});
}
else if (grid[i][j] == 1) {
freshCount++;
}
}
}

int time = 0;
while (!q.isEmpty()) {
int size = q.size();

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 < n && c < n && grid[r][c] == 1) {
q.offer(new int[] {r, c});
grid[r][c] = 2; // mark rotten
freshCount--;
}
}
}
time++; // increment time after each level
}
if (freshCount == 0) return time-1;

return -1;
}
}