1
1
# Time: O(n^3 * 4^n) = O(1), n = 4
2
2
# Space: O(n^2) = O(1)
3
3
4
- # You have 4 cards each containing a number from 1 to 9.
5
- # You need to judge whether they could operated through *, /, +, -, (, ) to get the value of 24.
6
- #
7
- # Example 1:
8
- # Input: [4, 1, 8, 7]
9
- # Output: True
10
- # Explanation: (8-4) * (7-1) = 24
11
- # Example 2:
12
- # Input: [1, 2, 1, 2]
13
- # Output: False
14
- # Note:
15
- # The division operator / represents real division, not integer division.
16
- # For example, 4 / (1 - 2/3) = 12.
17
- # Every operation done is between two numbers. In particular,
18
- # we cannot use - as a unary operator.
19
- # For example, with [1, 1, 1, 1] as input,
20
- # the expression -1 - 1 - 1 - 1 is not allowed.
21
- # You cannot concatenate numbers together. For example,
22
- # if the input is [1, 2, 1, 2], we cannot write this as 12 + 12.
23
-
24
4
from operator import add , sub , mul , truediv
25
5
from fractions import Fraction
26
6
29
9
except NameError :
30
10
xrange = range # Python 3
31
11
32
-
33
12
class Solution (object ):
34
13
def judgePoint24 (self , nums ):
35
14
"""
@@ -54,9 +33,6 @@ def judgePoint24(self, nums):
54
33
next_nums .pop ()
55
34
return False
56
35
57
-
58
- # Time: O(n^3 * 4^n) = O(1), n = 4
59
- # Space: O(n^2) = O(1)
60
36
class Solution2 (object ):
61
37
def judgePoint24 (self , nums ):
62
38
"""
@@ -84,3 +60,4 @@ def dfs(nums):
84
60
return False
85
61
86
62
return dfs (map (Fraction , nums ))
63
+
0 commit comments