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
49 changes: 49 additions & 0 deletions Problem1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#Rotting Oranges

#Time complexity -> O(mxn)
#Space complexity -> O(mxn)
from collections import deque
class Solution:
def orangesRotting(self, grid: List[List[int]]) -> int:
#Find all the rotten oranges and it's count, meanwhile also count total oranges in the matrix
# now during each loop rott the neighbors of the rotten oranges and increment the rotten count
# in the queue before the loop and also keep adding
# new rotted oranges. the number of loops required is the total minutes that will be needed to rot all possible oranges
# in the end if rotten is less then total oranges not all can be rotten

rottenOranges = deque()
totalOranges = 0
rottenOrangesCount = 0
dir = [(-1,0),(0,1),(1,0),(0,-1)]
for i in range(0,len(grid)):
for j in range(0,len(grid[0])):
val = grid[i][j]
if val == 0:
continue
if val == 2:
rottenOranges.append((i,j))
rottenOrangesCount+=1
totalOranges+=1
resultMin = 0

while rottenOranges:
orangesToRotInCurrentIteration = len(rottenOranges)
for _ in range(0, orangesToRotInCurrentIteration):
r,c = rottenOranges.popleft()
for i,j in dir:
if i+r >= 0 and i+r < len(grid) and j+c >= 0 and j+c < len(grid[0]):
if grid[i+r][j+c] == 0:
continue
if grid[i+r][j+c] == 1:
grid[i+r][j+c] = 2
rottenOrangesCount+=1
rottenOranges.append((i+r,j+c))
if len(rottenOranges) > 0:
resultMin+=1

if rottenOrangesCount<totalOranges:
return -1
return resultMin



40 changes: 40 additions & 0 deletions Problem2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#Employee Importance

# Timr -> On
# Space -> On

# Definition for Employee.
from typing import List
from collections import deque

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:
#find the employee node 1st whose total we want to return.
# then calculate the sum of all importances of all it's child nodes

employeeSubordinateMap = {}
employeeImportanceMap = {}

for employee in employees:
employeeSubordinateMap[employee.id] = employee.subordinates
employeeImportanceMap[employee.id] = employee


empoylyesToConsider = deque()
empoylyesToConsider.append(id)
importance = 0
while empoylyesToConsider:
empId = empoylyesToConsider.popleft()
emp = employeeImportanceMap[empId]
importance += emp.importance
for subIds in emp.subordinates:
empoylyesToConsider.append(subIds)


return importance