Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bb78ba8

Browse files
1
1 parent e760d59 commit bb78ba8

File tree

1 file changed

+17
-42
lines changed

1 file changed

+17
-42
lines changed

‎src/main/kotlin/p06xx/Problem0679.kt‎

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,34 @@
11
package p06xx
22

3-
import kotlin.math.absoluteValue
43
import util.expect
4+
import kotlin.math.absoluteValue
55

66
fun main() {
77
class Solution {
88
fun judgePoint24(cards: IntArray): Boolean {
9-
val dp = Array(0b1111) {
10-
hashSetOf<Double>()
9+
val dp = Array(1 shl 4) { hashSetOf<Double>() }
10+
repeat(4) {
11+
dp[1 shl it] += cards[it].toDouble()
1112
}
1213

13-
fun join(set1: Set<Double>?, set2: Set<Double>?): Set<Double> {
14-
val res = hashSetOf<Double>()
15-
16-
set1?.forEach { s1 ->
17-
set2?.forEach { s2 ->
18-
res.add(s1 + s2)
19-
res.add(s1 - s2)
20-
res.add(s2 - s1)
21-
res.add(s1 * s2)
22-
res.add(s1 / s2)
23-
res.add(s2 / s1)
24-
}
25-
}
26-
27-
return res
28-
}
29-
30-
repeat(0b1111) { num ->
31-
repeat(4) { d ->
32-
if (num and (1 shl d) > 0) {
33-
val target = num - (1 shl d)
34-
35-
if (target == 0) {
36-
dp[num].add(cards[d].toDouble())
37-
} else {
38-
dp[num].addAll(join(dp[1 shl d], dp[target]))
14+
repeat(16) {
15+
for (p in 1..it / 2) {
16+
if (p and it == p) {
17+
dp[p].forEach { s1 ->
18+
dp[it - p].forEach { s2 ->
19+
dp[it].add(s1 + s2)
20+
dp[it].add(s1 - s2)
21+
dp[it].add(s2 - s1)
22+
dp[it].add(s1 * s2)
23+
dp[it].add(s1 / s2)
24+
dp[it].add(s2 / s1)
25+
}
3926
}
4027
}
4128
}
4229
}
4330

44-
return arrayOf(
45-
0b0001 to 0b1110,
46-
0b0010 to 0b1101,
47-
0b0100 to 0b1011,
48-
0b1000 to 0b0111,
49-
0b0011 to 0b1100,
50-
0b0101 to 0b1010,
51-
0b1001 to 0b0110
52-
).any { (l, r) ->
53-
join(dp[l], dp[r]).any {
54-
(it - 24.0).absoluteValue < 0.000001
55-
}
56-
}
31+
return dp[0b1111].any { (it - 24).absoluteValue < .0000001 }
5732
}
5833
}
5934

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /