NESTED LIST WEIGHT SUM | LEETCODE # 339 | PYTHON BFS SOLUTION

  Рет қаралды 9,076

Cracking FAANG

Cracking FAANG

Күн бұрын

Пікірлер
@aaditya_87
@aaditya_87 6 ай бұрын
when r u getting outta da hood , theres always a siren noise😂😂
@worldrecordegg-g7y
@worldrecordegg-g7y 3 ай бұрын
meta nyc
@kapilrules
@kapilrules 5 ай бұрын
extend is key here. thank you very much @Cracking FAANG, You are super awesome !!
@manoelramon8283
@manoelramon8283 4 ай бұрын
simpler version: from collections import deque class Solution: def depthSum(self, nestedList: List[NestedInteger]) -> int: """ :type nestedList: List[NestedInteger] :rtype: int """ # element and level (1 for all of theem at the beginning) q = deque([e , 1] for e in nestedList) level = 1 total = 0 while q: e,level = q.popleft() if e.isInteger(): #integer total += e.getInteger() * level else: #list lst = e.getList() for list_item in lst: #extract the numbers and increase the level q.append([list_item, level + 1]) return total
@devdanlight
@devdanlight 3 ай бұрын
Hey 0ms BEATS 100% recurisve solution: class Solution: def depthSum(self, nestedList: List[NestedInteger]) -> int: def calculate_sum(nested, depth): total = 0 for ni in nested: if ni.isInteger(): # If it's an integer, multiply it by its depth and add to total total += ni.getInteger() * depth else: # If it's a list, recursively calculate the sum for the nested list total += calculate_sum(ni.getList(), depth + 1) return total # Start the recursive calculation from depth 1 return calculate_sum(nestedList, 1)
@Voidwanderer571
@Voidwanderer571 11 ай бұрын
4:50 all the 2*2 you wrote should be 2*1
@Spyro-kt8gy
@Spyro-kt8gy 8 ай бұрын
Which data structure did they use to represent the nested list in C++? I can't see it since the question is Premium.
@sarayarmohammadi3376
@sarayarmohammadi3376 3 ай бұрын
Thank you
@cc-to2jn
@cc-to2jn 3 ай бұрын
never knew u could do this using bfs, honestly much easier lol
@omi_naik
@omi_naik 2 ай бұрын
DFS Solution class Solution: def __init__(self): self.suml=0 def depthSum(self, nestedList: List[NestedInteger]) -> int: # Start DFS with depth 1 self.helperdfs(nestedList, 1) return self.suml def helperdfs(self, nestedList, depth): #print(nestedList) for i in nestedList: if i.isInteger(): # Add integer value multiplied by depth self.suml += i.getInteger() * depth else: # Recur for the nested list with increased depth self.helperdfs(i.getList(), depth + 1)
@ye7841
@ye7841 2 жыл бұрын
it's really helpful, thank you!
@crackfaang
@crackfaang 2 жыл бұрын
No problem! Make sure to subscribe and let me know if there’s any videos you’d like to see
@jianingli7542
@jianingli7542 Жыл бұрын
Why is time complexity O(n). We need to visit n element in the nestedList and each element could have up to n level depth, wouldn't that gives us O(n^2) for time complexity?
@crackfaang
@crackfaang Жыл бұрын
N = the total number of distinct items in the nested list provided. list = [1, 2, 3, 4, 5, 6, 7, 8, 9] has the same number of elements as [1, [2, [3, 4, [ 5, 6, 7, 8, [9]]]]]]. You are still processing a total of 9 numbers. It would be N^2 if at each level you then needed to process the array again somehow
@pat777b
@pat777b 8 ай бұрын
@@crackfaang I think another way to explain it is if you look at the parameters of the problem, you notice that depth has an upper bound of 50. Thus, the depth factor can be thought of as a constant multiplier and should not be considered when counting time complexity. of course, the total number of integers can far exceed 50 with these constraints.
@a2m2tkyo
@a2m2tkyo 5 ай бұрын
7:18 same
@kingskull619
@kingskull619 9 ай бұрын
why not recursive?
@pat777b
@pat777b 8 ай бұрын
he said he wants to avoid stack overflow. imo, the leetcode's problem parameters state that the maximum depth is 50 so there's no need to worry about stackoverflow. however, do note that the interviewer could alter the leetcode question. if the interviewer makes the maximum depth much bigger after a clarifying question, you should do the breadth first search solution to avoid stackoverflow. but if the interviewer keeps the maximum depth low, i'd go with a recursive dfs. imo, it's good to have both solutions in mind and choose the one that's most appropriate to what the interviewer wants.
RACE CAR | LEETCODE # 818 | PYTHON BFS SOLUTION
15:40
Cracking FAANG
Рет қаралды 12 М.
AI Is Making You An Illiterate Programmer
27:22
ThePrimeTime
Рет қаралды 282 М.
Мясо вегана? 🧐 @Whatthefshow
01:01
История одного вокалиста
Рет қаралды 7 МЛН
one year of studying (it was a mistake)
12:51
Jeffrey Codes
Рет қаралды 318 М.
LOWEST COMMON ANCESTOR OF A BINARY TREE III [PYTHON]
16:38
Cracking FAANG
Рет қаралды 15 М.
I Solved 100 LeetCode Problems
13:11
Green Code
Рет қаралды 345 М.
Pongfinity vs. World's Best Team
13:06
Pongfinity
Рет қаралды 18 М.
339. Nested List Weight Sum #leetcode
6:23
CodeWonka : Get started with programming
Рет қаралды 247
Dependency Injection, The Best Pattern
13:16
CodeAesthetic
Рет қаралды 917 М.
Premature Optimization
12:39
CodeAesthetic
Рет қаралды 855 М.
Meta Coding Question - Random Pick With Weight (LeetCode 528)
14:41
AlgosWithMichael
Рет қаралды 4,5 М.