↓Skip to main content
  1. LeetCode/

LeetCode 2231: Largest Number After Digit Swaps by Parity

·2 mins· ·
LeetCode Easy Sorting Heap-Priority-Queue
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-11-22

  • Submissions: Wrong Answer ×2 → Accepted

Approach
#

Parity refers to the digit’s value, not its position. Split the digits into odd and even groups, sort each descending (or use max-heaps, Heap), then from left to right fill each odd-digit position with the largest remaining odd digit and each even-digit position with the largest remaining even digit.

My Attempts
#

The first two grouped digits by the index’s parity (i % 2) instead of the digit’s: all four digits of 5575 are odd and can become 7555, but one version returned nothing and the other the wrong value. Testing int(s) % 2 passed.

Solution
#

class Solution:
    def largestInteger(self, num: int) -> int:
        digits = [int(c) for c in str(num)]
        odd = sorted((d for d in digits if d % 2), reverse=True)
        even = sorted((d for d in digits if d % 2 == 0), reverse=True)
        oi = ei = 0
        res = 0
        for d in digits:
            if d % 2:
                res = res * 10 + odd[oi]; oi += 1
            else:
                res = res * 10 + even[ei]; ei += 1
        return res

Complexity
#

Time O(d log d) for d digits, space O(d).

Mistakes to Avoid
#

  • Group by the digit’s parity, not the index’s.
  • Return an integer.

Interview Explanation
#

Odd digits swap only with odd and even with even, so I sort each group descending and, from left to right, give each position the largest remaining digit of its own parity.

Related

LeetCode 2099: Find Subsequence of Length K With the Largest Sum
·1 min
LeetCode Easy Array Hash-Table Sorting Heap-Priority-Queue
LeetCode 2099 note: pick the k largest values along with their indices, then sort by index to restore the original order
LeetCode 2335: Minimum Amount of Time to Fill Cups
·1 min
LeetCode Easy Array Greedy Sorting Heap-Priority-Queue
LeetCode 2335 note: the answer is max(the largest count, ceil(total / 2)); simulating means filling one cup from each of the two largest every second
LeetCode 1464: Maximum Product of Two Elements in an Array
·1 min
LeetCode Easy Array Sorting Heap-Priority-Queue
LeetCode 1464 note: all values are positive, so take the two largest a and b and return (a - 1)(b - 1)
LeetCode 1337: The K Weakest Rows in a Matrix
·1 min
LeetCode Easy Array Binary-Search Sorting Heap-Priority-Queue
LeetCode 1337 note: soldiers come first in each row, so binary search the first 0 for the count; sort by (count, row index) and take k
LeetCode 506: Relative Ranks
·1 min
LeetCode Easy Array Sorting Heap-Priority-Queue
LeetCode 506 note: sort indices by score descending, give medals to the top three and rank strings to the rest, writing back into the original positions
LeetCode 2357: Make Array Zero by Subtracting Equal Amounts
·1 min
LeetCode Easy Array Hash-Table Greedy Sorting
LeetCode 2357 note: each operation zeroes the group of the smallest positive value, so the answer is the number of distinct positive values