kamacoder-solutions icon indicating copy to clipboard operation
kamacoder-solutions copied to clipboard

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

Open yihao-bit opened this issue 1 year ago • 1 comments

`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 avatar Sep 23 '23 13:09 yihao-bit

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

yihao-bit avatar Sep 26 '23 14:09 yihao-bit