-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path1833_max_icecream_bars.ts
55 lines (40 loc) · 1.43 KB
/
1833_max_icecream_bars.ts
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
/*
It is a sweltering summer day, and a boy wants to buy some ice cream bars.
At the store, there are n ice cream bars. You are given an array costs of length n, where costs[i] is the price of the ith ice cream bar in coins. The boy initially has coins coins to spend, and he wants to buy as many ice cream bars as possible.
Note: The boy can buy the ice cream bars in any order.
Return the maximum number of ice cream bars the boy can buy with coins coins.
You must solve the problem by counting sort.
*/
/*
Greedy algorithm problem
*/
function countSort(arr: number[]): number[] {
let max: number = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] > max) max = arr[i];
}
const sortedArr: number[] = Array(max + 1).fill(0);
for (let i = 0; i < arr.length; i++) {
sortedArr[arr[i]] += 1;
}
return sortedArr;
}
function maxIceCream(costs: number[], coins: number): number {
let iceCreams = 0;
const sortedCosts = countSort(costs);
let i = 0;
while (i < sortedCosts.length && coins > 0) {
if (coins < sortedCosts[i] * i) {
iceCreams += Math.floor(coins / i);
break;
}
coins -= sortedCosts[i] * i;
iceCreams += sortedCosts[i];
i++;
}
return iceCreams;
}
console.log(maxIceCream([1, 3, 2, 4, 1], 7)); // 4
console.log(maxIceCream([10, 6, 8, 7, 7, 8], 5)); // 0
console.log(maxIceCream([1, 6, 3, 1, 2, 5], 20)); // 6
console.log(maxIceCream([4, 7, 6, 4, 4, 2, 2, 4, 8, 8], 41)); // 9