2551. Put Marbles in Bags =>Difficulty: Hard, Rating: 2042
Solution
創一個n-1的Table,用以記錄說要分割的位置在哪,並且會多出多少的Marbles保存下來後,先用Sort將其排序後,再由最末端往前,最前端往後互相扣除,及為所求。
其中因為Head, Tail均包含在min, max中,所以可以不用特別再去算。
class Solution {
public:
long long putMarbles(vector<int>& weights, int k) {
vector<long long> res;
int n = weights.size();