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
57 changes: 57 additions & 0 deletions EmployeeImportanceDFS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Time Complexity : O(n) where n is the number of employees in the list
// Space Complexity : O(n) where n is the number of employees in the list
// 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
// We can use a depth first search approach to solve this problem.
// We can start from the given employee id and then we can add the importance of the employee to the result and then we can call the dfs function for all the subordinates of the employee.
// We can keep track of the importance of the employees in a map and then we can return the result at the end.
// We can also use a breadth first search approach to solve this problem. We can use a queue to keep track of the employees and then we can add the importance of the employees to the result and then we can add the subordinates of the employees to the queue. We can keep track of the importance of the employees in a map and then we can return the result at the end. DFS could also be used to solve this problem.


// Your code here along with comments explaining your approach
public import java.util.*;

import java.util.HashMap;


// Definition for Employee.
class Employee {
public int id;
public int importance;
public List<Integer> subordinates;
};


class Solution {

Map<Integer, Employee> map;

int result;

public int getImportance(List<Employee> employees, int id) {

map = new HashMap<>();

result = 0;

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

dfs(id);

return result;
}

private void dfs(int id) {

Employee emp = map.get(id);

result = result + emp.importance;

for (int ids : emp.subordinates) {
dfs(ids);
}
}
}
85 changes: 85 additions & 0 deletions RottenOrangeBFS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Time Complexity : O(m*n) where m is the number of rows and n is the number of columns in the grid
// Space Complexity : O(m*n) where m is the number of rows and n is the number of columns in the grid
// 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
// We can use a breadth first search approach to solve this problem.
// We can start from all the rotten oranges and then we can rot the adjacent fresh oranges.
// We can keep track of the time taken to rot all the oranges and return the time taken at the end.
// If there are still fresh oranges left, we can return -1.
// We can use a queue to keep track of the rotten oranges and a variable to keep track of the time taken to rot all the oranges.
// We can also keep track of the number of fresh oranges and decrement it every time we rot a fresh orange.
// DFS could also be used to solve this problem.

// Your code here along with comments explaining your approach
import java.util.*;

class Solution {

public int orangesRotting(int[][] grid) {

int[][] directions = new int[][]
{
{-1, 0},
{1, 0},
{0, -1},
{0, 1}
};

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

int m = grid.length;
int 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) {
que.offer(new int[] { i, j });
}
else if (grid[i][j] == 1) {
fresh++;
}
}
}

if (fresh == 0) return 0;

int time = 0;

while (!que.isEmpty() && fresh > 0) {

int size = que.size();

for (int i = 0; i < size; i++) {

int[] curr = que.poll();

int row = curr[0];
int col = curr[1];

for (int[] dir : directions) {

int nr = row + dir[0];
int nc = col + dir[1];

if (nr >= 0 && nr < m &&
nc >= 0 && nc < n &&
grid[nr][nc] == 1) {

grid[nr][nc] = 2;
fresh--;
que.offer(new int[] { nr, nc });
}
}
}

time++;
}

return fresh == 0 ? time : -1;
}
}