Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.2902 #2320

Merged
merged 7 commits into from
Feb 6, 2024
Merged
Prev Previous commit
Next Next commit
feat: add solutions to lc problems: No.2902
  • Loading branch information
Nothing-avil authored Feb 5, 2024
commit 451ffd73aa05b5783d639f2c7018604a60646aa6
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
func countSubMultisets(nums []int, l int, r int) int {
multiset := make(map[int]int)
for _, num := range nums {
multiset[num]++
}
mem := make([]int, r+1)
mem[0] = 1
prefix := make([]int, len(mem))
for num, occ := range multiset {
copy(prefix, mem)
for sum := num; sum <= r; sum++ {
prefix[sum] = (prefix[sum] + prefix[sum-num]) % mod
}
for sum := r; sum >= 0; sum-- {
if num > 0 {
mem[sum] = prefix[sum]
if sum >= num*(occ+1) {
mem[sum] = (mem[sum] - prefix[sum-num*(occ+1)] + mod) % mod
}
} else {
mem[sum] = (mem[sum] * (occ + 1)) % mod
}
}
}
var result int
for sum := l; sum <= r; sum++ {
result = (result + mem[sum]) % mod
}
return result
}
var mod int = 1e9 + 7