力扣1705.吃苹果的最大数目
-
对组存腐烂时间和数量
- 每次取之前先把腐烂的或没了的弹出
cpp
class Solution {
typedef pair<int,int> PII;
public:
int eatenApples(vector<int>& apples, vector<int>& days) {
int n = apples.size();
priority_queue<PII,vector<PII>,greater<PII>> q;
int i = 0;
int res=0;
while(i < apples.size() || !q.empty())
{
if(i < apples.size() && apples[i] > 0)
q.push({i+days[i],apples[i]});
while(!q.empty() && (q.top().first <= i || q.top().second == 0))
q.pop();
if(!q.empty())
{
res ++;
int t1 = q.top().first,t2 = q.top().second;
q.pop();
q.push({t1,t2-1});
}
i++;
}
return res;
}
};