力扣1354.多次求和构造目标数组
-
优先队列
- 倒着把数组推回全是1的状态
- 每次取最大数执行操作
- 减掉其他数之和
-
class Solution { public: bool isPossible(vector<int>& target) { long long sum = 0; priority_queue<long long> q; for(int ev:target) { sum += ev; q.push(ev); } while(q.top() != 1) { //取出最大数 long long curMax = q.top(); q.pop(); //其他数之和 long long otherSum = sum - curMax; //如果其他数之和不能+1变成curmax if(curMax - otherSum < 1 || otherSum == 0) return false; //取余数,能减几个减几个 long long temp = curMax % otherSum; //留一个 if(temp == 0) temp = otherSum; q.push(temp); //更新当前和 sum = sum - curMax + temp; } return true; } };