diff --git a/leetcode/2501-2600/2554.Maximum-Number-of-Integers-to-Choose-From-a-Range-I/README.md b/leetcode/2501-2600/2554.Maximum-Number-of-Integers-to-Choose-From-a-Range-I/README.md new file mode 100644 index 000000000..81207eab0 --- /dev/null +++ b/leetcode/2501-2600/2554.Maximum-Number-of-Integers-to-Choose-From-a-Range-I/README.md @@ -0,0 +1,44 @@ +# [2554. Maximum Number of Integers to Choose From a Range I][title] + +## Description +You are given an integer array `banned` and two integers `n` and `maxSum`. You are choosing some number of integers following the below rules: + +- The chosen integers have to be in the range `[1, n]`. +- Each integer can be chosen **at most once*8. +- The chosen integers should not be in the array `banned`. +- The sum of the chosen integers should not exceed `maxSum`. + +Return the **maximum** number of integers you can choose following the mentioned rules. + +**Example 1:** + +``` +Input: banned = [1,6,5], n = 5, maxSum = 6 +Output: 2 +Explanation: You can choose the integers 2 and 4. +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. +``` + +**Example 2:** + +``` +Input: banned = [1,2,3,4,5,6,7], n = 8, maxSum = 1 +Output: 0 +Explanation: You cannot choose any integer while following the mentioned conditions. +``` + +**Example 3:** + +``` +Input: banned = [11], n = 7, maxSum = 50 +Output: 7 +Explanation: You can choose the integers 1, 2, 3, 4, 5, 6, and 7. +They are from the range [1, 7], all did not appear in banned, and their sum is 28, which did not exceed maxSum. +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/maximum-number-of-integers-to-choose-from-a-range-i +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/2501-2600/2554.Maximum-Number-of-Integers-to-Choose-From-a-Range-I/Solution.go b/leetcode/2501-2600/2554.Maximum-Number-of-Integers-to-Choose-From-a-Range-I/Solution.go index d115ccf5e..e036c202a 100755 --- a/leetcode/2501-2600/2554.Maximum-Number-of-Integers-to-Choose-From-a-Range-I/Solution.go +++ b/leetcode/2501-2600/2554.Maximum-Number-of-Integers-to-Choose-From-a-Range-I/Solution.go @@ -1,5 +1,23 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(banned []int, n int, maxSum int) int { + sum := 0 + bm := make(map[int]struct{}) + for _, x := range banned { + if x >= 1 && x <= n { + bm[x] = struct{}{} + } + } + ans := 0 + for i := 1; i <= n; i++ { + if _, ok := bm[i]; ok { + continue + } + sum += i + if sum > maxSum { + break + } + ans++ + } + return ans } diff --git a/leetcode/2501-2600/2554.Maximum-Number-of-Integers-to-Choose-From-a-Range-I/Solution_test.go b/leetcode/2501-2600/2554.Maximum-Number-of-Integers-to-Choose-From-a-Range-I/Solution_test.go index 14ff50eb4..e8e85d0ac 100755 --- a/leetcode/2501-2600/2554.Maximum-Number-of-Integers-to-Choose-From-a-Range-I/Solution_test.go +++ b/leetcode/2501-2600/2554.Maximum-Number-of-Integers-to-Choose-From-a-Range-I/Solution_test.go @@ -9,31 +9,32 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + banned []int + n, maxSum int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{1, 6, 5}, 5, 6, 2}, + {"TestCase2", []int{1, 2, 3, 4, 5, 6, 7}, 8, 1, 0}, + {"TestCase3", []int{11}, 7, 50, 7}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.banned, c.n, c.maxSum) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v", + c.expect, got, c.banned, c.n, c.maxSum) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }