Classic problem #1323EasyMathGreedy
Maximum 69 Number
Real-world version: The Best Single Swap on a Six-Nine CounterTracked in this browser only
Read Maximum 69 Number on LeetCode(opens in a new tab)
The statement here is our own: the same inputs, outputs and technique.
A counter shows the number num, and every one of its digits is a 6 or a 9. At most one digit may be swapped, turning a 6 into a 9 or a 9 into a 6. Leaving the counter untouched is allowed.
Return the largest number the counter can be made to show.
Examples
Example 1
- Input
- num = 66
- Output
- 96
Lifting the leading digit gives 96, which beats lifting the second one.
Example 2
- Input
- num = 9696
- Output
- 9996
The leading digit is already a nine, so the earliest six is the second digit, and lifting it gives 9996.
Example 3
- Input
- num = 999
- Output
- 999
Every digit is already a nine, so the counter is best left untouched.
Constraints
- 1 <= num <= 10^4
- every digit of num is a 6 or a 9
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
Python
def maximum69_number(num: int) -> int:Java
public int maximum69Number(int num)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Minimum Cost to Move Chips to The Same PositionReal-world version: Shunting Pallets Into One BayEasy
- Minimum Number of Pushes to Type Word IReal-world version: Eight-Key Field TerminalEasy
- Largest Odd Number in StringReal-world version: Largest Odd Segment of a SerialEasy
- Distribute Money to Maximum ChildrenReal-world version: Badges at the Token CounterEasy