Remove K Digits | Intuition | Dry Run | Leetcode 402 | codestorywithMIK
iPad PDF Notes - https://github.com/MAZHARMIK/Interview_DS_Algo/blob/master/iPad%20PDF%20Notes/Leetcode-402-Remove%20K%20Digits.pdf
Whatsapp Community Link : https://www.whatsapp.com/channel/0029Va6kVSjICVfiVdsHgi1A
This is the 16th Video of our Playlist "Stack: Popular Interview Problems".
In this video we will try to solve a very famous monotonic based problem : Remove K Digits | Intuition | Dry Run | Leetcode 402 | codestorywithMIK
I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
We will do live coding after explanation and see if we are able to pass all the test cases.
Also, please note that my Github solution link below contains both C++ as well as JAVA code.
Problem Name : Remove K Digits | Intuition | Dry Run | Leetcode 402 | codestorywithMIK
Company Tags : Meta, Google, Snapchat, Microsoft
My solutions on Github(C++ & JAVA) : https://github.com/MAZHARMIK/Interview_DS_Algo/blob/master/Stack/Remove%20K%20Digits.cpp
Leetcode Link : https://leetcode.com/problems/remove-k-digits/
My DP Concepts Playlist : https://youtu.be/7eLMOE1jnls
My Graph Concepts Playlist : https://youtu.be/5JGiZnr6B5w
My Recursion Concepts Playlist : https://www.youtube.com/watch?v=pfb1Zduesi8&list=PLpIkg8OmuX-IBcXsfITH5ql0Lqci1MYPM
My GitHub Repo for interview preparation : https://github.com/MAZHARMIK/Interview_DS_Algo
Instagram : https://www.instagram.com/codestorywithmik/
Facebook : https://www.facebook.com/people/codestorywithmik/100090524295846/
Twitter : https://twitter.com/CSwithMIK
Subscribe to my channel : https://www.youtube.com/@codestorywithMIK
Summary :
The provided solution implements an algorithm to remove `k` digits from a given string `num` in order to form the smallest possible number. The approach utilizes a stack-like structure implemented using a string. It's basically monotonic stack/string/dequeue
1. Iterate through each digit of the input number.
2. While the current digit is less than the digit at the top of the stack (if it exists) and `k` is greater than 0, pop elements from the stack and decrement `k`.
3. Append the current digit to the stack if the stack is empty or if the current digit is non-zero.
4. After processing all digits, if `k` is still greater than 0, pop elements from the stack until `k` becomes 0.
5. If the resulting stack is empty, return "0"; otherwise, convert the stack to a string and return the result.
This approach efficiently handles the removal of digits while ensuring that the resultant number is the smallest possible.
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
✨ Timelines✨
00:00 - Introduction
02:55 - How to think about it / Observation
08:17 - Why Greedy Fails
10:20 - Intuition/Applying Observation
16:26 - One more Dry Run
19:14 - Why people call it Monotonic Stack
24:10 - Corner Case
25:23 - Time Complexity
27:30 - Coding it up
#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #newyear2024