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
39 changes: 39 additions & 0 deletions EmployeeImportance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Time Complexity : O(n)
# Space Complexity : O(n)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No
# Approach : Map every employee ID to their object for easy access.
# Do BFS starting from the given employee, adding their importance.
# Keep exploring subordinates until all levels are visited and summed up.

"""
# Definition for Employee.
class Employee:
def __init__(self, id: int, importance: int, subordinates: List[int]):
self.id = id
self.importance = importance
self.subordinates = subordinates
"""

class Solution:
def getImportance(self, employees: List['Employee'], id: int) -> int:
id_emp_map = {}

for employee in employees:
id_emp_map[employee.id] = employee

q = deque()
q.append(id)
res = 0

while q:
currId = q.popleft()
currEmp = id_emp_map[currId]
res += currEmp.importance

for subId in currEmp.subordinates:
q.append(subId)

return res


47 changes: 47 additions & 0 deletions RottenOranges.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 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
# Approach : Collect all the rotten oranges and count how many fresh ones are there.
# Then, every minute, rot the nearby fresh ones using BFS until none are left.
# If we finish rotting all, return the time; else, return -1 since some can't be reached.

class Solution:
def orangesRotting(self, grid: List[List[int]]) -> int:
rows, cols, fresh = len(grid), len(grid[0]), 0
dirs = [[-1,0],[1,0],[0,1],[0,-1]]

q = deque()
for i in range(rows):
for j in range(cols):
if grid[i][j] == 2:
q.append((i,j))
if grid[i][j] == 1:
fresh += 1

time = 0
if fresh == 0:
return time

while q:
size = len(q)
time += 1
for i in range(size):
curr = q.popleft()
for dx, dy in dirs:
r = dx + curr[0]
c = dy + curr[1]

if r >= 0 and c >= 0 and r < rows and c < cols and grid[r][c] == 1:
grid[r][c] = 2
q.append((r, c))
fresh -= 1
if fresh == 0:
return time

return -1