0
0

More than 1 year has passed since last update.

Leetcode 1833. Maximum Ice Cream Bars

Last updated at Posted at 2023-01-06

難易度

Medium

アプローチ

Brute-force

class Solution {
    public int maxIceCream(int[] costs, int coins) {
        Arrays.sort(costs);
        int result = 0;
        for (int c : costs) {
            coins -= c;
            if (coins < 0) {
                break;
            }
            result++;
        }
        return result;
    }
    
/*  WA
    public int maxIceCream_WA(int[] costs, int coins) {
        Arrays.sort(costs);

        int count = 0;
        int cost = 0;
        for (int c : costs) {
            if (cost < coins && c < coins) {
                cost += c;
                count++;
            }
        }
        return count;

    }
*/
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0