Skip to content

Commit fbc0988

Browse files
committed
feat: add solutions to lc problem: No.1833.Maximum Ice Cream Bars
1 parent 52ef335 commit fbc0988

File tree

4 files changed

+63
-16
lines changed

4 files changed

+63
-16
lines changed

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

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,11 @@
4848
<li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li>
4949
</ul>
5050

51-
5251
## 解法
5352

5453
<!-- 这里可写通用的实现逻辑 -->
5554

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

5857
<!-- tabs:start -->
5958

@@ -65,13 +64,13 @@
6564
class Solution:
6665
def maxIceCream(self, costs: List[int], coins: int) -> int:
6766
costs.sort()
68-
ans, n = 0, len(costs)
69-
for i in range(n):
70-
if coins < costs[i]:
67+
ans = 0
68+
for c in costs:
69+
if coins < c:
7170
break
7271
else:
7372
ans += 1
74-
coins -= costs[i]
73+
coins -= c
7574
return ans
7675
```
7776

@@ -93,6 +92,24 @@ class Solution {
9392
}
9493
```
9594

95+
### **C++**
96+
97+
```cpp
98+
class Solution {
99+
public:
100+
int maxIceCream(vector<int>& costs, int coins) {
101+
sort(costs.begin(), costs.end());
102+
int ans = 0;
103+
for (int i = 0; i < costs.size() && coins >= costs[i]; ++i)
104+
{
105+
++ans;
106+
coins -= costs[i];
107+
}
108+
return ans;
109+
}
110+
};
111+
```
112+
96113
### **Go**
97114
98115
```go

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
<li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li>
4848
</ul>
4949

50-
5150
## Solutions
5251

5352
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)
@@ -60,13 +59,13 @@ Pay attention to the data range. The question can easily mislead us to use the 0
6059
class Solution:
6160
def maxIceCream(self, costs: List[int], coins: int) -> int:
6261
costs.sort()
63-
ans, n = 0, len(costs)
64-
for i in range(n):
65-
if coins < costs[i]:
62+
ans = 0
63+
for c in costs:
64+
if coins < c:
6665
break
6766
else:
6867
ans += 1
69-
coins -= costs[i]
68+
coins -= c
7069
return ans
7170
```
7271

@@ -86,6 +85,24 @@ class Solution {
8685
}
8786
```
8887

88+
### **C++**
89+
90+
```cpp
91+
class Solution {
92+
public:
93+
int maxIceCream(vector<int>& costs, int coins) {
94+
sort(costs.begin(), costs.end());
95+
int ans = 0;
96+
for (int i = 0; i < costs.size() && coins >= costs[i]; ++i)
97+
{
98+
++ans;
99+
coins -= costs[i];
100+
}
101+
return ans;
102+
}
103+
};
104+
```
105+
89106
### **Go**
90107
91108
```go
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int maxIceCream(vector<int>& costs, int coins) {
4+
sort(costs.begin(), costs.end());
5+
int ans = 0;
6+
for (int i = 0; i < costs.size() && coins >= costs[i]; ++i)
7+
{
8+
++ans;
9+
coins -= costs[i];
10+
}
11+
return ans;
12+
}
13+
};
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Solution:
22
def maxIceCream(self, costs: List[int], coins: int) -> int:
33
costs.sort()
4-
ans, n = 0, len(costs)
5-
for i in range(n):
6-
if coins < costs[i]:
4+
ans = 0
5+
for c in costs:
6+
if coins < c:
77
break
88
else:
99
ans += 1
10-
coins -= costs[i]
11-
return ans
10+
coins -= c
11+
return ans

0 commit comments

Comments
 (0)