L5. Next Greater Element | Stack and Queue Playlist

  Рет қаралды 62,413

take U forward

take U forward

Күн бұрын

Пікірлер: 43
@akhilesh_ku
@akhilesh_ku 4 ай бұрын
For leetcode's 496 it has two arrays , first just make nge for nums2 then take map and maps indexes of nums1 in nums2 then find it's value from nge vector nextGreaterElement(vector& nums1, vector& nums2) { int n = nums1.size(); int m = nums2.size(); vector nexti(m,-1); vector ans; unordered_map mp; stack st; st.push(-1); for(int i=m - 1;i>=0;i--){ while(!st.empty() && st.top()
@DhananjayKumar-bd2jg
@DhananjayKumar-bd2jg 3 ай бұрын
No need to create extra vector to store the index, just put in the map where key is array element and value is next greater element.
@SibiRanganathL
@SibiRanganathL 3 ай бұрын
Useful
@anishaa3298
@anishaa3298 2 күн бұрын
UNDERSTOOD!!!!!!!!!
@harshuke5831
@harshuke5831 6 күн бұрын
Understood ❤
@DhananjayKumar-bd2jg
@DhananjayKumar-bd2jg 3 ай бұрын
Solution for the leetcode :) vector nextGreaterElement(vector& nums1, vector& arr) { stack st; unordered_map m; int n = arr.size(); for(int i = n-1; i >= 0; i--){ while(!st.empty() && st.top() < arr[i]) st.pop(); if(st.empty()) m[arr[i]] = -1; else m[arr[i]] = st.top(); st.push(arr[i]); } vector ans; for(int i = 0; i < nums1.size(); i++){ ans.push_back(m[nums1[i]]); } return ans; }
@yoddha621
@yoddha621 Ай бұрын
Such easy solution, can't come up with this.
@parth2439
@parth2439 9 сағат бұрын
understood, Thank you
@jatinsaini2914
@jatinsaini2914 Ай бұрын
brother kya phadate ho app bhaut hi sahi..👌
@closer9689
@closer9689 3 ай бұрын
CODE => class Solution { public: //Better Approach :- T.C => O(n+m), S.C => O(n) vector nextGreaterElement(vector& nums1, vector& nums2) { int m = nums1.size(); int n = nums2.size(); unordered_map mp; /*for(int i = 0 ; i < m ; ++i) //No need to store nums1 element , in map, WHY? beacause , no need to check for element of nums2 , whether it is in nums1 or not { mp[nums1[i]] = -1; } */ stack st; for(int i = n-1 ; i >= 0 ; --i ) //traversing the nums2 backwards { int curr = nums2[i]; while( !st.empty() && st.top() < curr ) { st.pop(); } //If Not empty//top is ans//If empty//-1 is ans mp[curr] = !st.empty() ? st.top() : -1; //first check if stack is empty or not st.push(curr); } vector result; for(int i = 0 ; i < m ; ++i) { result.push_back(mp[nums1[i]]); } return result; } };
@foodiepanda6281
@foodiepanda6281 23 күн бұрын
Thanks for the video
@chitranshverma1822
@chitranshverma1822 4 ай бұрын
in brute force approach you didnt noted anything about -1 in answer....the case when there is nothing larger that the current one in nums
@personadevwithhitsat7277
@personadevwithhitsat7277 3 ай бұрын
Assign the whole array with -1
@xperthighlight
@xperthighlight Ай бұрын
after the loop you can add assign in to -1 ; if loops doesn't break than it will get assigned -1
@shashankshukla7989
@shashankshukla7989 Ай бұрын
JAVA BRUTEFORCE class Solution { public int nextGreater(int[] arr, int num) { int n = arr.length; for(int i=0; i
@AftabNaik
@AftabNaik Ай бұрын
This can also be implemented in linear time without using a stack Instead of maintaining a stack, we can say nge(i) = i+1 by default and then let's check if it's actually the case If yes then well n good Else we can say that nge(i) = nge(i+1) again check if its true or not, if its not then we will just check for nge(nge(i+1)) This will keep the range amortized and hence the time complexity will be O(n) Iterative implementation would be nge[i] = i+1; while(nge[i]>=nge[i+1]) { if(nge[i]==n)break; nge(i) = nge[nge[i]]; }
@kingkohli7175
@kingkohli7175 3 ай бұрын
amazing💯
@SibiRanganathL
@SibiRanganathL 3 ай бұрын
Understood
@DeadPoolx1712
@DeadPoolx1712 Ай бұрын
UNDERSTOOD;
@shivisingh9975
@shivisingh9975 3 ай бұрын
Understood!
@ashishlesnar9577
@ashishlesnar9577 Ай бұрын
for java solution public int[] nextGreaterElement(int[] nums1, int[] nums2) { Map map = new HashMap(); Stack stack = new Stack(); int n = nums1.length; int[] ans = new int[n]; for (int num : nums2) { while (!stack.isEmpty() && stack.peek() < num) map.put(stack.pop(), num); stack.push(num); } for (int i = 0; i < nums1.length; i++) { ans[i] = map.getOrDefault(nums1[i], -1); } return ans; }
@Abhi_9478
@Abhi_9478 3 ай бұрын
mind blowing
@NiteshMaurya1111
@NiteshMaurya1111 3 ай бұрын
thanks bhaiya
@chitranshverma1822
@chitranshverma1822 4 ай бұрын
the leetcode question includes circular array ..bhaiya how to deal with it??
@anm9992
@anm9992 4 ай бұрын
In reverse order first store all elements in stack then use ngr
@SarojVerma-z7q
@SarojVerma-z7q Ай бұрын
LeetCode's 496. Next Greater Element I class Solution { public: vector nextGreaterElement(vector& nums1, vector& nums2) { vector res; int i = 0; while(i < nums1.size()) { int pos2 = 0; for(int j=0; j
@sauravfarkade1928
@sauravfarkade1928 4 ай бұрын
Understood!!
@kaustubhgaikwad2562
@kaustubhgaikwad2562 4 ай бұрын
If I have 1 then next greater to it will be 2 not 3 tests cases are failing
@debadityaghosh7612
@debadityaghosh7612 2 ай бұрын
great
@nayankhuman1043
@nayankhuman1043 Ай бұрын
understood :)
@KartikeyTT
@KartikeyTT 4 ай бұрын
tysm sir
@rohitroy20786
@rohitroy20786 3 ай бұрын
496. Next Greater Element I class Solution { public: vector nextGreaterElement(vector& nums1, vector& nums2) { int n=nums2.size(); stack st; map mpp; for(int i=n-1;i>=0;i--){ while(!st.empty() && nums2[i]>=st.top()){ st.pop(); } if(st.empty()){ mpp[nums2[i]]=-1; } else{ mpp[nums2[i]]=st.top(); } st.push(nums2[i]); } vector ans; for(int i=0;i
@akashdevmore4447
@akashdevmore4447 25 күн бұрын
can any one explain tc of optimal soln is O(2n) not O(n^2)?
@anuranpradhan
@anuranpradhan 2 ай бұрын
song name in the last?
@krrishlather6521
@krrishlather6521 2 ай бұрын
should include the code also
@kartikverma6412
@kartikverma6412 4 ай бұрын
code :- class Solution { public: vector nextGreaterElement(vector& nums1, vector& nums2) { //monotonic stack-- ele are stored in a specific order stack st; unordered_map mp; //reverse order traversal for(int i=nums2.size()-1;i>=0;i--){ while(!st.empty() && st.top()
@surajbkunte7672
@surajbkunte7672 3 ай бұрын
vector nextGreaterElement(vector& nums1, vector& nums2) { map mpp; stack stk; for (int i = nums2.size() - 1; i >= 0; i--) { while (!stk.empty() && nums2[i] >= stk.top()) { stk.pop(); } if(stk.empty()){ mpp[nums2[i]] = -1; } else{ mpp[nums2[i]] = stk.top(); } stk.push(nums2[i]); } for (int i = 0; i < nums1.size(); i++) { nums1[i] = mpp[nums1[i]]; } return nums1; }
@rohitraj-df8qs
@rohitraj-df8qs 4 ай бұрын
why so less views
@vrajpatel9259
@vrajpatel9259 4 ай бұрын
this entire playlist is uploaded a day ago.
@alm7677
@alm7677 4 ай бұрын
Green looks 🤢 good
@oyeesharme
@oyeesharme Ай бұрын
thanks bhaiya
@aryankumar3018
@aryankumar3018 3 ай бұрын
Understood
@rutujashelke4208
@rutujashelke4208 2 ай бұрын
Understood
L6. Next Greater Element - II | Stack and Queue Playlist
15:41
take U forward
Рет қаралды 40 М.
I Solved 100 LeetCode Problems
13:11
Green Code
Рет қаралды 245 М.
FOREVER BUNNY
00:14
Natan por Aí
Рет қаралды 25 МЛН
Players push long pins through a cardboard box attempting to pop the balloon!
00:31
Don't underestimate anyone
00:47
奇軒Tricking
Рет қаралды 15 МЛН
L8. Trapping Rainwater | 2 Approaches | Stack and Queue Playlist
28:58
How I Mastered Data Structures and Algorithms
10:45
Ashish Pratap Singh
Рет қаралды 257 М.
L12. Largest Rectangle in Histogram | Stack and Queue Playlist
31:42
take U forward
Рет қаралды 43 М.
Big-O Notation - For Coding Interviews
20:38
NeetCode
Рет қаралды 515 М.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 687 М.
L4. Implement Min Stack | Stack and Queue Playlist
20:55
take U forward
Рет қаралды 45 М.
DSA & ₹1.2 Crore Per Annum Jobs - The Truth? (No Offence)
12:22
CodeWithHarry
Рет қаралды 712 М.
Next Greater Element I - Leetcode 496 - Python
14:53
NeetCode
Рет қаралды 79 М.
FOREVER BUNNY
00:14
Natan por Aí
Рет қаралды 25 МЛН