Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Put Marbles in Bags

XPChallenge Points: 30
levelLevel: Hard

You are given an integer array weights where each element represents the weight of a marble, and an integer k representing how many bags we must divide the marbles into.

The marbles must be divided using these rules:

  1. Every bag must contain at least one marble.

  2. Marbles placed in a bag must form a continuous segment in the original array.
    (If marbles at positions i and j are in the same bag, then all marbles from i to j must also be in that bag.)

  3. Cost of a bag that contains marbles from index i to j is calculated as:

    weights[i] + weights[j]
  4. The total score of a distribution is the sum of costs of all k bags.

Your task is to compute the difference between:

  • The maximum possible total score,

  • The minimum possible total score.

Return this difference.

Example 1:

Input: weights = [2, 4, 7, 1, 3], k = 3

Output: 9

Explanation:

Different valid partitions will produce different scores. The smallest score and largest score differ by 6.

Example 2:

Input: weights = [5, 5, 5], k = 1

Output: 0

Explanation:

There is only one bag, so max and min scores are the same.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result