“This is the 25th day of my participation in the First Challenge 2022. For details: First Challenge 2022”

preface

This is the first question in the Biweekly Contest 71. It’s Easy. It’s about manipulating an array of questions.

describe

You are given a positive integer num consisting of exactly four digits. Split num into two new integers new1 and new2 by using the digits found in num. Leading zeros are allowed in new1 and new2, and all the digits found in num must be used.

  • For example, given num = 2932, you have the following digits: two 2’s, one 9 and one 3. Some of the possible pairs [new1, new2] are [22, 93], [23, 92], [223, 9] and [2, 329].

Return the minimum possible sum of new1 and new2.

Example 1:

Input: num = 2932 Output: 52 Explanation: Some possible pairs [new1, new2] are [29, 23], [223, 9], etc. The minimum sum can be obtained by the pair [29, 23]: 29 plus 23 is 52.Copy the code

Note:

1000 <= num <= 9999
Copy the code

parsing

You are given a positive integer num consisting of exactly four digits. Split num into two new integers new1 and new2 using the number in num. Leading zeros are allowed in new1 and new2, and all numbers found in num must be used. For example, given num = 2932, there are the following numbers: two 2’s, a 9, and a 3. Some possible pairs of [new1, new2] are [22, 93], [23, 92], [223, 9], and [2, 329]. Returns the smallest possible sum of new1 and new2.

In fact, this problem can be solved using the simplest algorithm, because the constraint num is only four digits, so we first extract the digits above each position and put them in a list digits, and then sort them in ascending order, because to minimize the sum of all digits, And that is to try to make the most of the first number and the last number to make up the new number. We can do this if none of the four digits in the four-digit number are 0, but if there are 0’s, it’s different. We want to pop out all the 0’s in the digits so it doesn’t affect our calculation. There are four cases:

  • When the digits length is still 4, return directly as above

      digits[0]*10+digits[-1] +digits[1]*10+digits[2]
    Copy the code
  • When digits is still 3, let the first two digits make a number, plus the third digit sum minimum, and return

      digits[0] * 10 + digits[1] + digits[2]
    Copy the code
  • When digits is of length 2, then each number is the smallest sum of its own, then return

      digits[0] + digits[1]
    Copy the code
  • When the length of digits is still 1, return directly

      digits[0]
    Copy the code

Since num is just a four-digit number, both time and space complexity can be thought of as O(1).

answer

class Solution(object):
    def minimumSum(self, num):
        """
        :type num: int
        :rtype: int
        """
        digits = []
        for i in str(num):
            digits.append(int(i))
        digits.sort()
        while digits:
            if digits[0] == 0:
                digits.pop(0)
            else:
                break
        N = len(digits)
        if N == 4:
            return digits[0]*10+digits[-1] +digits[1]*10+digits[2]
        elif N == 3:
            return digits[0] * 10 + digits[1] + digits[2]
        elif N == 2:
            return digits[0] + digits[1]
        elif N == 1:
            return digits[0]		
Copy the code

The results

99/99 Test cases passed. Status: Accepted Runtime: 20 MS Memory Usage: 13.4 MBCopy the code

The original link

Leetcode.com/contest/biw…

Your support is my biggest motivation