How Far Have You Gotten in Greedy
1 min readJan 2, 2021
Description
Explanation
- When the first digit of a number is as big as possible, it would be the largest number.
- To make a number the largest, we should remove a digit K times from the front, which is smaller than the digit number that comes right after. Whenever a digit deleted, K is reduced by 1.
- If all the digits get traversed once and K is more than zero, they would be arranged in descending order from largest to smallest. Then, remove digits from the rear until K becomes zero.
Solution
Review
Using a stack in a greedy problem blew my mind. What looks like greedy is to make the very front digit the largest however long the number is. I just tried to remove small numbers at first to make a descending number so I failed to handle it after that. Interesting!