Online Stock Span - Leetcode 901 - Python

  Рет қаралды 35,349

NeetCode

NeetCode

Күн бұрын

Пікірлер: 55
@NeetCode
@NeetCode 3 жыл бұрын
Sorry the video cuts off before the code is complete, here's the full code: github.com/neetcode-gh/leetcode/blob/main/python/901-Online-Stock-Span.py
@karanssh
@karanssh 3 жыл бұрын
FYI, this link is 404 now, files got moved
@Neo-gy1bv
@Neo-gy1bv Жыл бұрын
WTF after spending 15 min i am stuck with half code
@Neo-gy1bv
@Neo-gy1bv Жыл бұрын
@@karanssh WTF after spending 15 min i am stuck with half code
@NamsInUsa
@NamsInUsa Жыл бұрын
class StockSpanner: def __init__(self): self.stack = [] def next(self, price: int) -> int: span = 1 while self.stack and self.stack[-1][0]
@KaushikkrSarma
@KaushikkrSarma 7 ай бұрын
@@Neo-gy1bv class StockSpanner: def __init__(self): self.stack = [] # [price,span] def next(self, price: int) -> int: span = 1 while self.stack and self.stack[-1][0]
@lettershere
@lettershere 3 жыл бұрын
Got this exact question, with a small twist, at an interview less than a month ago, did a very similar approach to get linear solution and got the job.
@artieschmidt3039
@artieschmidt3039 3 жыл бұрын
lets goooo mario!! What job did you get?
@ashutoshjha2648
@ashutoshjha2648 7 ай бұрын
Lucky broo
@qj3690
@qj3690 2 жыл бұрын
class StockSpanner: def __init__(self): self.stack = [] def next(self, price: int) -> int: span = 1 while self.stack and price >= self.stack[-1][0]: span += self.stack[-1][1] self.stack.pop() self.stack.append([price, span]) return self.stack[-1][1] Thank you neetcode
@whatuphere
@whatuphere Жыл бұрын
correction-- while self.stack and price > self.stack[-1][0]:
@calebcrf
@calebcrf 3 ай бұрын
Solution is incorrect
@littletiger1228
@littletiger1228 2 ай бұрын
@@calebcrf ??
@bharathkishore487
@bharathkishore487 Ай бұрын
Your return statement is wrong
@estifanosbireda1892
@estifanosbireda1892 2 жыл бұрын
5 minutes in and I understand what u mean, u make such a good videos. I even sometimes watch your solutions for questions I already did.
@thecommondude
@thecommondude 3 жыл бұрын
Really ingenious solution! Didn't expect this to be solved via a stack!
@thanirmalai
@thanirmalai 2 жыл бұрын
Thank you very much. I finally understood monotonic stack. Neetcode is a true legend
@michelleyang1881
@michelleyang1881 3 жыл бұрын
Took me a while to understand why the time complexity is O(n). From my understanding, for each time we push a new [price, span] pair, the worst case time complexity should be O(n). However, if we consider pushing all the prices, the overall time complexity is still O(n). Because we only gonna push each pair once + pop each pair AT MOST ONCE. Great explanation!
@dadisuperman3472
@dadisuperman3472 3 жыл бұрын
That's not a linear time complexity. Read my comment to understand why.
@Moch117
@Moch117 Жыл бұрын
Pushing to a stack is O(1) We go through the entire elements in 1 pass, which is why its O(n)
@dollyvishwakarma2
@dollyvishwakarma2 2 жыл бұрын
@NeetCode Please make a videos on monotonic increasing/decreasing sequences and their patterns in questions so that we can identify problems that will need a stack.
@ken470
@ken470 2 жыл бұрын
Nice approach ♥️ I thought of something(without using pair)what if we use a count variable that is set to 1 then we push an element in stack and if the stack was empty we push 1 to our ans array. If the top of stack has larger element than incoming element then we will also push 1 since there will be no consecutives. If the top has element smaller than our incoming element than we pop until larger element is found and increment the count and push this count to ans array now for the next element if it will be larger than previous element than then can use previous count and for the rest element we can pop again and add to count and push to ans this way we don't have to use pair..
@shamsularefinsajib7778
@shamsularefinsajib7778 Жыл бұрын
Amazing explanation, hats off!!
@subramaniannk4255
@subramaniannk4255 Жыл бұрын
This is the best explanation, thanks
@comble999
@comble999 3 жыл бұрын
Got a similar question at my phone interview last week, hopefully I came out the solution and pass it.
@sirmidor
@sirmidor 2 жыл бұрын
The video ends for me at 15:18, cutting off before the code is completed.
@NeetCode
@NeetCode 2 жыл бұрын
Sorry about that, here's the full code: github.com/neetcode-gh/leetcode/blob/main/python/901-Online-Stock-Span.py
@janardannn
@janardannn 8 ай бұрын
i had somehwhat like this in mind but didnt really drew it out, so was kind of hazy then i thought maybe its not feasible, so watching NC now
@ygwg6145
@ygwg6145 Жыл бұрын
Maybe it is easier to push price/index pair to stack. Then span=currIndex-prevIndex.
@auroshisray9140
@auroshisray9140 2 жыл бұрын
Great video thanks!!
@chaitu2037
@chaitu2037 3 жыл бұрын
As usual, amazing!
@Spedfree
@Spedfree 3 жыл бұрын
Which program is he using to write on his screen like so?
@NeetCode
@NeetCode 3 жыл бұрын
I use Paint3D
@Liv3fast
@Liv3fast 3 жыл бұрын
If it weren’t for the design implementation of the problem, couldn’t we just use a DP array?
@Liv3fast
@Liv3fast 3 жыл бұрын
Actually was able to still implement it, but I guess the stack is still better space complexity.
@girirajrdx7277
@girirajrdx7277 2 жыл бұрын
@@Liv3fast class Node: def __init__(self,index,span): self.index=index self.span=span def stockSpan(price, n=0) : #Your code goes here if len(price)==0: return [] highest=Node(0,1) lenn=len(price) span_data=[0]*lenn span_data[0]=1 i=1 while iprice[j]: count=count+highest.span highest.index=i highest.span=count span_data[i]=count break count=count+1 j=j-1 span_data[i]=count i=i+1 return span_data you can try this.....
@minhthinhhuynhle9103
@minhthinhhuynhle9103 2 жыл бұрын
11:19 What happened if the number is 65 instead ???
@user-jo1zj1vg4v
@user-jo1zj1vg4v 2 жыл бұрын
It would still be 1 because the problem states that we are looking for (consecutive) days since 70 is greater than 65.
@__________________________6910
@__________________________6910 3 жыл бұрын
You are great
@paulo25740
@paulo25740 3 жыл бұрын
ha! what a neat solution
@Nick-uo2bi
@Nick-uo2bi 3 жыл бұрын
Please introduce DSA and DP playlist for concept. It will help alot :)
@GauravKumar-sr6bt
@GauravKumar-sr6bt 2 жыл бұрын
@NeetCode Even though each element will be added and removed at Max one time, but the comparisons we had to make to compute span of a particular position can be more than 1. So time complexity would be O(n*n). Detailed explanation: what we are basically doing is summing up the spans of previous peeks, provided previous peeks are smaller than the current value for which we are computing span. E.g. if data is [1,5,10,2,4,9,4,5,8,13], then to compute the span of 13, we will have to sum up spans of previous peaks smaller than 13, i.e. span(8) + span(9) + span(10).
@jugsma6676
@jugsma6676 3 жыл бұрын
this is my solution, is this fine? def stock_spanner(stock: List) -> List: stack = [] for i, v in enumerate(stock): if i == 0: stack.append(1) else: prev = stock[i-1] jump = 1 while v > prev and jump > 0: jump += stack[i - jump] prev = stock[i-jump] stack.append(jump) return stack
@b_2818
@b_2818 2 жыл бұрын
right?
@dadisuperman3472
@dadisuperman3472 3 жыл бұрын
That's not a linear solution time wise. By adding the stack you worsened the space complexity from O(1) to O(n), and without changing the time complexity which is in worse case scenario O(n2). You have just to compare the number of comparison you did in the stack solution against the jump solution. No difference! A list of spans is also a stack, but instead of popping the values you just jump the index using the spans. 🤷🏻‍♂️ Take this expls [40,30,20,10,90,80,70,60,50,100] [90, 80, 70, 60, 50, 40, 30, 20, 10, 100]
@jamesmandla1192
@jamesmandla1192 3 жыл бұрын
the number of comparisons should be the same regardless of whether you pop from the stack or simply jump indexes so I guess you're correct about space complexity. However in the examples you gave , the time complexity was still O(2n) which was the worst case he mentioned. For example, in the second solution you only have to start checking the elements beforehand once you reach 100 because all the immediate previous adjacent elements for the previous values are larger which means we can immediate move on and don't have to check the span.
@dadisuperman3472
@dadisuperman3472 3 жыл бұрын
@@jamesmandla1192 number of comparisons is the time complexity. 😏
@jamesmandla1192
@jamesmandla1192 3 жыл бұрын
@@dadisuperman3472 right, I just realised haha. But I think space complexity would be O(n) regardless of whether you used a stack because you would need to create an array to store the output right?
@dadisuperman3472
@dadisuperman3472 3 жыл бұрын
@@jamesmandla1192 nope
@shuhaoliang144
@shuhaoliang144 Жыл бұрын
Amortized time is O(1) for each call
@yashbabuaa
@yashbabuaa Ай бұрын
class StockSpanner: def __init__(self): self.st = [] # pair: (price, span) def next(self, price: int) -> int: span = 1 while self.st and self.st[-1][0]
@yashbabuaa
@yashbabuaa Ай бұрын
we can also return span
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 836 М.
Wiggle Sort - Leetcode 280 - Python
11:00
NeetCode
Рет қаралды 24 М.
小丑女COCO的审判。#天使 #小丑 #超人不会飞
00:53
超人不会飞
Рет қаралды 16 МЛН
Cat mode and a glass of water #family #humor #fun
00:22
Kotiki_Z
Рет қаралды 42 МЛН
L15. Stock Span Problem | Stack and Queue Playlist
19:21
take U forward
Рет қаралды 38 М.
What P vs NP is actually about
17:58
Polylog
Рет қаралды 149 М.
Dependency Injection, The Best Pattern
13:16
CodeAesthetic
Рет қаралды 918 М.
Remove K Digits - Leetcode 402 - Python
14:36
NeetCode
Рет қаралды 66 М.
Big-O Notation - For Coding Interviews
20:38
NeetCode
Рет қаралды 566 М.
This Algorithm is 1,606,240% FASTER
13:31
ThePrimeagen
Рет қаралды 864 М.
8 patterns to solve 80% Leetcode problems
7:30
Sahil & Sarra
Рет қаралды 517 М.
Stock span problem
14:39
Techdose
Рет қаралды 50 М.
小丑女COCO的审判。#天使 #小丑 #超人不会飞
00:53
超人不会飞
Рет қаралды 16 МЛН