力扣1705.吃苹果的最大数目
力扣1705.吃苹果的最大数目
-
对组存腐烂时间和数量
- 每次取之前先把腐烂的或没了的弹出
-
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; } };
原文地址:https://blog.csdn.net/Pisasama/article/details/140606577
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!