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 6b60b86

Browse files
1
1 parent 2277396 commit 6b60b86

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package p34xx
2+
3+
import util.expect
4+
import java.util.*
5+
6+
fun main() {
7+
class Solution {
8+
fun maxSubarrays(n: Int, conflictingPairs: Array<IntArray>): Long {
9+
val ends = TreeMap<Int, Int>()
10+
11+
conflictingPairs.forEach {
12+
if (it[0] > it[1]) {
13+
val (e, s) = it
14+
it[0] = s
15+
it[1] = e
16+
}
17+
ends[it[1]] = (ends[it[1]] ?: 0) + 1
18+
}
19+
20+
conflictingPairs.sortBy { it[0] }
21+
22+
var index = 0
23+
var result = 0L
24+
25+
val save = hashMapOf<Int, Long>()
26+
for (start in 1..n) {
27+
val end = ends.firstEntry()?.key ?: (n + 1)
28+
result += end - start
29+
30+
if (ends[end] == 1) {
31+
save[end] = (save[end] ?: 0) + (ends.higherKey(end) ?: (n + 1)) - end
32+
}
33+
34+
while (index < conflictingPairs.size && conflictingPairs[index][0] == start) {
35+
conflictingPairs[index++][1].also {
36+
ends[it]?.also { c ->
37+
if (c == 1) {
38+
ends.remove(it)
39+
} else {
40+
ends[it] = c - 1
41+
}
42+
}
43+
}
44+
}
45+
}
46+
47+
return result + save.values.max()
48+
}
49+
}
50+
51+
expect {
52+
Solution().maxSubarrays(
53+
4, arrayOf(
54+
intArrayOf(2, 3),
55+
intArrayOf(1, 4),
56+
)
57+
)
58+
}
59+
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package p34xx
2+
3+
import util.expect
4+
5+
fun main() {
6+
class Solution {
7+
fun maxSum(nums: IntArray): Int {
8+
return nums.toSet().filter { it > 0 }.takeIf { it.isNotEmpty() }?.sum() ?: nums.max()
9+
}
10+
}
11+
12+
expect {
13+
Solution().maxSum(
14+
intArrayOf()
15+
)
16+
}
17+
}

0 commit comments

Comments
(0)

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