Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2083fb8

Browse files
add 679
1 parent 70e36ed commit 2083fb8

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Your ideas/fixes/algorithms are more than welcome!
2323
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2424
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
2525
|680|[Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_680.java) | O(n) | O(1) | Easy | String
26+
|679|[24 Game](https://leetcode.com/problems/24-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_679.java) | O(n^2) | O(n) | Hard | Recursion
2627
|677|[Map Sum Pairs](https://leetcode.com/problems/map-sum-pairs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_677.java) | O(n) | O(n) | Medium | HashMap
2728
|676|[Implement Magic Dictionary](https://leetcode.com/problems/implement-magic-dictionary/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_676.java) | O(n^2) | O(n) | Medium |
2829
|674|[Longest Continuous Increasing Subsequence](https://leetcode.com/problems/longest-continuous-increasing-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_674.java) | O(n^2) | O(1) | Easy |
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.stream.IntStream;
4+
5+
/**
6+
* 679. 24 Game
7+
*
8+
* You have 4 cards each containing a number from 1 to 9.
9+
* You need to judge whether they could operated through *, /, +, -, (, ) to get the value of 24.
10+
11+
Example 1:
12+
Input: [4, 1, 8, 7]
13+
Output: True
14+
Explanation: (8-4) * (7-1) = 24
15+
16+
Example 2:
17+
Input: [1, 2, 1, 2]
18+
Output: False
19+
20+
Note:
21+
The division operator / represents real division, not integer division. For example, 4 / (1 - 2/3) = 12.
22+
Every operation done is between two numbers.
23+
In particular, we cannot use - as a unary operator. For example, with [1, 1, 1, 1] as input, the expression -1 - 1 - 1 - 1 is not allowed.
24+
You cannot concatenate numbers together. For example, if the input is [1, 2, 1, 2], we cannot write this as 12 + 12.
25+
26+
*/
27+
28+
public class _679 {
29+
public static class Solution1 {
30+
public boolean judgePoint24(int[] nums) {
31+
return dfs(IntStream.of(nums).mapToDouble(num -> num).toArray());
32+
}
33+
34+
private boolean dfs(double[] nums) {
35+
if (nums.length == 1) {
36+
return Math.abs(nums[0] - 24) < 1e-8;//1e-8 means 0.000000001, i.e. 10^(-8)
37+
}
38+
39+
for (int i = 0; i < nums.length; i++) {
40+
for (int j = 0; j < nums.length; j++) {
41+
if (i != j) {
42+
int len = 0;
43+
double[] a = new double[nums.length-1];
44+
for (int k = 0; k < nums.length; k++) {
45+
if (k != i && k != j) {
46+
a[len++] = nums[k];
47+
}
48+
}
49+
50+
a[len] = nums[i] + nums[j];
51+
if (dfs(a)) {
52+
return true;
53+
}
54+
55+
a[len] = nums[i] - nums[j];
56+
if (dfs(a)) {
57+
return true;
58+
}
59+
60+
a[len] = nums[i] * nums[j];
61+
if (dfs(a)) {
62+
return true;
63+
}
64+
65+
if (nums[j] > 1e-8) {
66+
a[len] = nums[i] / nums[j];
67+
if (dfs(a)) {
68+
return true;
69+
}
70+
}
71+
}
72+
}
73+
}
74+
return false;
75+
}
76+
}
77+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._679;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _679Test {
10+
private static _679.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _679.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(true, solution1.judgePoint24(new int[]{4,1,8,7}));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(false, solution1.judgePoint24(new int[]{1,2,1,2}));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
// 8 / (1 - 2/3) = 24
30+
assertEquals(true, solution1.judgePoint24(new int[]{1,2,3,8}));
31+
}
32+
33+
@Test
34+
public void test4() {
35+
assertEquals(true, solution1.judgePoint24(new int[]{1,3,4,6}));
36+
}
37+
38+
@Test
39+
public void test5() {
40+
assertEquals(true, solution1.judgePoint24(new int[]{1,9,1,2}));
41+
}
42+
43+
}

0 commit comments

Comments
 (0)