力扣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;
      }
  };
相关推荐
血小板要健康13 小时前
队列 + 宽搜(BFS):二叉树层序遍历 算法总结
java·数据结构·笔记·算法·leetcode·宽度优先
Felven13 小时前
A. Riptide
算法·c 算法
m0_7393128713 小时前
四元数、李群SO(3)/李代数so(3)的作用及应用场景
算法·机器人·自动驾驶
artificiali14 小时前
880 第4章多元
人工智能·算法
Felven14 小时前
B. Deja Vu
数据结构·算法
小玮看世界14 小时前
[Python]螺旋遍历 vs 最短路径:方向控制类算法的“同源异流”
开发语言·python·算法
月华路14 小时前
《模型不玄学》第14章 标签、损失与样本权重
人工智能·算法·机器学习
黄金龙PLUS14 小时前
SPARKLE置换算法的优缺点
算法·网络安全·密码学·哈希算法·同态加密
不会就选b14 小时前
算法日常・每日刷题--<贪心>3
数据结构·算法·leetcode
ysu_031414 小时前
03-双链表与循环链表
c语言·数据结构·链表