力扣1705.吃苹果的最大数目

力扣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;
      }
  };
相关推荐
cc.ChenLy39 分钟前
算法从入门到精通实战指南
算法
Jerry7 小时前
LeetCode 160. 相交链表
算法
Jerry8 小时前
LeetCode 19. 删除链表的倒数第 N 个结点
算法
金銀銅鐵8 小时前
费马小定理
python·数学·算法
技术不好的崎鸣同学9 小时前
[ACTF2020 新生赛]Exec 思路及解法
算法·安全·web安全
Full Stack Developme10 小时前
Java LRU 与 LFU 算法及应用
java·开发语言·算法
Jerry11 小时前
LeetCode 707. 设计链表
算法
疋瓞12 小时前
python和C++对比(1)_数据类型和数据结构
数据结构·c++·python
C语言小火车12 小时前
C++ 堆排序深度精讲:基于完全二叉树的选择排序进化,最坏情况 O(n log n) 的稳定王者
开发语言·c++·算法·排序算法·堆排序