Sweets Eating
You are given n sweets with sugar concentrations described by an integer array penalty. A person can eat at most m sweets per day. The sugar penalty for eating the ith sweet on the dth day is calculated as d * penalty[i]. The person needs to eat exactly k sweets. Your task is to determine the minimum total sugar penalty for each k from 1 to n.
Return an array where the ith element is the answer for k = i.
Example 1:
Input: penalty = [3, 1, 4, 2, 5], m = 2
Output: [1, 3, 7, 13, 22]
Explanation:
First, we sort the sweets by penalty: [1, 2, 3, 4, 5]. To minimize the penalty, we eat the sweetest (highest penalty) items on Day 1, the next highest on Day 2, and so on.
- k = 1: Eat [1]. Day 1: 1. Penalty is 1 * 1 = 1.
- k = 2: Eat [1, 2]. Day 1: 1, 2. Penalty is 1 * 1 + 1 * 2 = 3.
- k = 3: Eat [1, 2, 3]. Day 1: 2, 3. Day 2: 1. Penalty is 1 * 2 + 1 * 3 + 2 * 1 = 7.
- k = 4: Eat [1, 2, 3, 4]. Day 1: 3, 4. Day 2: 1, 2. Penalty is 1 * 3 + 1 * 4 + 2 * 1 + 2 * 2 = 13.
- k = 5: Eat [1, 2, 3, 4, 5]. Day 1: 4, 5. Day 2: 2, 3. Day 3: 1. Penalty is 1 * 4 + 1 * 5 + 2 * 2 + 2 * 3 + 3 * 1 = 22.
Example 2:
Input: penalty = [5, 3, 6, 2], m = 2
Output: [2, 5, 12, 21]
Explanation:
Sorted penalty is [2, 3, 5, 6].
- k = 1: Penalty is 1 * 2 = 2.
- k = 2: Day 1: 2, 3. Penalty is 1 * 2 + 1 * 3 = 5.
- k = 3: Day 1: 3, 5. Day 2: 2. Penalty is 1 * 3 + 1 * 5 + 2 * 2 = 12.
- k = 4: Day 1: 5, 6. Day 2: 2, 3. Penalty is 1 * 5 + 1 * 6 + 2 * 2 + 2 * 3 = 21.
Now Your Turn!
Pick the correct output for the given inputInput: penalty = [7, 2, 4, 6, 5, 3], m = 3
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 ≤ m ≤ n ≤ 2 * 105
- 1 ≤ penalty[i] ≤ 2 * 105