402. Remove K Digits | Monotonic Stack | Greedy | Brute Force - Optimal Solution
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
Пікірлер: 25
Practice StacK & Monotonic Stack Problems - kzread.info/head/PLEL7R4Pm6EmDSG2vFQN8S04AQfLHC0jR7 . Latest Video on *Hindi Channel* - kzread.info/dash/bejne/iHWEmaOTgsjdc84.html
Bro and we reversing it or using str function so these t.c will be added nn
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
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.
I was try to do it by DP but it gives me TLE😅
idk whether I'm the one who misses his energetic starting this time.
bro waiting for 100days series :(
@ARYANMITTAL
3 ай бұрын
Yaa bro, I’ll planning to bring it in Hindi, so doing preparations for it, will be available on Hindi Channel ❤️❤️
#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); }
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
3 ай бұрын
same bro 🤣 then i solved it using deque try using deque
@selfgrowthsigma
3 ай бұрын
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
3 ай бұрын
The problem us u wrote ans=ans+st.top() Try ans+=st.top()
@kabir2190
3 ай бұрын
@@YashAggarwal-ri6kt they are different?😕
@ARYANMITTAL
3 ай бұрын
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
bro your voice [isToLow].
Is there any reason for uploading 100 days series in Hindi channel Ppl like me from South india are also follows ur videos bro
Aapka sound bohot low arha ha
Too long video
bro can you explain the question in english hindi mix so that we understand the problem more easily
Aryan please share your leetcode username
#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); }