Classic problem #2231EasySortingHeap (Priority Queue)
Largest Number After Digit Swaps by Parity
Real-world version: Swapping Digits That Share Their OddnessTracked in this browser only
Read Largest Number After Digit Swaps by Parity 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. Any two of its digits may be swapped, as often as you like, provided the two are both odd or both even.
Return the largest number the counter can be made to show.
Examples
Example 1
- Input
- num = 1357
- Output
- 7531
Every digit is odd, so they can all be swapped with each other and falling order is largest.
Example 2
- Input
- num = 2468
- Output
- 8642
Every digit is even, so again they arrange into falling order.
Example 3
- Input
- num = 1122
- Output
- 1122
The two odd digits are both 1 and the two even digits are both 2, so no swap changes anything.
Constraints
- 1 <= num <= 10^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 largest_integer(num: int) -> int:Java
public int largestInteger(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
- Maximum Product of Two Elements in an ArrayReal-world version: Best Pair Of Voucher PayoutsEasy
- Minimum Number GameReal-world version: Shelving Boxes in TurnsEasy
- High FiveReal-world version: Top Five Average per ApprenticeEasy
- Make Array Zero by Subtracting Equal AmountsReal-world version: Rounds Needed to Clear Every ReadingEasy