Java:1705:吃苹果的最大数目
This commit is contained in:
parent
043a6b4668
commit
624ab19cde
100
src/main/java/leetcode/editor/cn/MaximumNumberOfEatenApples.java
Normal file
100
src/main/java/leetcode/editor/cn/MaximumNumberOfEatenApples.java
Normal file
@ -0,0 +1,100 @@
|
||||
//有一棵特殊的苹果树,一连 n 天,每天都可以长出若干个苹果。在第 i 天,树上会长出 apples[i] 个苹果,这些苹果将会在 days[i] 天后(也就
|
||||
//是说,第 i + days[i] 天时)腐烂,变得无法食用。也可能有那么几天,树上不会长出新的苹果,此时用 apples[i] == 0 且 days[i] =
|
||||
//= 0 表示。
|
||||
//
|
||||
// 你打算每天 最多 吃一个苹果来保证营养均衡。注意,你可以在这 n 天之后继续吃苹果。
|
||||
//
|
||||
// 给你两个长度为 n 的整数数组 days 和 apples ,返回你可以吃掉的苹果的最大数目。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
// 输入:apples = [1,2,3,5,2], days = [3,2,1,4,2]
|
||||
//输出:7
|
||||
//解释:你可以吃掉 7 个苹果:
|
||||
//- 第一天,你吃掉第一天长出来的苹果。
|
||||
//- 第二天,你吃掉一个第二天长出来的苹果。
|
||||
//- 第三天,你吃掉一个第二天长出来的苹果。过了这一天,第三天长出来的苹果就已经腐烂了。
|
||||
//- 第四天到第七天,你吃的都是第四天长出来的苹果。
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
// 输入:apples = [3,0,0,0,0,2], days = [3,0,0,0,0,2]
|
||||
//输出:5
|
||||
//解释:你可以吃掉 5 个苹果:
|
||||
//- 第一天到第三天,你吃的都是第一天长出来的苹果。
|
||||
//- 第四天和第五天不吃苹果。
|
||||
//- 第六天和第七天,你吃的都是第六天长出来的苹果。
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// apples.length == n
|
||||
// days.length == n
|
||||
// 1 <= n <= 2 * 10⁴
|
||||
// 0 <= apples[i], days[i] <= 2 * 10⁴
|
||||
// 只有在 apples[i] = 0 时,days[i] = 0 才成立
|
||||
//
|
||||
// Related Topics 贪心 数组 堆(优先队列) 👍 60 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.PriorityQueue;
|
||||
|
||||
//Java:1705:吃苹果的最大数目
|
||||
public class MaximumNumberOfEatenApples {
|
||||
public static void main(String[] args) {
|
||||
Solution solution = new MaximumNumberOfEatenApples().new Solution();
|
||||
// TO TEST
|
||||
}
|
||||
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int eatenApples(int[] apples, int[] days) {
|
||||
int ans = 0;
|
||||
PriorityQueue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
|
||||
int n = apples.length;
|
||||
int i = 0;
|
||||
while (i < n) {
|
||||
while (!pq.isEmpty() && pq.peek()[0] <= i) {
|
||||
pq.poll();
|
||||
}
|
||||
int rottenDay = i + days[i];
|
||||
int count = apples[i];
|
||||
if (count > 0) {
|
||||
pq.offer(new int[]{rottenDay, count});
|
||||
}
|
||||
if (!pq.isEmpty()) {
|
||||
int[] arr = pq.peek();
|
||||
arr[1]--;
|
||||
if (arr[1] == 0) {
|
||||
pq.poll();
|
||||
}
|
||||
ans++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
while (!pq.isEmpty()) {
|
||||
while (!pq.isEmpty() && pq.peek()[0] <= i) {
|
||||
pq.poll();
|
||||
}
|
||||
if (pq.isEmpty()) {
|
||||
break;
|
||||
}
|
||||
int[] arr = pq.poll();
|
||||
int curr = Math.min(arr[0] - i, arr[1]);
|
||||
ans += curr;
|
||||
i += curr;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
File diff suppressed because one or more lines are too long
@ -0,0 +1,41 @@
|
||||
<p>有一棵特殊的苹果树,一连 <code>n</code> 天,每天都可以长出若干个苹果。在第 <code>i</code> 天,树上会长出 <code>apples[i]</code> 个苹果,这些苹果将会在 <code>days[i]</code> 天后(也就是说,第 <code>i + days[i]</code> 天时)腐烂,变得无法食用。也可能有那么几天,树上不会长出新的苹果,此时用 <code>apples[i] == 0</code> 且 <code>days[i] == 0</code> 表示。</p>
|
||||
|
||||
<p>你打算每天 <strong>最多</strong> 吃一个苹果来保证营养均衡。注意,你可以在这 <code>n</code> 天之后继续吃苹果。</p>
|
||||
|
||||
<p>给你两个长度为 <code>n</code> 的整数数组 <code>days</code> 和 <code>apples</code> ,返回你可以吃掉的苹果的最大数目<em>。</em></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>apples = [1,2,3,5,2], days = [3,2,1,4,2]
|
||||
<strong>输出:</strong>7
|
||||
<strong>解释:</strong>你可以吃掉 7 个苹果:
|
||||
- 第一天,你吃掉第一天长出来的苹果。
|
||||
- 第二天,你吃掉一个第二天长出来的苹果。
|
||||
- 第三天,你吃掉一个第二天长出来的苹果。过了这一天,第三天长出来的苹果就已经腐烂了。
|
||||
- 第四天到第七天,你吃的都是第四天长出来的苹果。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>apples = [3,0,0,0,0,2], days = [3,0,0,0,0,2]
|
||||
<strong>输出:</strong>5
|
||||
<strong>解释:</strong>你可以吃掉 5 个苹果:
|
||||
- 第一天到第三天,你吃的都是第一天长出来的苹果。
|
||||
- 第四天和第五天不吃苹果。
|
||||
- 第六天和第七天,你吃的都是第六天长出来的苹果。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>apples.length == n</code></li>
|
||||
<li><code>days.length == n</code></li>
|
||||
<li><code>1 <= n <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= apples[i], days[i] <= 2 * 10<sup>4</sup></code></li>
|
||||
<li>只有在 <code>apples[i] = 0</code> 时,<code>days[i] = 0</code> 才成立</li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>贪心</li><li>数组</li><li>堆(优先队列)</li></div></div><br><div><li>👍 60</li><li>👎 0</li></div>
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user