Greedy
There are different type of greedy approachs, but generally follows the pattern:
- find the rule for best move during each step
- implement the rule each step, might need to repeatedly apply the rule
- some common patterns of the rule: eliminiation, update variables, ...
Elimination
Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible.
Example 1:
Input: num = "1432219", k = 3
Output: "1219"
Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.
The rule: when choosing to keep 1 or 4 in 1432219, we will choose 1 as this will gives a smaller number. Apply this rule of removal repeatedly, we will get the smallest number.