Underrated channel, sir please make more videos ,you have the potential to be on top.
@nikoo286 ай бұрын
I am adding one new video every week 😄
@NerchukoMava6 ай бұрын
@@nikoo28 I am so glad for your reply sir😇
@NerchukoMava6 ай бұрын
Sir please do videos on Recursion as soon as possible, which will be so helpful for many students
@TufanPandu Жыл бұрын
After watching my expensive course i come to watch this video and it's OSM!
@nikoo28 Жыл бұрын
Amazing!
@surajsidar32805 ай бұрын
Which course you took and what was the cost? Please tell so that I won't take that course
@bonafontciel Жыл бұрын
I was able to type my own code just with your explanation. Thanks!
@nikoo28 Жыл бұрын
Great job!
@varsshhaa6 күн бұрын
omg!! I am happy that I have found this channel !! explanation is simply great !! Looking forward to more such videos !!
@Hayat2647411 ай бұрын
i never thought this could be that simple , thanku sir
@dharmeshkase60038 ай бұрын
the way you explain everything is just
@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 Жыл бұрын
if you check the code available in the video description, that works perfectly and passes on LeetCode.
@kireetmishra16719 ай бұрын
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 Жыл бұрын
Great explanation! Clear and intuitive!
@a3rdtierguy8646 ай бұрын
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()
@pietech16945 ай бұрын
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
@tanishktripathi877310 ай бұрын
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 Жыл бұрын
Great explanation. Thank you so much Nikhil
@srikrishna_ss82632 жыл бұрын
Your Way Of Explanation Simply Awesome 🤗
@sahithd312529 күн бұрын
loved the way you explain the problem!
@andretheruler7146 ай бұрын
thank you, i was a bit confused until you explained it
@pradheeshkumar-v4p5 ай бұрын
That was a crystal clear explanation !!
@vilakshan.s6 ай бұрын
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
@sammedpatil39072 жыл бұрын
what will be space and time space complexity if we are using stack and Map??
@MuhammadFahreza3 ай бұрын
in 13:40. Why the time complexity is O(n) ? Should we also count, the effort to traverse down the stack for each value ?
@nikoo283 ай бұрын
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)
@mohammedshahed50518 ай бұрын
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 Жыл бұрын
amazing explanation!
@kemarjordan6285 күн бұрын
Brilliant man!
@Rocky-me7qh2 ай бұрын
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
@mohinischannel96652 жыл бұрын
very nice explaination ..clear.
@hilalkocak_8 ай бұрын
Best explanation ever!
@nikoo287 ай бұрын
Glad it was helpful!
@deepakkumar-sk4kj3 күн бұрын
Please do Solve Leetcode 503 Next Greater Element II
@Elkhaligy5 ай бұрын
Good one
@stanleyjekwu68102 жыл бұрын
Thank you so much, very clear
@sumitsanu952411 ай бұрын
Same example as striver
@vinodpaluvuri54 Жыл бұрын
we need to compare nums1 with nums2..where are we doing that
@nikoo28 Жыл бұрын
nums1 is a subset of nums2. Can you please elaborate your concern?
@amogu_078 ай бұрын
@@nikoo28 i didnt understand how ppl got it , you didnt explain it properly at all u shd see the video urself
@sanand6084 Жыл бұрын
Awesome bro ! Subbed.
@nikoo28 Жыл бұрын
Thanks for the sub!
@AdityaRaj-pe3zy4 ай бұрын
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-u4b11 ай бұрын
I never thought this could be that simple thank you
@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 Жыл бұрын
exactly...is jxtu ne aadha question btaya hai aur ek jagha galat logic bhi btaya hai. iski mkc
@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_078 ай бұрын
@@VinaySharma-pz9tr bro he did NGE 1 for the first half and jumped to NGE 2 next half wtf man🤣🤣
@nikoo288 ай бұрын
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 Жыл бұрын
Sir please can you explain Next greater element II question no. 503
@nikoo28 Жыл бұрын
i will create a video on it soon
@vishaltorgal3372 Жыл бұрын
Awesome
@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 Жыл бұрын
Thankyou bro
@Ishowspeed_147 Жыл бұрын
if i need to print the index of that numbers how we can done with this code please explain me
@nikoo28 Жыл бұрын
use an additional stack to store the indices
@Ishowspeed_147 Жыл бұрын
@@nikoo28 can u please send me the code
@nikoo2805 Жыл бұрын
try it out on your own first.
@Ishowspeed_147 Жыл бұрын
@@nikoo2805 huhu
@ashishkr.229 Жыл бұрын
you didnt declare helperStack
@nikoo2811 ай бұрын
check out the solution on Github in the video description. That is often more complete.
@harikrishnas12997 ай бұрын
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()
@Harshii21349 ай бұрын
just woww o my god!!
@shalini32862 жыл бұрын
keep it up broooo :)
@jackfrost89696 ай бұрын
this wasn't even Leetcode 496 problem
@nikoo286 ай бұрын
What do you mean?
@jackfrost89696 ай бұрын
@@nikoo28 your solution was not related to Leetcode 496 question
@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 Жыл бұрын
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 Жыл бұрын
It will be hard to debug for me. Try some edge cases would be my suggestion