Skip to content

Commit b48cd44

Browse files
committed
leetcode
1 parent 2c92d3c commit b48cd44

File tree

4 files changed

+210
-0
lines changed

4 files changed

+210
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
3+
4+
5+
*- 1833. Maximum Ice Cream Bars -*
6+
7+
It is a sweltering summer day, and a boy wants to buy some ice cream bars.
8+
9+
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.
10+
11+
Return the maximum number of ice cream bars the boy can buy with coins coins.
12+
13+
Note: The boy can buy the ice cream bars in any order.
14+
15+
16+
17+
Example 1:
18+
19+
Input: costs = [1,3,2,4,1], coins = 7
20+
Output: 4
21+
Explanation: The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7.
22+
Example 2:
23+
24+
Input: costs = [10,6,8,7,7,8], coins = 5
25+
Output: 0
26+
Explanation: The boy cannot afford any of the ice cream bars.
27+
Example 3:
28+
29+
Input: costs = [1,6,3,1,2,5], coins = 20
30+
Output: 6
31+
Explanation: The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18.
32+
33+
34+
Constraints:
35+
36+
costs.length == n
37+
1 <= n <= 105
38+
1 <= costs[i] <= 105
39+
1 <= coins <= 108
40+
41+
*/
42+
43+
class A {
44+
int maxIceCream(List<int> costs, int coins) {
45+
costs.sort();
46+
int i = 0;
47+
while (i < costs.length && coins >= costs[i]) {
48+
coins -= costs[i++];
49+
}
50+
return i;
51+
}
52+
}
53+
54+
class B {
55+
int maxIceCream(List<int> costs, int coins) {
56+
int ans = 0;
57+
costs.sort();
58+
for (int cost in costs) {
59+
coins -= cost;
60+
if (coins < 0) {
61+
break;
62+
}
63+
ans++;
64+
}
65+
return ans;
66+
}
67+
}
68+
69+
class C {
70+
int maxIceCream(List<int> costs, int coins) {
71+
int n = costs.length;
72+
int ans = 0;
73+
costs.sort();
74+
for (int i = 0; i < n; i++) {
75+
if (ans + costs[i] > coins) return i;
76+
ans += costs[i];
77+
}
78+
return costs.length;
79+
}
80+
}
81+
/*
82+
83+
*/
84+
85+
class D {
86+
int maxIceCream(List<int> costs, int coins) {
87+
costs.sort();
88+
int result = 0;
89+
int sum = 0;
90+
for (int cost in costs) {
91+
sum += cost;
92+
if (sum > coins) {
93+
return result;
94+
}
95+
result++;
96+
if (sum == coins) {
97+
return result;
98+
}
99+
}
100+
return result;
101+
}
102+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import "sort"
4+
5+
// func maxIceCream(costs []int, coins int) int {
6+
// sort.Ints(costs)
7+
// var size int = len(costs)
8+
// var answer int = 0
9+
// for i := 0; i < size; i++ {
10+
// if answer+costs[i] > coins {
11+
// return i
12+
// }
13+
// answer += costs[i]
14+
// }
15+
// return len(costs)
16+
// }
17+
func maxIceCream(costs []int, coins int) int {
18+
sort.Ints(costs)
19+
var result int = 0
20+
var sum int = 0
21+
for _, cost := range costs {
22+
sum += cost
23+
if sum > coins {
24+
return result
25+
}
26+
result++
27+
if sum == coins {
28+
return result
29+
}
30+
}
31+
return result
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# 🔥 4 Approaches 🔥 || Simple Fast and Easy || with Explanation
2+
3+
## Solution - 1
4+
5+
```dart
6+
class Solution {
7+
int maxIceCream(List<int> costs, int coins) {
8+
costs.sort();
9+
int i = 0;
10+
while (i < costs.length && coins >= costs[i]) {
11+
coins -= costs[i++];
12+
}
13+
return i;
14+
}
15+
}
16+
```
17+
18+
## Solution - 2
19+
20+
```dart
21+
class Solution {
22+
int maxIceCream(List<int> costs, int coins) {
23+
int ans = 0;
24+
costs.sort();
25+
for (int cost in costs) {
26+
coins -= cost;
27+
if (coins < 0) {
28+
break;
29+
}
30+
ans++;
31+
}
32+
return ans;
33+
}
34+
}
35+
```
36+
37+
## Solution - 3
38+
39+
```dart
40+
class Solution {
41+
int maxIceCream(List<int> costs, int coins) {
42+
int n = costs.length;
43+
int ans = 0;
44+
costs.sort();
45+
for (int i = 0; i < n; i++) {
46+
if (ans + costs[i] > coins) return i;
47+
ans += costs[i];
48+
}
49+
return costs.length;
50+
}
51+
}
52+
```
53+
54+
## Solution - 4
55+
56+
```dart
57+
class Solution {
58+
int maxIceCream(List<int> costs, int coins) {
59+
costs.sort();
60+
int result = 0;
61+
int sum = 0;
62+
for (int cost in costs) {
63+
sum += cost;
64+
if (sum > coins) {
65+
return result;
66+
}
67+
result++;
68+
if (sum == coins) {
69+
return result;
70+
}
71+
}
72+
return result;
73+
}
74+
}
75+
```

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
178178
- [**944.** Delete Columns to Make Sorted](DeleteColumnsToMakeSorted/delete_columns_to_make_sorted.dart)
179179
- [**2244.** Minimum Rounds to Complete All Tasks](MinimumRoundsToCompleteAllTasks/minimum_rounds_to_complete_all_tasks.dart)
180180
- [**452.** Minimum Number of Arrows to Burst Balloons](MinimumNumberOfArrowsToBurstBalloons/minimum_number_of_arrows_to_burst_balloons.dart)
181+
- [**1833.** Maximum Ice Cream Bars](MaximumIceCreamBars/maximum_ice_cream_bars.dart)
181182

182183
## Reach me via
183184

0 commit comments

Comments
 (0)