Skip to content

Commit d989caf

Browse files
committed
Add solution and test-cases for problem 2554
1 parent dbd4dda commit d989caf

File tree

3 files changed

+76
-13
lines changed

3 files changed

+76
-13
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# [2554. Maximum Number of Integers to Choose From a Range I][title]
2+
3+
## Description
4+
You are given an integer array `banned` and two integers `n` and `maxSum`. You are choosing some number of integers following the below rules:
5+
6+
- The chosen integers have to be in the range `[1, n]`.
7+
- Each integer can be chosen **at most once*8.
8+
- The chosen integers should not be in the array `banned`.
9+
- The sum of the chosen integers should not exceed `maxSum`.
10+
11+
Return the **maximum** number of integers you can choose following the mentioned rules.
12+
13+
**Example 1:**
14+
15+
```
16+
Input: banned = [1,6,5], n = 5, maxSum = 6
17+
Output: 2
18+
Explanation: You can choose the integers 2 and 4.
19+
2 and 4 are from the range [1, 5], both did not appear in banned, and their sum is 6, which did not exceed maxSum.
20+
```
21+
22+
**Example 2:**
23+
24+
```
25+
Input: banned = [1,2,3,4,5,6,7], n = 8, maxSum = 1
26+
Output: 0
27+
Explanation: You cannot choose any integer while following the mentioned conditions.
28+
```
29+
30+
**Example 3:**
31+
32+
```
33+
Input: banned = [11], n = 7, maxSum = 50
34+
Output: 7
35+
Explanation: You can choose the integers 1, 2, 3, 4, 5, 6, and 7.
36+
They are from the range [1, 7], all did not appear in banned, and their sum is 28, which did not exceed maxSum.
37+
```
38+
39+
## 结语
40+
41+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
42+
43+
[title]: https://leetcode.com/problems/maximum-number-of-integers-to-choose-from-a-range-i
44+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(banned []int, n int, maxSum int) int {
4+
sum := 0
5+
bm := make(map[int]struct{})
6+
for _, x := range banned {
7+
if x >= 1 && x <= n {
8+
bm[x] = struct{}{}
9+
}
10+
}
11+
ans := 0
12+
for i := 1; i <= n; i++ {
13+
if _, ok := bm[i]; ok {
14+
continue
15+
}
16+
sum += i
17+
if sum > maxSum {
18+
break
19+
}
20+
ans++
21+
}
22+
return ans
523
}

leetcode/2501-2600/2554.Maximum-Number-of-Integers-to-Choose-From-a-Range-I/Solution_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,32 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
banned []int
14+
n, maxSum int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{1, 6, 5}, 5, 6, 2},
18+
{"TestCase2", []int{1, 2, 3, 4, 5, 6, 7}, 8, 1, 0},
19+
{"TestCase3", []int{11}, 7, 50, 7},
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.banned, c.n, c.maxSum)
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 %v",
28+
c.expect, got, c.banned, c.n, c.maxSum)
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)