JavaInterview JavaInterview
首页
指南
分类
标签
归档
  • CSDN (opens new window)
  • 文档集合 (opens new window)
  • 系统架构 (opens new window)
  • 微信号 (opens new window)
  • 公众号 (opens new window)

『Java面试+Java学习』
首页
指南
分类
标签
归档
  • CSDN (opens new window)
  • 文档集合 (opens new window)
  • 系统架构 (opens new window)
  • 微信号 (opens new window)
  • 公众号 (opens new window)
  • 指南
  • 简历

  • Java

  • 面试

  • 算法

  • algorithm
  • leetcode
JavaInterview.cn
2025-06-09
目录

1833. 雪糕的最大数量Java

文章发布较早,内容可能过时,阅读注意甄别。

# 题目

夏日炎炎,小男孩 Tony 想买一些雪糕消消暑。

商店中新到 n 支雪糕,用长度为 n 的数组 costs 表示雪糕的定价,其中 costs[i] 表示第 i 支雪糕的现金价格。Tony 一共有 coins 现金可以用于消费,他想要买尽可能多的雪糕。

注意:Tony 可以按任意顺序购买雪糕。

给你价格数组 costs 和现金量 coins ,请你计算并返回 Tony 用 coins 现金能够买到的雪糕的 最大数量 。

你必须使用计数排序解决此问题。

示例 1:

输入:costs = [1,3,2,4,1], coins = 7
输出:4
解释:Tony 可以买下标为 0、1、2、4 的雪糕,总价为 1 + 3 + 2 + 1 = 7

示例 2:

输入:costs = [10,6,8,7,7,8], coins = 5
输出:0
解释:Tony 没有足够的钱买任何一支雪糕。

示例 3:

输入:costs = [1,6,3,1,2,5], coins = 20
输出:6
解释:Tony 可以买下所有的雪糕,总价为 1 + 6 + 3 + 1 + 2 + 5 = 18 。

提示:

  • costs.length == n
  • 1 <= n <= 105
  • 1 <= costs[i] <= 105
  • 1 <= coins <= 108

# 思路

heapSort

# 解法

class Solution {
    public int maxIceCream(int[] costs, int coins) {
        Heap heap = new Heap(costs);
        heap.create();
        heap.heapSort();
        int count = 0;
        for (int x : costs) {
            if (coins >= x) {
                coins -= x;
            } else {
                break;
            }
            count++;
        }
        return count;
    }
    class Heap {
        int[] heap;
        int heapSize;
        Heap(int[] heap) {
            this.heap = heap;
            this.heapSize = heap.length;
        }
        private void create() {
            for (int i = heapSize / 2 - 1; i >= 0; i --) {
                maxify(i);
            }
        }
        private void maxify(int i) {
            int l = 2 * i + 1;
            int r = 2 * i + 2;
            int largest;
            if (l < heapSize && heap[l] > heap[i]) {
                largest = l;
            } else {
                largest = i;
            }
            if (r < heapSize && heap[r] > heap[largest]) {
                largest = r;
            }
            if (largest == i) {
                return;
            }
            int tmp = heap[i];
            heap[i] = heap[largest];
            heap[largest] = tmp;
            maxify(largest);
        }
        private void heapSort(){
            for(int i = 0; i < heap.length; i ++) {
                //执行n次,将每个当前最大的值放到堆末尾
                int tmp = heap[0];
                heap[0] = heap[heapSize-1];
                heap[heapSize-1] = tmp;
                heapSize --;
                maxify(0);
            }
        }
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

# 总结

  • 分析出几种情况,然后分别对各个情况实现
微信 支付宝
#Java
最近更新
01
1644.测试路径 Java
02-25
02
1888. 使二进制字符串字符交替的最少反转次数 Java
02-25
03
1890. 2020年最后一次登录 Java
02-25
更多文章>
Theme by Vdoing | Copyright © 2019-2026 JavaInterview.cn
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式