力扣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;
      }
  };
相关推荐
ambition202422 分钟前
从暴力搜索到理论最优:一道任务调度问题的完整算法演进历程
c语言·数据结构·c++·算法·贪心算法·深度优先
cmpxr_4 分钟前
【C】原码和补码以及环形坐标取模算法
c语言·开发语言·算法
qiqsevenqiqiqiqi4 分钟前
前缀和差分
算法·图论
代码旅人ing13 分钟前
链表算法刷题指南
数据结构·算法·链表
Yungoal18 分钟前
常见 时间复杂度计算
c++·算法
6Hzlia25 分钟前
【Hot 100 刷题计划】 LeetCode 48. 旋转图像 | C++ 矩阵变换题解
c++·leetcode·矩阵
不爱吃炸鸡柳1 小时前
单链表专题(完整代码版)
数据结构·算法·链表
CylMK2 小时前
题解:AT_abc382_d [ABC382D] Keep Distance
算法
Dfreedom.2 小时前
计算机视觉全景图
人工智能·算法·计算机视觉·图像算法
Morwit2 小时前
【力扣hot100】 1. 两数之和
数据结构·c++·算法·leetcode·职场和发展