Show problems as
Classic problem #1323EasyMathGreedy

Maximum 69 Number

Real-world version: The Best Single Swap on a Six-Nine Counter
Tracked 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)