File tree Expand file tree Collapse file tree 4 files changed +63
-16
lines changed
solution/1800-1899/1833.Maximum Ice Cream Bars Expand file tree Collapse file tree 4 files changed +63
-16
lines changed Original file line number Diff line number Diff line change 48
48
<li><code>1 <= coins <= 10<sup>8</sup></code></li>
49
49
</ul >
50
50
51
-
52
51
## 解法
53
52
54
53
<!-- 这里可写通用的实现逻辑 -->
55
54
56
- 注意数据范围,题目很容易误导我们使用01背包 (会超时),其实这题就是简单贪心,优先选择定价小的雪糕。
55
+ 注意数据范围,题目很容易误导我们使用 01 背包 (会超时),其实这题就是简单贪心,优先选择定价小的雪糕。
57
56
58
57
<!-- tabs:start -->
59
58
65
64
class Solution :
66
65
def maxIceCream (self , costs : List[int ], coins : int ) -> int :
67
66
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 :
71
70
break
72
71
else :
73
72
ans += 1
74
- coins -= costs[i]
73
+ coins -= c
75
74
return ans
76
75
```
77
76
@@ -93,6 +92,24 @@ class Solution {
93
92
}
94
93
```
95
94
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
+
96
113
### **Go**
97
114
98
115
```go
Original file line number Diff line number Diff line change 47
47
<li><code>1 <= coins <= 10<sup>8</sup></code></li>
48
48
</ul >
49
49
50
-
51
50
## Solutions
52
51
53
52
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
60
59
class Solution :
61
60
def maxIceCream (self , costs : List[int ], coins : int ) -> int :
62
61
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 :
66
65
break
67
66
else :
68
67
ans += 1
69
- coins -= costs[i]
68
+ coins -= c
70
69
return ans
71
70
```
72
71
@@ -86,6 +85,24 @@ class Solution {
86
85
}
87
86
```
88
87
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
+
89
106
### **Go**
90
107
91
108
```go
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
def maxIceCream (self , costs : List [int ], coins : int ) -> int :
3
3
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 :
7
7
break
8
8
else :
9
9
ans += 1
10
- coins -= costs [ i ]
11
- return ans
10
+ coins -= c
11
+ return ans
You can’t perform that action at this time.
0 commit comments