Next Greater Element 1 (LeetCode 496) | Full solution with examples and animation | Study Algorithms

  Рет қаралды 30,183

Nikhil Lohia

Nikhil Lohia

Күн бұрын

Пікірлер: 75
@NerchukoMava
@NerchukoMava 6 ай бұрын
Underrated channel, sir please make more videos ,you have the potential to be on top.
@nikoo28
@nikoo28 6 ай бұрын
I am adding one new video every week 😄
@NerchukoMava
@NerchukoMava 6 ай бұрын
@@nikoo28 I am so glad for your reply sir😇
@NerchukoMava
@NerchukoMava 6 ай бұрын
Sir please do videos on Recursion as soon as possible, which will be so helpful for many students
@TufanPandu
@TufanPandu Жыл бұрын
After watching my expensive course i come to watch this video and it's OSM!
@nikoo28
@nikoo28 Жыл бұрын
Amazing!
@surajsidar3280
@surajsidar3280 5 ай бұрын
Which course you took and what was the cost? Please tell so that I won't take that course
@bonafontciel
@bonafontciel Жыл бұрын
I was able to type my own code just with your explanation. Thanks!
@nikoo28
@nikoo28 Жыл бұрын
Great job!
@varsshhaa
@varsshhaa 6 күн бұрын
omg!! I am happy that I have found this channel !! explanation is simply great !! Looking forward to more such videos !!
@Hayat26474
@Hayat26474 11 ай бұрын
i never thought this could be that simple , thanku sir
@dharmeshkase6003
@dharmeshkase6003 8 ай бұрын
the way you explain everything is just
@ranjansingh5414
@ranjansingh5414 Жыл бұрын
second if statement will not give correct output for your case this will give correct output, since you need to peek and add to greater element stack then push the element if(element < stack.peek()){ result[i] = stack.peek(); stack.push(element); continue; }
@nikoo28
@nikoo28 Жыл бұрын
if you check the code available in the video description, that works perfectly and passes on LeetCode.
@kireetmishra1671
@kireetmishra1671 9 ай бұрын
yeah bro you're correct, the sequence in the video is wrong, if the element is pushed before it will give the top element i.e. the same element from the array
@lakcai4063
@lakcai4063 Жыл бұрын
Great explanation! Clear and intuitive!
@a3rdtierguy864
@a3rdtierguy864 6 ай бұрын
UPDATED SOLUTION(LEETCODE SOLUTION) JUST USE ONLY MAP vector nextGreaterElement(vector& nums1, vector& nums2) { unordered_mapm; stacks; int n=nums2.size(); m[nums2[n-1]]=-1; s.push(nums2[n-1]); for(int i=nums2.size()-2;i>=0;i--) { if(s.size()>0&&s.top()>nums2[i]) { m[nums2[i]]=s.top(); s.push(nums2[i]); continue; } while(s.size()>0&&s.top()
@pietech1694
@pietech1694 5 ай бұрын
while forming a new array with stack store original array element index into map then traverse num2 and get the index of elements from hash and look the value at that index in fresh Array
@tanishktripathi8773
@tanishktripathi8773 10 ай бұрын
This is such a nice explanation, Here is the Java simplified solution class Solution { public int[] nextGreaterElement(int[] nums1, int[] nums2) { HashMap map = new HashMap(); Stack stack = new Stack(); for(int i=nums2.length-1; i>=0; i--) { while(!stack.isEmpty() && stack.peek()0) { map.put(nums2[i], stack.peek()); } else { map.put(nums2[i], -1); } stack.push(nums2[i]); } int[]res = new int[nums1.length]; for(int i =0; i
@prasadm8441
@prasadm8441 Жыл бұрын
Great explanation. Thank you so much Nikhil
@srikrishna_ss8263
@srikrishna_ss8263 2 жыл бұрын
Your Way Of Explanation Simply Awesome 🤗
@sahithd3125
@sahithd3125 29 күн бұрын
loved the way you explain the problem!
@andretheruler714
@andretheruler714 6 ай бұрын
thank you, i was a bit confused until you explained it
@pradheeshkumar-v4p
@pradheeshkumar-v4p 5 ай бұрын
That was a crystal clear explanation !!
@vilakshan.s
@vilakshan.s 6 ай бұрын
I think time complexity will still be O(n^2) where in worst case we will end up emptying the whole stack in order to find next greater element for ith position
@sammedpatil3907
@sammedpatil3907 2 жыл бұрын
what will be space and time space complexity if we are using stack and Map??
@MuhammadFahreza
@MuhammadFahreza 3 ай бұрын
in 13:40. Why the time complexity is O(n) ? Should we also count, the effort to traverse down the stack for each value ?
@nikoo28
@nikoo28 3 ай бұрын
you do not pop the entire stack each time. the max pops you will do is (n). So total time complexity is still O(2n) which equates to O(n)
@mohammedshahed5051
@mohammedshahed5051 8 ай бұрын
Okay, in the "optimized solution" as well we have an inner loop to remove all the elements which are lesser in that so even here we have a loop with in a loop, how does the time complexity o(n2)(brute force) != o(n2) (optimized) i guess it is inevitable, so best case o(n) worst case o(n2)
@mdshafiuddin1234
@mdshafiuddin1234 Жыл бұрын
amazing explanation!
@kemarjordan628
@kemarjordan628 5 күн бұрын
Brilliant man!
@Rocky-me7qh
@Rocky-me7qh 2 ай бұрын
Array is actually filled with distinct numbers. Next greater number of 4 is 12 and 6 what's the right answer? In the leetcode description itself is written, given arrays are distinct
@mohinischannel9665
@mohinischannel9665 2 жыл бұрын
very nice explaination ..clear.
@hilalkocak_
@hilalkocak_ 8 ай бұрын
Best explanation ever!
@nikoo28
@nikoo28 7 ай бұрын
Glad it was helpful!
@deepakkumar-sk4kj
@deepakkumar-sk4kj 3 күн бұрын
Please do Solve Leetcode 503 Next Greater Element II
@Elkhaligy
@Elkhaligy 5 ай бұрын
Good one
@stanleyjekwu6810
@stanleyjekwu6810 2 жыл бұрын
Thank you so much, very clear
@sumitsanu9524
@sumitsanu9524 11 ай бұрын
Same example as striver
@vinodpaluvuri54
@vinodpaluvuri54 Жыл бұрын
we need to compare nums1 with nums2..where are we doing that
@nikoo28
@nikoo28 Жыл бұрын
nums1 is a subset of nums2. Can you please elaborate your concern?
@amogu_07
@amogu_07 8 ай бұрын
@@nikoo28 i didnt understand how ppl got it , you didnt explain it properly at all u shd see the video urself
@sanand6084
@sanand6084 Жыл бұрын
Awesome bro ! Subbed.
@nikoo28
@nikoo28 Жыл бұрын
Thanks for the sub!
@AdityaRaj-pe3zy
@AdityaRaj-pe3zy 4 ай бұрын
most of the you tuber have taken the same example and are doing the same thing but in question we have to compare NUMS1 and NUMS2 the find it
@NidhiChaudhary-u4b
@NidhiChaudhary-u4b 11 ай бұрын
I never thought this could be that simple thank you
@kavinsanthosh1306
@kavinsanthosh1306 Жыл бұрын
you are finding the greater element for every element in nums2 arr. But the question was for only nums1 arr. How to get only the answer for nums1 arr?
@VinaySharma-pz9tr
@VinaySharma-pz9tr Жыл бұрын
exactly...is jxtu ne aadha question btaya hai aur ek jagha galat logic bhi btaya hai. iski mkc
@FelixTheForgotten
@FelixTheForgotten Жыл бұрын
Have Fun! If you can't finish the code from there, you need to learn programming better. Here is my solution in c++, copy it if you want. class Solution { public: vector nextGreaterElement(vector& nums1, vector& nums2) { vector ans(nums2.size(),-1); stack num; for(int i = nums2.size()-1;i>=0;i--){ while(!num.empty() && num.top()
@amogu_07
@amogu_07 8 ай бұрын
@@VinaySharma-pz9tr bro he did NGE 1 for the first half and jumped to NGE 2 next half wtf man🤣🤣
@nikoo28
@nikoo28 8 ай бұрын
once you have found out the next greater element for each integer in nums2, you literally have everything needed to solve. The remaining part should be fairly simple. You can maintain a map/array for each element of nums1.
@pratishkumar32
@pratishkumar32 Жыл бұрын
Sir please can you explain Next greater element II question no. 503
@nikoo28
@nikoo28 Жыл бұрын
i will create a video on it soon
@vishaltorgal3372
@vishaltorgal3372 Жыл бұрын
Awesome
@ranjansingh5414
@ranjansingh5414 Жыл бұрын
The should be actual code running for all inputs ''''''''' long[] result = new long[n]; // Your code here long[] result = new long[n]; Stack stack = new Stack(); for(int i = n-1; i>=0; i--){ long element = arr[i]; if(stack.isEmpty()){ stack.push(element); result[i] = -1; continue; } if(element < stack.peek()){ result[i] = stack.peek(); stack.push(element); continue; } while(stack.peek()
@chinmaypanchal2521
@chinmaypanchal2521 Жыл бұрын
Thankyou bro
@Ishowspeed_147
@Ishowspeed_147 Жыл бұрын
if i need to print the index of that numbers how we can done with this code please explain me
@nikoo28
@nikoo28 Жыл бұрын
use an additional stack to store the indices
@Ishowspeed_147
@Ishowspeed_147 Жыл бұрын
@@nikoo28 can u please send me the code
@nikoo2805
@nikoo2805 Жыл бұрын
try it out on your own first.
@Ishowspeed_147
@Ishowspeed_147 Жыл бұрын
@@nikoo2805 huhu
@ashishkr.229
@ashishkr.229 Жыл бұрын
you didnt declare helperStack
@nikoo28
@nikoo28 11 ай бұрын
check out the solution on Github in the video description. That is often more complete.
@harikrishnas1299
@harikrishnas1299 7 ай бұрын
static int[] nextGreaterElement(int[] arr) { int[] nge = new int[arr.length]; Stack st = new Stack(); for(int i= arr.length-1; i>=0; i--) { int element = arr[i]; if(st.isEmpty()) { st.push(element); nge[i] = -1; continue; } if(st.peek() > element ) { st.push(element); nge[i] = st.peek(); continue; } while(st.peek()
@Harshii2134
@Harshii2134 9 ай бұрын
just woww o my god!!
@shalini3286
@shalini3286 2 жыл бұрын
keep it up broooo :)
@jackfrost8969
@jackfrost8969 6 ай бұрын
this wasn't even Leetcode 496 problem
@nikoo28
@nikoo28 6 ай бұрын
What do you mean?
@jackfrost8969
@jackfrost8969 6 ай бұрын
@@nikoo28 your solution was not related to Leetcode 496 question
@gokulakannan3664
@gokulakannan3664 Жыл бұрын
In your explanation part, In the array, there is two 3.... if we want next higher value of 3 , then which one will consider.... in your explanation, u use map.... it can't allow duplicate key .... explain u explain?
@pranshusati5115
@pranshusati5115 Жыл бұрын
Hey nikhil i iknow this video is over 1 year old but could you help me get why dose my code not work i passed 12/16 test cases //code here HashMap mp = new HashMap(); mp.put(nums2[nums2.length -1],-1); int max = nums2[nums2.length-1]; for(int i = nums2.length-2; i>=0; i--){ int current= nums2[i]; if(current < max){ if(i!=nums2.length && nums2[i+1] > current){ max = nums2[i+1]; mp.put(current,max );} } else mp.put(current,-1); max = Math.max(current,max); } int[] res = new int [nums1.length]; for(int i =0; i< nums1.length; i++){ res[i] = mp.get(nums1[i]); } return res;
@nikoo28
@nikoo28 Жыл бұрын
It will be hard to debug for me. Try some edge cases would be my suggestion
Next Greater Element | Two Variants | Leetcode
22:00
take U forward
Рет қаралды 257 М.
風船をキャッチしろ!🎈 Balloon catch Challenges
00:57
はじめしゃちょー(hajime)
Рет қаралды 92 МЛН
Мама у нас строгая
00:20
VAVAN
Рет қаралды 10 МЛН
L5. Next Greater Element | Stack and Queue Playlist
18:25
take U forward
Рет қаралды 62 М.
2 NGR | Nearest Greater to right | Next Largest Element
31:11
Aditya Verma
Рет қаралды 302 М.
Next Greater Element I - Leetcode 496 - Python
14:53
NeetCode
Рет қаралды 79 М.
How to STUDY so FAST it feels like CHEATING
8:03
The Angry Explainer
Рет қаралды 1,8 МЛН
LeetCode 496: Next Greater Element I
12:56
Engineering Digest
Рет қаралды 3,1 М.