Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2ada18f

Browse files
committed
Merge pull request #396 from 0xff-dev/1833
Add solution and test-cases for problem 1833
2 parents 67bb9f9 + 5f3c78e commit 2ada18f

File tree

3 files changed

+48
-25
lines changed

3 files changed

+48
-25
lines changed

‎leetcode/1801-1900/1833.Maximum-Ice-Cream-Bars/README.md

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
# [1833.Maximum Ice Cream Bars][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
It is a sweltering summer day, and a boy wants to buy some ice cream bars.
5+
6+
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 i<sup>th</sup> 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.
7+
8+
Return the **maximum** number of ice cream bars the boy can buy with `coins` coins.
9+
10+
**Note**: The boy can buy the ice cream bars in any order.
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: costs = [1,3,2,4,1], coins = 7
16+
Output: 4
17+
Explanation: The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 +たす 3 +たす 2 +たす 1 = 7.
1318
```
1419

15-
## 题意
16-
> ...
17-
18-
## 题解
20+
**Example 2:**
1921

20-
### 思路1
21-
> ...
22-
Maximum Ice Cream Bars
23-
```go
2422
```
23+
Input: costs = [10,6,8,7,7,8], coins = 5
24+
Output: 0
25+
Explanation: The boy cannot afford any of the ice cream bars.
26+
```
27+
28+
**Example 3:**
2529

30+
```
31+
Input: costs = [1,6,3,1,2,5], coins = 20
32+
Output: 6
33+
Explanation: The boy can buy all the ice cream bars for a total price of 1 +たす 6 +たす 3 +たす 1 +たす 2 +たす 5 = 18.
34+
```
2635

2736
## 结语
2837

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
// heap, sort
6+
func Solution(costs []int, coins int) int {
7+
ans := 0
8+
sum := 0
9+
sort.Ints(costs)
10+
for i := 0; i < len(costs); i++ {
11+
sum += costs[i]
12+
if sum > coins {
13+
break
14+
}
15+
ans++
16+
}
17+
return ans
518
}

‎leetcode/1801-1900/1833.Maximum-Ice-Cream-Bars/Solution_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
coins int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase", []int{1, 3, 2, 4, 1}, 7, 4},
18+
{"TestCase", []int{10, 6, 8, 7, 7, 8}, 5, 0},
19+
{"TestCase", []int{1, 6, 3, 1, 2, 5}, 20, 6},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.inputs, c.coins)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.inputs, c.coins)
2829
}
2930
})
3031
}
3132
}
3233

33-
//压力测试
34+
//压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
//使用案列
38+
//使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /