Skip to content

Commit 52703a9

Browse files
committed
feat: add solutions to lc problem: No. 1833.Maximum Ice Cream Bars
1 parent 7c86f4c commit 52703a9

File tree

5 files changed

+108
-2
lines changed

5 files changed

+108
-2
lines changed

solution/1800-1899/1833.Maximum Ice Cream Bars/README.md

+38-1
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,59 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
注意数据范围,题目很容易误导我们使用01背包(会超时),其实这题就是简单贪心,优先选择定价小的雪糕。
57+
5658
<!-- tabs:start -->
5759

5860
### **Python3**
5961

6062
<!-- 这里可写当前语言的特殊实现逻辑 -->
6163

6264
```python
63-
65+
class Solution:
66+
def maxIceCream(self, costs: List[int], coins: int) -> int:
67+
costs.sort()
68+
ans, n = 0, len(costs)
69+
for i in range(n):
70+
if coins < costs[i]:
71+
break
72+
else:
73+
ans += 1
74+
coins -= costs[i]
75+
return ans
6476
```
6577

6678
### **Java**
6779

6880
<!-- 这里可写当前语言的特殊实现逻辑 -->
6981

7082
```java
83+
class Solution {
84+
public int maxIceCream(int[] costs, int coins) {
85+
Arrays.sort(costs);
86+
int ans = 0, n = costs.length;
87+
for (int i = 0; i < n && coins >= costs[i]; i++) {
88+
ans++;
89+
coins -= costs[i];
90+
}
91+
return ans;
92+
}
93+
}
94+
```
7195

96+
### **Go**
97+
98+
```go
99+
func maxIceCream(costs []int, coins int) int {
100+
sort.Ints(costs)
101+
n := len(costs)
102+
ans := 0
103+
for i := 0; i < n && coins >= costs[i]; i++ {
104+
ans++
105+
coins -= costs[i]
106+
}
107+
return ans
108+
}
72109
```
73110

74111
### **...**

solution/1800-1899/1833.Maximum Ice Cream Bars/README_EN.md

+38-1
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,55 @@
5050

5151
## Solutions
5252

53+
Pay attention to the data range. The question can easily mislead us to use the 01 backpack (it will overtime). In fact, this question is a simple "greedy problem" (choose low-priced ice cream first)
54+
5355
<!-- tabs:start -->
5456

5557
### **Python3**
5658

5759
```python
58-
60+
class Solution:
61+
def maxIceCream(self, costs: List[int], coins: int) -> int:
62+
costs.sort()
63+
ans, n = 0, len(costs)
64+
for i in range(n):
65+
if coins < costs[i]:
66+
break
67+
else:
68+
ans += 1
69+
coins -= costs[i]
70+
return ans
5971
```
6072

6173
### **Java**
6274

6375
```java
76+
class Solution {
77+
public int maxIceCream(int[] costs, int coins) {
78+
Arrays.sort(costs);
79+
int ans = 0, n = costs.length;
80+
for (int i = 0; i < n && coins >= costs[i]; i++) {
81+
ans++;
82+
coins -= costs[i];
83+
}
84+
return ans;
85+
}
86+
}
87+
```
6488

89+
### **Go**
90+
91+
```go
92+
func maxIceCream(costs []int, coins int) int {
93+
sort.Ints(costs)
94+
n := len(costs)
95+
ans := 0
96+
for i := 0; i < n && coins >= costs[i]; i++ {
97+
ans++
98+
coins -= costs[i]
99+
}
100+
return ans
101+
}
65102
```
66103

67104
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func maxIceCream(costs []int, coins int) int {
2+
sort.Ints(costs)
3+
n := len(costs)
4+
ans := 0
5+
for i := 0; i < n && coins >= costs[i]; i++ {
6+
ans++
7+
coins -= costs[i]
8+
}
9+
return ans
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public int maxIceCream(int[] costs, int coins) {
3+
Arrays.sort(costs);
4+
int ans = 0, n = costs.length;
5+
for (int i = 0; i < n && coins >= costs[i]; i++) {
6+
ans++;
7+
coins -= costs[i];
8+
}
9+
return ans;
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def maxIceCream(self, costs: List[int], coins: int) -> int:
3+
costs.sort()
4+
ans, n = 0, len(costs)
5+
for i in range(n):
6+
if coins < costs[i]:
7+
break
8+
else:
9+
ans += 1
10+
coins -= costs[i]
11+
return ans

0 commit comments

Comments
 (0)