Greedy

There are different type of greedy approachs, but generally follows the pattern:

  1. find the rule for best move during each step
  2. implement the rule each step, might need to repeatedly apply the rule
  3. some common patterns of the rule: eliminiation, update variables, ...

Elimination

402. Remove K Digits

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.

results matching ""

    No results matching ""