sort

wonderful world·2023년 1월 29일

coding

목록 보기
5/9

2551. Put Marbles in Bags

https://leetcode.com/problems/put-marbles-in-bags/description/
https://leetcode.com/contest/weekly-contest-330/problems/put-marbles-in-bags/
from nyu_ldf

class Solution(object):
    def putMarbles(self, weights, k):
        """
        :type weights: List[int]
        :type k: int
        :rtype: int
        """
        v, d = [0] * (len(weights) - 1), 0
        for i in range(len(weights) - 1):
            v[i] = weights[i] + weights[i + 1]
        v.sort()
        for i in range(k - 1):
            d += v[len(weights) - 2 - i] - v[i]
        return d
profile
hello wirld

0개의 댓글