L5. Next Greater Element | Stack and Queue Playlist

Поділитися
Вставка
  • Опубліковано 14 лис 2024

КОМЕНТАРІ • 40

  • @akhilesh_ku
    @akhilesh_ku 3 місяці тому +22

    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 місяці тому +2

      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

  • @DhananjayKumar-bd2jg
    @DhananjayKumar-bd2jg 3 місяці тому +5

    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;
    }

  • @jatinsaini2914
    @jatinsaini2914 26 днів тому

    brother kya phadate ho app bhaut hi sahi..👌

  • @chitranshverma1822
    @chitranshverma1822 3 місяці тому +16

    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 місяці тому +17

      Assign the whole array with -1

    • @xperthighlight
      @xperthighlight Місяць тому +1

      after the loop you can add assign in to -1 ;
      if loops doesn't break than it will get assigned -1

    • @shashankshukla7989
      @shashankshukla7989 Місяць тому +1

      JAVA BRUTEFORCE
      class Solution {
      public int nextGreater(int[] arr, int num) {
      int n = arr.length;
      for(int i=0; i

  • @yoddha621
    @yoddha621 Місяць тому +1

    Such easy solution, can't come up with this.

  • @closer9689
    @closer9689 3 місяці тому +1

    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 15 днів тому

    Thanks for the video

  • @AftabNaik
    @AftabNaik Місяць тому +1

    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]];
    }

  • @krrishlather6521
    @krrishlather6521 2 місяці тому +2

    should include the code also

  • @kingkohli7175
    @kingkohli7175 2 місяці тому

    amazing💯

  • @DeadPoolx1712
    @DeadPoolx1712 Місяць тому

    UNDERSTOOD;

  • @kaustubhgaikwad2562
    @kaustubhgaikwad2562 3 місяці тому

    If I have 1 then next greater to it will be 2 not 3 tests cases are failing

  • @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

  • @chitranshverma1822
    @chitranshverma1822 3 місяці тому +3

    the leetcode question includes circular array ..bhaiya how to deal with it??

    • @anm9992
      @anm9992 3 місяці тому

      In reverse order first store all elements in stack then use ngr

  • @SibiRanganathL
    @SibiRanganathL 3 місяці тому +1

    Understood

  • @shivisingh9975
    @shivisingh9975 2 місяці тому

    Understood!

  • @akashdevmore4447
    @akashdevmore4447 17 днів тому

    can any one explain tc of optimal soln is O(2n) not O(n^2)?

  • @Abhi_9478
    @Abhi_9478 3 місяці тому

    mind blowing

  • @NiteshMaurya1111
    @NiteshMaurya1111 3 місяці тому

    thanks bhaiya

  • @kartikverma6412
    @kartikverma6412 3 місяці тому +1

    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()

  • @anuranpradhan
    @anuranpradhan 2 місяці тому +1

    song name in the last?

  • @nayankhuman1043
    @nayankhuman1043 Місяць тому

    understood :)

  • @KartikeyTT
    @KartikeyTT 3 місяці тому

    tysm sir

  • @debadityaghosh7612
    @debadityaghosh7612 2 місяці тому

    great

  • @sauravfarkade7032
    @sauravfarkade7032 4 місяці тому

    Understood!!

  • @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

  • @ashishlesnar9577
    @ashishlesnar9577 22 дні тому

    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;
    }

  • @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 місяці тому +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 2 місяці тому

    Understood

  • @rutujashelke4208
    @rutujashelke4208 2 місяці тому

    Understood