2554. Maximum Number of Integers to Choose From a Range I
思路:
就用hash table記錄在banned出現的數字
接著從1開始到n
如果遇到不在banned裡的數字就加到sum裡面
注意不要讓sum超過maxSum就好
又水了一天每日
golang code:
func maxCount(banned []int, n int, maxSum int) int {
rec := make(map[int]struct{})
for _, val := range banned {
rec[val] = struct{}{}
}
cnt := 0
for i := 1; i <= n && maxSum >= i; i++ {
if _, ok := rec[i]; !ok {
cnt++
maxSum -= i
}
}
return cnt
}