[
dfs
backtracking
math
]
BinarySearch 0218 24
Problem statement
https://binarysearch.com/problems/24/
Solution
Almost the same as Leetcode 0679 24 Game, but here division is integet and we can not change order of nums.
Complexity
See complexity analysis of leetcode problem.
Code
class Solution:
def solve(self, nums):
def helper(nums):
if len(nums) == 1: return nums
result = []
for i in range(1, len(nums)):
left = helper(nums[:i])
right = helper(nums[i:])
for x, y in product(left, right):
if y != 0: result += [x+y, x-y, x*y, x//y]
else: result += [x, 0]
return result
return 24 in helper(nums)