Leetcode 679. 24 point game | Python

Time:2020-10-22

679. 24 point game


Source: leetcode https://leetcode-cn.com/problems/24-game

subject


You have four cards with numbers 1 to 9. You need to determine if you can get 24 by the operation of *, /, +, -, (,).

Example 1:

Input: [4,1,8,7]
Output: true
Explanation: (8-4) * (7-1) = 24

Example 2:

Input: [1,2,1,2]
Output: false

be careful:

  • Division operator / represents real division, not integer division. For example, 4 / (1 – 2 / 3) = 12.
  • Each operator operates on two numbers. In particular, we can’t use – as a unary operator. For example, the expression – 1 – 1 – 1 – 1 is not allowed when [1, 1, 1, 1] is used as input.
  • You can’t connect numbers together. For example, when the input is [1,2,1,2], it cannot be written as 12 + 12.

Thinking of problem solving


Idea: backtracking

Let’s leave this question aside. Let’s first say that we usually play 24 point games in the following order:

  • Select two numbers and perform four operations;
  • Then, from the calculation results and the remaining two numbers, select two numbers again, and then carry out four operations to get the results;
  • In the end, there are two numbers left to do four operations to see if the final result is equal to 24.

Now look at this question, we also use the above form of enumeration, to try all possible combinations.

As an additional note, parentheses’ (‘,’) appear in the example, where brackets have priority. But here, we do not deal with it, because this situation is included in the enumeration.

There is one more thing to note. In the title, the division operation is a real number division, not an integer division, then the accuracy problem will be involved here. By default, as long as the final result error is less than 1e-6, it is considered equal. The divisor cannot be 0. If the absolute value is less than 1e-6, it is considered to be equal to 0.

We know that in the four operations, addition and multiplication conform to the commutative law. Then, in these two operations, the position of the two numbers before and after does not affect the result, so we can consider skipping one of them.

The specific codes are as follows.

code implementation


class Solution:
    def judgePoint24(self, nums: List[int]) -> bool:
        def helper(nums):
            if not nums:
                return False
            if len(nums) == 1:
                return abs(nums[0] - 24) < 0.000001

            for i in range(len(nums)):
                for j in range(len(nums)):
                    #You cannot select numbers in the same location
                    if i == j:
                        continue
                    #Is the final result of the mark equal to 24
                    status = False

                    x = nums[i]
                    y = nums[j]

                    #Select the number corresponding to I, j to calculate, and put other numbers into the array for subsequent calculation
                    new_nums = [nums[k] for k in range(len(nums)) if( k!=i and k!=j)]
                    #Four operations are performed. The addition and multiplication conform to the commutative law. A + B = B + A, a * b = b * a, one of which can be skipped
                    #Here, I > J part is skipped by default, or short-circuit statement
                    #Addition, multiplication
                    if i < j:
                        status = status or helper(new_nums + [x+y])
                        status = status or helper(new_nums + [x*y])
                    #Subtraction
                    status = status or helper(new_nums + [x-y])
                    #Division
                    if abs(y) > 0.000001:
                        status = status or helper(new_nums + [x/y])

                    if status:
                        return True
            return False
        return helper(nums)

Achieving results


Leetcode 679. 24 point game | Python

Welcome to our attention


The official account [book collection]

Recommended Today

[jQuery] principle and initial code of rotation chart

JQuery rotation chart <!DOCTYPE html> <html lang=”en”> <head> <meta charset=”UTF-8″> <title>Title</title> <style> *{ margin: 0; padding: 0; } ul{ list-style: none; } .carousel-box{ width: 670px; height: 240px; position: relative; margin: 0 auto; } .carousel-box img{ width: 670px; height: 240px; position: absolute; top: 0px; left: 0px; } .carousel-box ul{ position: absolute; left: 50%; margin-left: -78px; bottom: […]