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 a26a58e

Browse files
1
1 parent b4033dc commit a26a58e

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

‎src/main/kotlin/p34xx/Problem3479.kt

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package p34xx
2+
3+
import util.expect
4+
5+
fun main() {
6+
class Solution {
7+
fun numOfUnplacedFruits(fruits: IntArray, baskets: IntArray): Int {
8+
class SegNode(val left: Int, val right: Int) {
9+
var max: Int = 0
10+
11+
val children by lazy {
12+
arrayOf(SegNode(left, (left + right) / 2), SegNode((left + right) / 2 + 1, right))
13+
}
14+
15+
fun mark(index: Int, value: Int) {
16+
if (index < left || index > right) {
17+
return
18+
}
19+
20+
max = maxOf(max, value)
21+
22+
if (left < right) {
23+
children.forEach {
24+
it.mark(index, value)
25+
}
26+
}
27+
}
28+
29+
fun remove(value: Int): Int? {
30+
if (value > max) {
31+
return null
32+
}
33+
34+
if (left == right) {
35+
max = -1
36+
return left
37+
} else {
38+
val result = children[0].remove(value) ?: children[1].remove(value)
39+
40+
max = children.maxOf { it.max }
41+
42+
return result
43+
}
44+
}
45+
}
46+
47+
val root = SegNode(0, baskets.lastIndex)
48+
49+
baskets.forEachIndexed { index, b ->
50+
root.mark(index, b)
51+
}
52+
53+
var result = 0
54+
55+
fruits.forEach { f ->
56+
root.remove(f) ?: result++
57+
}
58+
59+
return result
60+
}
61+
}
62+
63+
expect {
64+
Solution().numOfUnplacedFruits(
65+
intArrayOf(0, 17), intArrayOf(14, 19)
66+
)
67+
}
68+
}

0 commit comments

Comments
(0)

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