package leetcode;

/**
 * https://leetcode.com/problems/maximum-ice-cream-bars/
 */
public class Problem1833 {
    public int maxIceCream(int[] costs, int coins) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem1833 prob = new Problem1833();
        System.out.println(prob.maxIceCream(new int[]{1,3,2,4,1}, 7)); // 4
        System.out.println(prob.maxIceCream(new int[]{10,6,8,7,7,8}, 5)); // 0
        System.out.println(prob.maxIceCream(new int[]{1,6,3,1,2,5}, 20)); // 6
    }
}

package leetcode;

import java.util.Arrays;

/**
 * https://leetcode.com/problems/maximum-ice-cream-bars/
 */
public class Problem1833 {
    public int maxIceCream(int[] costs, int coins) {
        int answer = 0;
        int currentCoins = coins;
        Arrays.sort(costs);
        for (int cost : costs) {
            if (currentCoins - cost >= 0) {
                currentCoins -= cost;
                answer++;
            } else {
                break;
            }
        }
        return answer;
    }
}

Solve problem 1833