leetcode 2231. Largest Number After Digit Swaps by Parity(python)

Get into the habit of writing together! This is the 12th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

describe

You are given a positive integer num. You may swap any two digits of num that have the same parity (i.e. both odd digits or both even digits).

Return the largest possible value of num after any number of swaps.

Example 1:

Input: num = 1234
Output: 3412
Explanation: Swap the digit 3 with the digit 1, this results in the number 3214.
Swap the digit 2 with the digit 4, this results in the number 3412.
Note that there may be other sequences of swaps but it can be shown that 3412 is the largest possible number.
Also note that we may not swap the digit 4 with the digit 1 since they are of different parities.
复制代码

Example 2:

Input: num = 65875
Output: 87655
Explanation: Swap the digit 8 with the digit 6, this results in the number 85675.
Swap the first digit 5 with the digit 7, this results in the number 87655.
Note that there may be other sequences of swaps but it can be shown that 87655 is the largest possible number.
复制代码

Note:

1 <= num <= 10^9

Parse

According to the meaning of the question, give you a positive integer num, the question requires that any two odd numbers or any two even numbers in any two digits of num can be exchanged, which can be exchanged countless times, and finally returns the possible maximum value of num after the change.

When I read this question during the competition, I thought it was very simple, and the code I wrote was a bit long-winded. I always wanted to optimize the code, but I gave up because of the event. It was stinky and long, only to find that they and I wrote at the same level, and instantly lost the desire to optimize the code.

In fact, this question mainly examines the basic operations and simple sorting of arrays. The idea is quite simple:

  • Change num to list nums, because strings cannot be modified at index positions
  • Take out the even numbers in nums and put them in A, and then sort A in descending order
  • Put the elements in A in descending order, and then put them back in nums at the previous even position
  • Take out the odd numbers in nums and put them in B, and then sort B in descending order
  • Put the elements in B in descending order, and then put them back in nums in the previous odd positions
  • Concatenate nums into a string and return it

The time complexity is O(N + NlogN) and the space complexity is O(N).

answer

class Solution(object):
    def largestInteger(self, num):
        """
        :type num: int
        :rtype: int
        """
        nums = list(str(num))
        N = len(nums)
        A = [i for i in nums if int(i)%2 == 0]
        A.sort()
        A = A[::-1]
        n = 0
        for i in range(N):
            if int(nums[i])%2 == 0:
                nums[i] = A[n]
                n += 1
        B = [i for i in nums if int(i)%2 == 1]
        B.sort()
        B = B[::-1]
        n = 0
        for i in range(N):
            if int(nums[i])%2 == 1:
                nums[i] = B[n]
                n += 1
        return int(''.join(nums))		
复制代码

operation result

238 / 238 test cases passed.
Status: Accepted
Runtime: 34 ms
Memory Usage: 13.3 MB
复制代码

Original title link

leetcode.com/contest/wee…

Your support is my greatest motivation

Guess you like

Origin juejin.im/post/7085517090504835109