力扣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;
      }
  };
相关推荐
晚霞的不甘2 小时前
Flutter for OpenHarmony 可视化教学:A* 寻路算法的交互式演示
人工智能·算法·flutter·架构·开源·音视频
望舒5132 小时前
代码随想录day25,回溯算法part4
java·数据结构·算法·leetcode
独好紫罗兰3 小时前
对python的再认识-基于数据结构进行-a006-元组-拓展
开发语言·数据结构·python
C++ 老炮儿的技术栈3 小时前
Qt 编写 TcpClient 程序 详细步骤
c语言·开发语言·数据库·c++·qt·算法
KYGALYX3 小时前
逻辑回归详解
算法·机器学习·逻辑回归
铉铉这波能秀3 小时前
LeetCode Hot100数据结构背景知识之集合(Set)Python2026新版
数据结构·python·算法·leetcode·哈希算法
参.商.3 小时前
【Day 27】121.买卖股票的最佳时机 122.买卖股票的最佳时机II
leetcode·golang
踢足球09293 小时前
寒假打卡:2026-2-8
数据结构·算法
IT猿手3 小时前
基于强化学习的多算子差分进化路径规划算法QSMODE的机器人路径规划问题研究,提供MATLAB代码
算法·matlab·机器人
千逐-沐风3 小时前
SMU-ACM2026冬训周报3rd
算法