-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path24-game.java
More file actions
57 lines (47 loc) · 1.84 KB
/
Copy path24-game.java
File metadata and controls
57 lines (47 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Solution {
private static final double TARGET = 24.0;
private static final double EPSILON = 1e-6;
public boolean judgePoint24(int[] cards) {
List<Double> nums = new ArrayList<>();
for (int card : cards) {
nums.add((double) card);
}
return solve(nums);
}
private boolean solve(List<Double> nums) {
// Base Case: If only one number left, check if it's close to 24
if (nums.size() == 1) {
return Math.abs(nums.get(0) - TARGET) < EPSILON;
}
for (int i = 0; i < nums.size(); i++) {
for (int j = 0; j < nums.size(); j++) {
if (i == j) continue;
double num1 = nums.get(i);
double num2 = nums.get(j);
// Create a new list with the remaining numbers (excluding num1 and num2)
List<Double> nextNums = new ArrayList<>();
for (int k = 0; k < nums.size(); k++) {
if (k != i && k != j) {
nextNums.add(nums.get(k));
}
}
nextNums.add(num1 + num2);
if (solve(nextNums)) return true;
nextNums.remove(nextNums.size() - 1);
nextNums.add(num1 - num2);
if (solve(nextNums)) return true;
nextNums.remove(nextNums.size() - 1);
nextNums.add(num1 * num2);
if (solve(nextNums)) return true;
nextNums.remove(nextNums.size() - 1);
// avoid division by zero
if (Math.abs(num2) > EPSILON) {
nextNums.add(num1 / num2);
if (solve(nextNums)) return true;
nextNums.remove(nextNums.size() - 1);
}
}
}
return false;
}
}