Largest Ordered Three-Digit Number
EasyGreedy · Google
You are given a list of decimal digits. Choose one, two, or three of them without changing their relative order, and write the chosen digits next to each other to form a number. Return the largest number you can form, as an integer.
Leading zeros simply disappear in the integer value: choosing 0, 4, 2 forms 42.
Example 1
- Input:
- digits = [4, 7, 1, 6, 3, 5]
- Output:
- 765
- Explanation
- The 7 is the largest digit that still leaves two digits after it, so the number starts with 7. After it, the best ordered pair is 6 then 5, giving 765.
Example 2
- Input:
- digits = [2, 0, 9]
- Output:
- 209
- Explanation
- The digits cannot be rearranged, so 920 is not allowed. Using all three gives 209, which beats any one- or two-digit choice such as 29.
Example 3
- Input:
- digits = [0, 0, 3, 8]
- Output:
- 38
- Explanation
- Any three-digit pick here starts with 0 (for example 0, 3, 8 gives 38), so the best result is the two-digit 38.
Constraints
1 <= digits.length <= 10^50 <= digits[i] <= 9