I didn’t know how to solve this Leetcode Problem!😭😭😭

This is a medium Leetcode 402 question where you are asked to remove k digits from a number to make that number the smallest possible number. See problem description below:

problem description

The question is quite understandable and straight forward but the issue is with knowing the numbers to remove. At first, I thought that sorting the numbers and keeping track of the positions and then removing the largest numbers would work but apparently that didn’t work.

After trying to no avail, I had to seek for solution online and came across two algorithms:


To understand the algorithm above, please check thecodingworld on YouTube. He did a good job to explain the algorithm. His code was written in Python, so I had to translate to Java.


Also to understand the second algorithm above, please check Tech Dose for the explanation. I also translated the code to Java.


I have learnt a lot from these algorithms especially from the way people think and I think that’s the fun of solving algorithm questions.

Thank you for reading. Please leave a comment or suggestion below.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store