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

46. 携带研究材料,go语言代码超时,但我找不到优化的点了 #124

Open
yihao-bit opened this issue Sep 23, 2023 · 1 comment

Comments

@yihao-bit
Copy link
Contributor

`package main

import "fmt"

func main() {
var m, n int
fmt.Scan(&m, &n)
materialSpaces := make([]int, m)
for i := range materialSpaces {
fmt.Scan(&materialSpaces[i])
}
materialValues := make([]int, m)
for i := range materialValues {
fmt.Scan(&materialValues[i])
}
dp := make([]int, n+1)
for i := 0; i < m; i++ {
for j := n; j >= materialSpaces[i]; j-- {
dp[j] = max(dp[j], dp[j-materialSpaces[i]]+materialValues[i])
}
}
fmt.Println(dp[n])
}

func max(a, b int) int {
if a > b {
return a
}
return b
}
`花了1.7s

@yihao-bit
Copy link
Contributor Author

把fmt.Scan替换成bufio.NewScanner就行

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant