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-bd2jg3 ай бұрын
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.
@SibiRanganathL3 ай бұрын
Useful
@anishaa32982 күн бұрын
UNDERSTOOD!!!!!!!!!
@harshuke58316 күн бұрын
Understood ❤
@DhananjayKumar-bd2jg3 ай бұрын
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Ай бұрын
Such easy solution, can't come up with this.
@parth24399 сағат бұрын
understood, Thank you
@jatinsaini2914Ай бұрын
brother kya phadate ho app bhaut hi sahi..👌
@closer96893 ай бұрын
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; } };
@foodiepanda628123 күн бұрын
Thanks for the video
@chitranshverma18224 ай бұрын
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
@personadevwithhitsat72773 ай бұрын
Assign the whole array with -1
@xperthighlightАй бұрын
after the loop you can add assign in to -1 ; if loops doesn't break than it will get assigned -1
@shashankshukla7989Ай бұрын
JAVA BRUTEFORCE class Solution { public int nextGreater(int[] arr, int num) { int n = arr.length; for(int i=0; i
@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]]; }
@kingkohli71753 ай бұрын
amazing💯
@SibiRanganathL3 ай бұрын
Understood
@DeadPoolx1712Ай бұрын
UNDERSTOOD;
@shivisingh99753 ай бұрын
Understood!
@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_94783 ай бұрын
mind blowing
@NiteshMaurya11113 ай бұрын
thanks bhaiya
@chitranshverma18224 ай бұрын
the leetcode question includes circular array ..bhaiya how to deal with it??
@anm99924 ай бұрын
In reverse order first store all elements in stack then use ngr
@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
@sauravfarkade19284 ай бұрын
Understood!!
@kaustubhgaikwad25624 ай бұрын
If I have 1 then next greater to it will be 2 not 3 tests cases are failing
@debadityaghosh76122 ай бұрын
great
@nayankhuman1043Ай бұрын
understood :)
@KartikeyTT4 ай бұрын
tysm sir
@rohitroy207863 ай бұрын
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
@akashdevmore444725 күн бұрын
can any one explain tc of optimal soln is O(2n) not O(n^2)?
@anuranpradhan2 ай бұрын
song name in the last?
@krrishlather65212 ай бұрын
should include the code also
@kartikverma64124 ай бұрын
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()
@surajbkunte76723 ай бұрын
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; }