Skip to content

Commit 5da1912

Browse files
authored
Create 1626-best-team-with-no-conflicts.kt
1 parent cde15a2 commit 5da1912

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
fun bestTeamScore(scores: IntArray, ages: IntArray): Int {
3+
val n = scores.size
4+
5+
val pairs = scores.zip(ages).sortedWith(
6+
compareBy({it.first}, {it.second})
7+
)
8+
9+
val dp = IntArray (n).apply {
10+
for ((i, v) in pairs.withIndex())
11+
this[i] = v.first
12+
}
13+
14+
for (i in 0 until n) {
15+
val (score, age) = pairs[i]
16+
for (j in 0 until i) {
17+
val (score2, age2) = pairs[j]
18+
if (age >= age2) {
19+
dp[i] = maxOf(dp[i], score + dp[j])
20+
}
21+
}
22+
}
23+
24+
return dp.max() ?: 0
25+
}
26+
}

0 commit comments

Comments
 (0)