Maximum Subarray Min-Product - Monotonic Increasing Stack - Leetcode 1856 - Python

  Рет қаралды 32,387

NeetCode

NeetCode

Күн бұрын

Пікірлер: 61
@NeetCode
@NeetCode 3 жыл бұрын
💡 SIMILAR PROBLEM: kzbin.info/www/bejne/sKmYhKpvZphjgpI
@hfeng1995
@hfeng1995 2 жыл бұрын
Leetcode has inflation issue. Used to be hard, now just medium :)
@halahmilksheikh
@halahmilksheikh 2 жыл бұрын
For anyone else wondering what the connection is between this problem and Largest Rectangle in Histogram. In the old problem, another way to think about it is "maximize the product of (the minimum value contained in a contiguous array) and (the length of the array)". We use the mono increasing stack to keep track of longest valid windows associated with each minimum value. Max MinProduct is an extension of Histogram so definitely understand Histo first if you're confused about MinProd.
@yichenyao5927
@yichenyao5927 Жыл бұрын
@@hfeng1995 for real! I feel like this is even trickier than the largest rectangle (which is Hard) by creating a prefix array. I got TLE for not creating that while the rest of the code following the exact structure of monotonic stack in largest rectangle (which I previously figured it out on my own)
@halahmilksheikh
@halahmilksheikh 2 жыл бұрын
These monotonic stack problems are more confusing than DP... at least with DP you can reason it out but here the logic is so confusing
@danielsun716
@danielsun716 2 жыл бұрын
My question is during the 30 mins interview, how does we decide to use DP or monotonic stack?
@tofahub
@tofahub 2 жыл бұрын
That’s what I came to say
@Alexander-zt9kz
@Alexander-zt9kz 8 ай бұрын
@@danielsun716really DP is for when you have a problem who can be solved by sub problems which are computed multiple times, so you cache them. Stack is used to emulate a callstack, reverse data, undo / check valid operations, or keeping items in monotonic order, either non decreasing or non increasing, sometimes with a twist. Harder monotonic stack questions are extremely difficult imo, much harder than DP or greedy questions.
@ziomalZparafii
@ziomalZparafii 2 жыл бұрын
I've solved dozen of leetcode and watched 3 times more of your solutions but this is the first one that I was unable to understand the problem and then also your explanation. Either it's too confused for me or I need to watch it again on some better day ;)
@alexeysubbota
@alexeysubbota 11 ай бұрын
You are not alone, me too!
@Alexander-zt9kz
@Alexander-zt9kz 9 ай бұрын
Stack problems are literally the hardest pattern on LC, much much harder than DP.
@dudeyouhavenoidea
@dudeyouhavenoidea 8 ай бұрын
I didn't get the explanation at first but I eventually did
@picturesqueedits3083
@picturesqueedits3083 2 ай бұрын
watch strivers video largest histogram, he hasn't explained well
@numberonep5404
@numberonep5404 2 жыл бұрын
This one definitely earned its unofficial "Hard" tag omg xD Crystal clear explanation as usual, thank you!
@mahesh_kok
@mahesh_kok 2 жыл бұрын
For people coming from largest rectangle in histogram below code explains the problem some modifications help avoid extra loops # this is my solution which is exactly the same as largest rectangle in histogram def maxSumMinProduct(nums) -> int: # add -1 to the end so that we can avoid extra while loop # for example when computing the last elemenet in nums # which is smallest and hence we will be computing the stack nums = nums + [-1] res = -1 stack = [] # adding 0 to the pre_comp_sum helps in calculating pre sum pre_comp_sum = [0] pre_comp_sum.extend(pre_comp_sum[-1] + n for n in nums) for index, ele in enumerate(nums): if stack and ele < stack[-1][1]: last_index = -1 while stack and stack[-1][1] >= ele: last_index, last_ele = stack.pop() new_res = last_ele * (pre_comp_sum[index] - pre_comp_sum[last_index]) res = max(res, new_res) stack.append((last_index, ele)) else: stack.append((index, ele)) return res
@oswald3042
@oswald3042 3 жыл бұрын
Intuition in one line: Use monotonic increasing stack to get the sorting order of elements without actually sorting the array.
@AymenDaoudi-u6h
@AymenDaoudi-u6h Жыл бұрын
Intuition ? really ?!
@nithinbosej
@nithinbosej 3 жыл бұрын
Could have been better if explained with a bit bigger array like - [3, 1, 6, 4, 5, 2]. So some of the not-so-intuitive scenarios show up. But indeed a great explanation! Thanks!
@brieflyfun
@brieflyfun Жыл бұрын
It seems that having duplicated elements with the same value in stack is not useful. It is save to pop it out and put it back as newStart set to the original index. So change the while line condition stack[-1][1] > n to stack[-1][1] >= n will still work. So the stack would be strictly increasing.
@RomanChurganov
@RomanChurganov Жыл бұрын
In a first place you should show on examples what we should include to sub-array as much numbers as we can to increase Min-Product, unless next number is smaller then smallest of sub-array. So smallest number acts like a baseline of a hill, and traversing with a stack while going down from hill it allows us to just keep track other side of the hill
@mosaic34
@mosaic34 3 жыл бұрын
hey neetcode ! whenever i get stuck in a problem i always search you up first , you have an amazing and simple explanation which is always on point. i am trying to solve leetcode problem number 321 create maximum number and i couldnt find simple explanation to it anywhere on the internet , i am still stuck .....can you please make your next video solving that problem
@NeetCode
@NeetCode 3 жыл бұрын
Thanks, yes I will solve that problem soon!
@jdhp3696
@jdhp3696 2 жыл бұрын
Thank you for the explanation. The last part of popping of the remaining stack is something that I did not think about.
@DavidDLee
@DavidDLee Жыл бұрын
Unlike most of your videos, the explanation here is not clear.
@zhangxinhello
@zhangxinhello Жыл бұрын
Actually, you should point out: the key to solve this problem is find the firstLeftMinValue and RightMinValue for index i, that's why we use the stack
@quentinau7507
@quentinau7507 2 жыл бұрын
Watched it 5 times, finally understand the algorithm.
@erdemtuna_me
@erdemtuna_me 2 жыл бұрын
IMHO, 1-indexing prefix array usage creates a big difference (from coding perspective) in this problem in compared to 0-indexing...
@shrimpo6416
@shrimpo6416 3 жыл бұрын
This question is very complicated, but perfect explanation and I finally understand it!
@sarveshchavan4391
@sarveshchavan4391 3 жыл бұрын
Really appreciate your efforts for explaining ! Need more of such explanation for Hard problems
@SRIDHARANBEE-jy9li
@SRIDHARANBEE-jy9li 3 жыл бұрын
Hii i just wanna thank you bro its literally amazing ❤️
@SaurabhJeena-sh4gh
@SaurabhJeena-sh4gh 25 күн бұрын
easiest question of leetcode class Solution { public int maxSumMinProduct(int[] nums) { long max=Long.MIN_VALUE; Stack stack=new Stack(); long [] prefixSum=new long [nums.length]; int [] ns=new int [nums.length]; int [] ps=new int [nums.length]; long sum=0; for(int i=0;i=0;i--){ while(!stack.isEmpty()&&nums[stack.peek()]>=nums[i]){ stack.pop(); } if(stack.isEmpty()){ ns[i]=nums.length; } else{ ns[i]=stack.peek(); } stack.push(i); } stack.clear(); for(int i=0;inums[i]){ stack.pop(); } if(stack.isEmpty()){ ps[i]=-1; } else{ ps[i]=stack.peek(); } stack.push(i); } for(int i=0;i
@s_rox5042
@s_rox5042 7 ай бұрын
sliding window does not work here since we cannot decide to go left or right if the num[let_pos] == nums[right_pos]
@abhisheksaraf2616
@abhisheksaraf2616 8 ай бұрын
this problem is similar to Largest Rectangle in Histogram there we are multiplying by index, here sum within range
@sachinpaul2111
@sachinpaul2111 Жыл бұрын
This is the exact same pattern as that histogram rectangle problem
@VerzapierZyGaming
@VerzapierZyGaming 3 жыл бұрын
hey, what happens if the array is 1,2,3,1 we would still get a 2, that starts from idx 1 in the stack right? but the minimum is 1 at the idx 3?
@NeetCode
@NeetCode 3 жыл бұрын
Before adding the 1, we would have to pop 3 and 2 from the stack. Once we pop those, we would compute if either 2 or 3 leads us to the max result.
@VerzapierZyGaming
@VerzapierZyGaming 3 жыл бұрын
@@NeetCode oh so we keep poping if its smaller than the peak of stack, got it thats where i had been mssinh great work!
@haoyuwang3243
@haoyuwang3243 2 жыл бұрын
I feel sad coz I still don't quite get it tho this is the clearest explanation on the planet of earth..This damn question..
@amannegi8256
@amannegi8256 3 жыл бұрын
amazing mate!
@pranee31
@pranee31 Жыл бұрын
Neatly done
@Sulerhy
@Sulerhy 5 ай бұрын
So confusing. Anyways thank you for your explain, it helps me step by step
@liftandshiftdev3222
@liftandshiftdev3222 Жыл бұрын
i solved it using segment tree
@SunilJamkatelTrue
@SunilJamkatelTrue 2 жыл бұрын
Isn't this a O(n^2) solution with the while nested loop?
@vinayakgandhi5690
@vinayakgandhi5690 2 жыл бұрын
It is O(N) Amortized. It looks like O(N^2) as we run a while loop in each iteration, but the total run time of the while loop over all n iteration is only N because we pop an element at most once.
@vishnups
@vishnups Жыл бұрын
Each element will be added to the stack exactly once. You can think of it as iterating over the input list once and over the elements of the stack once, making it two linear passes.
@pramod3293
@pramod3293 3 жыл бұрын
What if the numbers can be negative?
@avanishgvyas1992
@avanishgvyas1992 3 ай бұрын
One feedback for the solution videos, not just this one but in general for all video solutions: It would help tremendously if you could show animation of the running algorithm to explain how it works.
@yassineacherkouk
@yassineacherkouk Жыл бұрын
this is not something that you can come up with alone.
@naveen3192
@naveen3192 2 жыл бұрын
I can understand the steps in the problem and how it's solved but I don't quite get how these steps lead to generating all sub arrays. I don't get that connection. Can somebody help?
@sodiqyusuff8019
@sodiqyusuff8019 2 жыл бұрын
Looks like you haven't really understood the steps. So basically, you don't need to generate ALL sub arrays. Since you're multiplying all subarrays by the MIN val in the sub, that automatically means some sub arrays will be useless. Consider the array [1,2,5,1,0], and consider the element 2 at index 1. 2 belongs to sub arrays like [2,5], [2,5,1] and [2,5,1,0] but only [2,5] is useful to 2 because that's the only one for which 2 is the minimum. You will never multiply 2 by the sum of the rest so it's pointless to generate them. The popping of the stack when you encounter a smaller number helps to ensure this. I hope my explanation helps a little.
@naveen3192
@naveen3192 2 жыл бұрын
@@sodiqyusuff8019 Hey, thanks for that. Rewatching the video now. I get why we don't need all sub-arrays. Thanks
@jianhaoluo9491
@jianhaoluo9491 3 жыл бұрын
Solid!
@piyushyadav9006
@piyushyadav9006 3 жыл бұрын
I solved this using recursion .. Basically first I search for the min index and then I divided given array into two subarray to check whether they could give me the max without the min element. But in this method I've to sum all the subarrays again and again and thus time complexity is similar to that of QuickSort which is O(nlogn) Could there be any optimisation done.
@chaoluncai4300
@chaoluncai4300 2 жыл бұрын
Your way is actually Divide&Conquer, gj! could use a prefix sum to conquer the repetitive sum, but again has to compute the min of subarrays again and again... btw for breakin ties do you just skip all the candidates that's equals to the current minimum candidate?
@jonaskhanwald566
@jonaskhanwald566 3 жыл бұрын
Mission successfully faile
@jonaskhanwald566
@jonaskhanwald566 3 жыл бұрын
Need Combination sum IV - #377
@shadowsw8020
@shadowsw8020 3 ай бұрын
Badly explained
@sparrow2068
@sparrow2068 2 күн бұрын
fr
@Philgob
@Philgob 3 ай бұрын
this ain't no fucking medium
Edit Distance - Dynamic Programming - Leetcode 72 - Python
21:00
Maximum Frequency Stack - Leetcode 895 - Python
13:21
NeetCode
Рет қаралды 28 М.
Noodles Eating Challenge, So Magical! So Much Fun#Funnyfamily #Partygames #Funny
00:33
Yay😃 Let's make a Cute Handbag for me 👜 #diycrafts #shorts
00:33
LearnToon - Learn & Play
Рет қаралды 117 МЛН
FOREVER BUNNY
00:14
Natan por Aí
Рет қаралды 31 МЛН
Maximum Product Subarray - Dynamic Programming - Leetcode 152
15:31
Daily Temperatures - Monotonic Stack - Leetcode 739 - Python
11:52
Subarray Sum Equals K - Prefix Sums - Leetcode 560 - Python
15:19
Redundant Connection - Union Find - Leetcode 684 - Python
16:04
Big-O Notation - For Coding Interviews
20:38
NeetCode
Рет қаралды 518 М.
Do NOT Learn Kubernetes Without Knowing These Concepts...
13:01
Travis Media
Рет қаралды 325 М.
N-Queens - Backtracking - Leetcode 51 - Python
17:51
NeetCode
Рет қаралды 180 М.
Gas Station - Greedy - Leetcode 134 - Python
15:47
NeetCode
Рет қаралды 142 М.