Leetcode Problem 2551. Put Marbles in Bags

2551. Put Marbles in Bags

Leetcode Solutions

Key approach of the solution

Algorithm

  1. Initialize an array pairWeights to store the sum of every adjacent pair in weights.
  2. Sort the pairWeights array in non-decreasing order.
  3. Calculate the sum of the k - 1 largest elements in pairWeights to get the maximum score.
  4. Calculate the sum of the k - 1 smallest elements in pairWeights to get the minimum score.
  5. Return the difference between the maximum score and the minimum score as the answer.
UML Thumbnail

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...