mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 04:31:55 +08:00
Fix the content
This commit is contained in:
@ -17,7 +17,7 @@ public class coin_change_greedy {
|
||||
// 循环进行贪心选择,直到无剩余金额
|
||||
while (amt > 0) {
|
||||
// 找到小于且最接近剩余金额的硬币
|
||||
while (coins[i] > amt) {
|
||||
while (i > 0 && coins[i] > amt) {
|
||||
i--;
|
||||
}
|
||||
// 选择 coins[i]
|
||||
|
||||
@ -15,14 +15,14 @@ public class top_k {
|
||||
Queue<Integer> heap = new PriorityQueue<Integer>();
|
||||
// 将数组的前 k 个元素入堆
|
||||
for (int i = 0; i < k; i++) {
|
||||
heap.add(nums[i]);
|
||||
heap.offer(nums[i]);
|
||||
}
|
||||
// 从第 k+1 个元素开始,保持堆的长度为 k
|
||||
for (int i = k; i < nums.length; i++) {
|
||||
// 若当前元素大于堆顶元素,则将堆顶元素出堆、当前元素入堆
|
||||
if (nums[i] > heap.peek()) {
|
||||
heap.poll();
|
||||
heap.add(nums[i]);
|
||||
heap.offer(nums[i]);
|
||||
}
|
||||
}
|
||||
return heap;
|
||||
|
||||
Reference in New Issue
Block a user