0828字节笔试
有大佬用go 做的吗
第一题,60%超时了,o(n)遍历的,想不明白了
我用go做的第三题,不知道为什么答案是0%
package main import ( "fmt" ) func main() { n := 0 fmt.Scanf("%d", &n) l := make([]int, 0) dp := make([]int, n) for i := 0; i < n; i++ { x := 0 fmt.Scanf("%d", &x) l = append(l, x) } dp[n-1] = l[n-1] for i := n - 2; i >= 0; i-- { dp[i] = l[i] + max(0, dp[i+1]) } left := l[0] leftMax := left ans := dp[0] for i := 1; i < n; i++ { ans = max(ans, leftMax+dp[i]) left = l[i] + max(0, leftMax) leftMax = max(leftMax, left) } fmt.Println(ans) } func max(a, b int) int { if a > b { return a } return b }