File tree 5 files changed +108
-2
lines changed
solution/1800-1899/1833.Maximum Ice Cream Bars
5 files changed +108
-2
lines changed Original file line number Diff line number Diff line change 53
53
54
54
<!-- 这里可写通用的实现逻辑 -->
55
55
56
+ 注意数据范围,题目很容易误导我们使用01背包(会超时),其实这题就是简单贪心,优先选择定价小的雪糕。
57
+
56
58
<!-- tabs:start -->
57
59
58
60
### ** Python3**
59
61
60
62
<!-- 这里可写当前语言的特殊实现逻辑 -->
61
63
62
64
``` 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
64
76
```
65
77
66
78
### ** Java**
67
79
68
80
<!-- 这里可写当前语言的特殊实现逻辑 -->
69
81
70
82
``` 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
+ ```
71
95
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
+ }
72
109
```
73
110
74
111
### ** ...**
Original file line number Diff line number Diff line change 50
50
51
51
## Solutions
52
52
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
+
53
55
<!-- tabs:start -->
54
56
55
57
### ** Python3**
56
58
57
59
``` 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
59
71
```
60
72
61
73
### ** Java**
62
74
63
75
``` 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
+ ```
64
88
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
+ }
65
102
```
66
103
67
104
### ** ...**
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments