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 d298d9f

Browse files
1
1 parent 91b3a39 commit d298d9f

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

‎src/main/kotlin/p02xx/Problem0231.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ import util.expect
55
fun main() {
66
class Solution {
77
fun isPowerOfTwo(n: Int): Boolean {
8-
if (n < 0) {
9-
return false
10-
}
11-
return Integer.bitCount(n) == 1
8+
return n.takeIf { it > 0 }?.countOneBits() == 1
129
}
1310
}
1411

‎src/main/kotlin/p33xx/Problem3363.kt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package p33xx
2+
3+
import util.expect
4+
5+
fun main() {
6+
class Solution {
7+
fun maxCollectedFruits(fruits: Array<IntArray>): Int {
8+
val dp = Array(fruits.size) {
9+
IntArray(fruits[it].size)
10+
}
11+
12+
dp[fruits.lastIndex][0] = fruits[fruits.lastIndex][0]
13+
dp[0][fruits.lastIndex] = fruits[0][fruits.lastIndex]
14+
15+
for (r in 1 until fruits.size) {
16+
for (c in maxOf(r + 1, fruits.lastIndex - r) until fruits.size) {
17+
dp[r][c] = fruits[r][c] + maxOf(dp[r - 1][c - 1], dp[r - 1][c], dp[r - 1].getOrNull(c + 1) ?: 0)
18+
dp[c][r] = fruits[c][r] + maxOf(dp[c - 1][r - 1], dp[c][r - 1], dp.getOrNull(c + 1)?.get(r - 1) ?: 0)
19+
}
20+
}
21+
22+
return fruits.indices.sumOf { fruits[it][it] } + dp[fruits.lastIndex][fruits.lastIndex - 1] + dp[fruits.lastIndex - 1][fruits.lastIndex]
23+
}
24+
}
25+
26+
expect {
27+
Solution().maxCollectedFruits(
28+
arrayOf(
29+
intArrayOf(16, 3, 11, 14, 14),
30+
intArrayOf(3, 0, 10, 13, 14),
31+
intArrayOf(7, 18, 8, 7, 18),
32+
intArrayOf(7, 8, 5, 7, 5),
33+
intArrayOf(0, 14, 8, 1, 0),
34+
)
35+
)
36+
}
37+
}

0 commit comments

Comments
(0)

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