力扣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;
      }
  };
相关推荐
smj2302_796826528 小时前
解决leetcode第3943题递增后的数对数量
数据结构·python·算法·leetcode
炽烈小老头9 小时前
【每天学习一点算法 2026/05/25】矩阵中的最长递增路径
学习·算法·矩阵
叁散10 小时前
实验报告:5G 仿真环境与基本链路模拟
算法
从负无穷开始的三次元代码生活11 小时前
算法零碎灵感点分享
算法
梓䈑11 小时前
【算法题攻略】快速排序 和 归并排序
数据结构·c++·排序算法
染指111011 小时前
9.LangChain框架(实现RAG)
数据库·人工智能·算法·机器学习·ai·大模型
大数据三康11 小时前
在spyder进行的遗传算法练习
开发语言·python·算法
Gene_202211 小时前
轮式底盘的微分平坦
算法
医用门11 小时前
医院用门一线品牌
leetcode
吴佳浩12 小时前
现代多模态大模型的核心基础:Unified Self-Attention
人工智能·算法·llm