#Time : O(N) N -> no of employee#Space: O(N)"""# Definition for Employee.class Employee:def __init__(self, id: int, importance: int, subordinates: List[int]):self.id = idself.importance = importanceself.subordinates = subordinates"""class Solution:def buildMapping(self, employees):d = {}for emp in employees:d[emp.id] = empreturn ddef dfs(self, d, id):if id not in d:return 0s = d[id].importancefor subId in d[id].subordinates:s += self.dfs(d,subId)return sdef getImportance(self, employees: List['Employee'], id: int) -> int:d = self.buildMapping(employees)return self.dfs(d,id)
Please work on the problems at home and post your solutions to the thread. I'll be discussing the questions in a Zoom meeting next Saturday.
417 40.5% Medium
#Time : O(mn)#space: O(mn)class Solution:def __init__(self):self.p = set()self.a = set()def dfs(self, matrix, row, col, prev, visited):if row < 0 or col < 0:self.p = self.p | visitedreturnif row > len(matrix)-1 or col > len(matrix[0])-1:self.a = self.a | visitedreturnif (row,col) in visited:returnif prev >= matrix[row][col]:# if the paticular co-ord is already in any set we dont have to carry the dfs, we can just add all the paths to that set.if (row,col) in self.a and (row,col) in self.p:if (row,col) in self.a:self.a = self.a | visitedif (row,col) in self.p:self.p = self.p | visitedreturnvisited.add((row,col))prev = matrix[row][col]self.dfs(matrix,row-1,col,prev,visited)self.dfs(matrix,row,col-1,prev,visited)self.dfs(matrix,row+1,col,prev,visited)self.dfs(matrix,row,col+1,prev,visited)visited.remove((row,col))returndef pacificAtlantic(self, matrix: List[List[int]]) -> List[List[int]]:for row in range(len(matrix)):for col in range(len(matrix[0])):self.dfs(matrix,row,col,matrix[row][col],set())# at the end we have to take intersection of the atlantic and pacific sets.return [list(e) for e in self.p.intersection(self.a)]
--
Jan 2020 : 4 FAANG OFFERS. 2019: 15 FAANG OFFERS
https://www.techbayarea.us/
FB: https://www.facebook.com/techbayarea.us
Discord: https://discord.gg/yWMWptS
meetup : https://www.meetup.com/techbayarea/
google groups: https://groups.google.com/forum/#!forum/leetcode-meetup
---
You received this message because you are subscribed to the Google Groups "leetcode-meetup" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leetcode-meet...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/leetcode-meetup/d009c1c9-b546-4c13-84a3-01c7a1b5bb5ao%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/leetcode-meetup/CAM7qDyAa0zXnviM%2BdZAETte2NH2_4bQ-PmGmwyNhXOZdzBzC0w%40mail.gmail.com.
Please work on the problems at home and post your solutions to the thread. I'll be discussing the questions in a Zoom meeting next Saturday.
472 42.7% Hard
| 690 | Employee Importance | 56.6% | Easy |
| 417 | Pacific Atlantic Water Flow | 40.5% | Medium |
| 472 | Concatenated Words | 42.7% | Hard |