-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2554-MaximumNumberOfIntegersToChooseFromARangeI.go
More file actions
111 lines (98 loc) · 3.53 KB
/
2554-MaximumNumberOfIntegersToChooseFromARangeI.go
File metadata and controls
111 lines (98 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
// 2554. Maximum Number of Integers to Choose From a Range I
// 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.
// 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.
// Constraints:
// 1 <= banned.length <= 10^4
// 1 <= banned[i], n <= 10^4
// 1 <= maxSum <= 10^9
import "fmt"
import "sort"
func maxCount(banned []int, n int, maxSum int) int {
visited := map[int]bool{}
for _, v := range banned {
visited[v] = true
}
res, sum := 0, 0
for i := 1; i <= n && sum + i <= maxSum; i++ {
if !visited[i] {
res++
sum += i
}
}
return res
}
func maxCount1(banned []int, n int, maxSum int) int {
res, visited := 0, make(map[int]bool)
for _, v := range banned {
visited[v] = true
}
for i := 1; i <= n && maxSum >= i; i++ {
if visited[i] { continue }
res++
maxSum -= i
}
return res
}
func maxCount2(banned []int, n int, maxSum int) int {
sort.Ints(banned)
res, sum := 0, 0
for i := 1; i <= n; i++ {
if len(banned) > 0 && i == banned[0] {
for len(banned) > 0 && i == banned[0] {
banned = banned[1: ]
}
} else {
sum += i
res++
}
if sum > maxSum {
return res - 1
}
}
return res
}
func main() {
// 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.
fmt.Println(maxCount([]int{1,6,5}, 5, 6)) // 2
// 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.
fmt.Println(maxCount([]int{1,2,3,4,5,6,7}, 8, 1)) // 0
// 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.
fmt.Println(maxCount([]int{11}, 7, 50)) // 7
fmt.Println(maxCount1([]int{1,6,5}, 5, 6)) // 2
fmt.Println(maxCount1([]int{1,2,3,4,5,6,7}, 8, 1)) // 0
fmt.Println(maxCount1([]int{11}, 7, 50)) // 7
fmt.Println(maxCount2([]int{1,6,5}, 5, 6)) // 2
fmt.Println(maxCount2([]int{1,2,3,4,5,6,7}, 8, 1)) // 0
fmt.Println(maxCount2([]int{11}, 7, 50)) // 7
}