From a49c10969d7bbeb9afd4aa4a6d033d5501de1884 Mon Sep 17 00:00:00 2001 From: Won Joon Thomas Choi <113500771+724thomas@users.noreply.github.com> Date: Sun, 1 Nov 2026 15:46:07 +0900 Subject: [PATCH] Create 2551. Put Marbles in Bags.py --- .../2551. Put Marbles in Bags.py" | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 "leetcode3/\354\265\234\354\233\220\354\244\200/2551. Put Marbles in Bags.py" diff --git "a/leetcode3/\354\265\234\354\233\220\354\244\200/2551. Put Marbles in Bags.py" "b/leetcode3/\354\265\234\354\233\220\354\244\200/2551. Put Marbles in Bags.py" new file mode 100644 index 00000000..c5bc9269 --- /dev/null +++ "b/leetcode3/\354\265\234\354\233\220\354\244\200/2551. Put Marbles in Bags.py" @@ -0,0 +1,16 @@ +class Solution: + def putMarbles(self, weights: list[int], k: int) -> int: + n = len(weights) + pairs = [] + + for i in range(n-1): + pairs.append(weights[i] + weights[i+1]) + + pairs.sort() + min_value = 0 + max_value = 0 + + for i in range(k-1): + min_value += pairs[i] + max_value += pairs[-i-1] + return max_value - min_value