402. Remove K Digits | Monotonic Stack | Greedy | Brute Force - Optimal Solution

Sdílet
Vložit
  • čas přidán 9. 04. 2024
  • In this video, I'll talk about how to solve Leetcode 402. Remove K Digits | Monotonic Stack | Greedy | Brute Force - Optimal Solution
    Stack Playlist - • Complete Stack & Queue...
    Greedy Playlist - • Complete Greedy Intui...
    Similar Problem - • 3106. Lexicographicall...
    Let's Connect:
    📝Linkedin: / aryan-mittal-0077
    📸 Instagram: / ez.pz.dsa
    📱Telegram : t.me/aryan_mittal_group
    🤖 Github: github.com/aryan-0077
    About Me:
    I am Aryan Mittal - A Software Engineer in Goldman Sachs, Speaker, Creator & Educator. During my free time, I create programming education content on this channel & also how to use that to grow :)
    ✨ Hashtags ✨
    #programming #Interviews #leetcode #faang #maang #datastructures #algorithms

Komentáře • 25

  • @ARYANMITTAL
    @ARYANMITTAL  Před 3 měsíci

    Practice StacK & Monotonic Stack Problems - czcams.com/play/PLEL7R4Pm6EmDSG2vFQN8S04AQfLHC0jR7.html
    .
    Latest Video on *Hindi Channel* - czcams.com/video/VAP7AZRewCk/video.html

  • @AJ-xc3ks
    @AJ-xc3ks Před 3 měsíci

    Bro and we reversing it or using str function so these t.c will be added nn

  • @rakeshchahal8330
    @rakeshchahal8330 Před 3 měsíci +2

    class Solution {
    public:
    string removeKdigits(string nums, int k) {
    int n = nums.size();
    string ans="";
    for(int i=0;inums[i] && k>0)
    {
    ans.pop_back();
    k--;
    }
    ans.push_back(nums[i]);
    }
    while(!ans.empty() && k>0)
    {
    ans.pop_back();
    k--;
    }
    int i=0;
    while(ans[i]=='0' && i

  • @rishabhgupta6179
    @rishabhgupta6179 Před 3 měsíci

    Hi Arayan , I am still not able to understand why removing numbers which are greater than the current number will ensure least possible ans . If possible , do you have an easy explanation.

  • @siddharthkachhia4829
    @siddharthkachhia4829 Před 3 měsíci

    I was try to do it by DP but it gives me TLE😅

  • @codingkart245
    @codingkart245 Před 3 měsíci +2

    idk whether I'm the one who misses his energetic starting this time.

  • @satwiktatikonda764
    @satwiktatikonda764 Před 3 měsíci +1

    bro waiting for 100days series :(

    • @ARYANMITTAL
      @ARYANMITTAL  Před 3 měsíci +1

      Yaa bro, I’ll planning to bring it in Hindi, so doing preparations for it, will be available on Hindi Channel ❤️❤️

  • @Ramgangakumar
    @Ramgangakumar Před měsícem

    bro your voice [isToLow].

  • @user-yl4vp2ox8v
    @user-yl4vp2ox8v Před 2 měsíci

    #include
    int rever ( int num)
    {
    int rev = 0 ;
    while(num > 0)
    {
    int reminder = num%10;
    rev = rev*10 + reminder;
    num = num/10;
    }
    return rev ;
    }
    int main()
    {
    int num = 125;
    int rev = 0 ;
    int pos = 1 ;
    while (num > 0 )
    {
    int reminder = num %10;
    if (pos == 1 || pos == 4)
    {
    rev = 10 *rev + reminder;
    pos++;
    }
    pos++;
    num = num/10;
    }
    int rev1 = rever(rev);
    printf("%d", rev1);
    }

  • @user-cv6ed5cb9c
    @user-cv6ed5cb9c Před 3 měsíci

    For me it gave memory limit exceeded for test case 42/43 .
    The code was same :
    class Solution {
    public:
    string removeKdigits(string num, int k) {
    stack st;
    for(auto i:num){
    while(!st.empty() && i0 ){
    st.pop();k--;
    }
    st.push(i);
    }
    while(k>0 && !st.empty())st.pop(),k--;
    string ans="";
    while(!st.empty()){
    ans=ans+st.top();
    st.pop();
    }
    int endIdx=0;
    for(int i=ans.length()-1;i>=0;i--){
    if(ans[i]!='0'){
    endIdx=i;
    break;
    }
    }
    ans=ans.substr(0,endIdx+1);
    if(ans=="")return "0";
    reverse(ans.begin(),ans.end());
    return ans;
    }
    };

    • @kabir2190
      @kabir2190 Před 3 měsíci

      same bro 🤣
      then i solved it using deque try using deque

    • @selfgrowthsigma
      @selfgrowthsigma Před 3 měsíci

      class Solution {
      public:
      string removeKdigits(string nums, int k) {
      int n=nums.size();
      if(n==k) return "0";
      stackst;
      for(int i=0;inums[i] && k>0){
      k--;
      st.pop();
      }
      st.push(nums[i]);
      }
      while(!st.empty() && k>0){
      k--;
      st.pop();
      }
      string ans="";
      while(!st.empty()){
      ans+=st.top();
      st.pop();
      }
      reverse(ans.begin(),ans.end());
      for(int i=0;i

    • @YashAggarwal-ri6kt
      @YashAggarwal-ri6kt Před 3 měsíci +1

      The problem us u wrote ans=ans+st.top()
      Try ans+=st.top()

    • @kabir2190
      @kabir2190 Před 3 měsíci +1

      @@YashAggarwal-ri6kt they are different?😕

    • @ARYANMITTAL
      @ARYANMITTAL  Před 3 měsíci +4

      Yes sir, they are different, in cpp += is an overloaded operator for charcters and performs same as push_back() which is O(1) operation, while ans = ans + is an O(n) operation

  • @satwiktatikonda764
    @satwiktatikonda764 Před 3 měsíci

    Is there any reason for uploading 100 days series in Hindi channel
    Ppl like me from South india are also follows ur videos bro

  • @tkab1205
    @tkab1205 Před 3 měsíci

    Aapka sound bohot low arha ha

  • @khatriiedits3606
    @khatriiedits3606 Před 3 měsíci

    Too long video

  • @ganeshsharma5810
    @ganeshsharma5810 Před 3 měsíci

    bro can you explain the question in english hindi mix so that we understand the problem more easily

  • @RohanSharma-tl9wh
    @RohanSharma-tl9wh Před 3 měsíci

    Aryan please share your leetcode username

  • @user-yl4vp2ox8v
    @user-yl4vp2ox8v Před 2 měsíci

    #include
    int rever ( int num)
    {
    int rev = 0 ;
    while(num > 0)
    {
    int reminder = num%10;
    rev = rev*10 + reminder;
    num = num/10;
    }
    return rev ;
    }
    int main()
    {
    int num = 125;
    int rev = 0 ;
    int pos = 1 ;
    while (num > 0 )
    {
    int reminder = num %10;
    if (pos == 1 || pos == 4)
    {
    rev = 10 *rev + reminder;
    pos++;
    }
    pos++;
    num = num/10;
    }
    int rev1 = rever(rev);
    printf("%d", rev1);
    }