kt: add «2551. Put Marbles in Bags»
URL: https://leetcode.com/problems/put-marbles-in-bags/ Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
76d3e0eab1
commit
efff2000aa
1 changed files with 18 additions and 0 deletions
18
kt/put-marbles-in-bags.kt
Normal file
18
kt/put-marbles-in-bags.kt
Normal file
|
@ -0,0 +1,18 @@
|
|||
class Solution {
|
||||
fun putMarbles(
|
||||
weights: IntArray,
|
||||
k: Int,
|
||||
): Long =
|
||||
weights
|
||||
.zip(weights.drop(1))
|
||||
.map { (w1, w2) -> (w1 + w2).toLong() }
|
||||
.sorted()
|
||||
.let { pairs ->
|
||||
(0..k - 2)
|
||||
.map { i ->
|
||||
weights.size - 2 - i to i
|
||||
}.sumOf { (i, j) ->
|
||||
pairs[i] - pairs[j]
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue