力扣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;
      }
  };
相关推荐
Magnum Lehar1 小时前
vulkan游戏引擎test_manager实现
java·算法·游戏引擎
水蓝烟雨2 小时前
[面试精选] 0094. 二叉树的中序遍历
算法·面试精选
超闻逸事2 小时前
【题解】[UTPC2024] C.Card Deck
c++·算法
暴力求解2 小时前
C++类和对象(上)
开发语言·c++·算法
JKHaaa2 小时前
几种简单的排序算法(C语言)
c语言·算法·排序算法
让我们一起加油好吗2 小时前
【基础算法】枚举(普通枚举、二进制枚举)
开发语言·c++·算法·二进制·枚举·位运算
FogLetter2 小时前
微信红包算法揭秘:从随机性到产品思维的完美结合
算法
YGGP3 小时前
吃透 Golang 基础:数据结构之 Map
开发语言·数据结构·golang
BUG收容所所长3 小时前
二分查找的「左右为难」:如何优雅地找到数组中元素的首尾位置
前端·javascript·算法
weixin_419658313 小时前
数据结构之栈
数据结构