24 Game - Complete Solution Guide
24 Game is LeetCode problem 679, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given an integer array cards of length 4 . You have four cards, each containing a number in the range [1, 9] . You should arrange the numbers on these cards in a mathematical expression using the operators ['+', '-', '*', '/'] and the parentheses '(' and ')' to get the value 24. You are restricted with the following rules: The division operator '/' represents real division, not integer division. For example, 4 / (1 - 2 / 3) = 4 / (1 / 3) = 12 . Every operation done is between two numbers
Detailed Explanation
The 24 Game problem challenges you to use four given numbers (each between 1 and 9) and the basic arithmetic operations (+, -, *, /) along with parentheses to achieve a final result of 24. The division operator represents real division. Each operation must be between two numbers. Number concatenation is not allowed. The task is to determine if it's possible to reach 24 using the given numbers and operations.
Solution Approach
The solution uses a recursive backtracking approach to explore all possible combinations of numbers and operations. It starts with the four given numbers. In each recursive step, it picks two numbers, applies all possible operations to them, and then recursively calls itself with the remaining numbers (including the result of the operation). When only one number is left, it checks if that number is approximately equal to 24 (within a small epsilon).
Step-by-Step Algorithm
- Step 1: Convert the input integer array to a double array for floating-point operations.
- Step 2: Define a recursive function `solve` that takes a list of numbers as input.
- Step 3: Base case for `solve`: if the list contains only one number, check if its absolute difference with 24 is less than a small epsilon value. Return `true` if it is, `false` otherwise.
- Step 4: Iterate through all possible pairs of numbers in the list.
- Step 5: For each pair, apply all six possible operations (+, -, (a-b), *, /, (a/b), (b/a)) to them, if the divisor is not too close to zero. Calculate the result of each operation.
- Step 6: Create a new list containing the remaining numbers and the result of the current operation.
- Step 7: Recursively call `solve` with the new list.
- Step 8: If the recursive call returns `true`, return `true` immediately. If the base case is not reached or if no valid combination of operations leads to 24, return `false`.
Key Insights
- Insight 1: Backtracking is the core approach. Since there are a limited number of cards (4), we can explore all possible combinations of operations and numbers recursively.
- Insight 2: Floating-point precision is important when dealing with division. Use a small epsilon value to compare floating-point numbers for equality.
- Insight 3: There are only a limited number of permutations and operator combinations to explore, meaning the actual time complexity is acceptable even if it looks high at first glance.
Complexity Analysis
Time Complexity: O(1)
Space Complexity: O(1)
Topics
This problem involves: Array, Math, Backtracking.
Companies
Asked at: Huawei, Roku.