All problems
1109EasyMathGreedy

The Best Single Swap on a Six-Nine Counter

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1323Maximum 69 Number

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def maximum69_number(num: int) -> int:
Java
public int maximum69Number(int num)
September 7
Apply