当前位置:网站首页>2021-04-25: given an array arr and a positive number m, the
2021-04-25: given an array arr and a positive number m, the
2022-06-24 15:53:00 【Fuda scaffold constructor's daily question】
Fuda answer 2021-04-25:
The prefix and + Double ended queues with large left and small right . It's too late , So it's simple .
The code to use golang To write . The code is as follows :
package main
import (
"container/list"
"fmt"
)
func main() {
arr := []int{1, 2, -3, 4, -5}
ret := maxSum(arr, 5)
fmt.Println(ret)
}
// O(N) Solution method , Optimal solution
func maxSum(arr []int, M int) int {
if len(arr) == 0 || M < 1 {
return 0
}
N := len(arr)
// The prefix and
sum := make([]int, N)
sum[0] = arr[0]
for i := 1; i < N; i++ {
sum[i] = sum[i-1] + arr[i]
}
// deque
qmax := list.New()
i := 0
end := getMin(N, M)
for ; i < end; i++ {
for qmax.Len() > 0 && sum[qmax.Back().Value.(int)] <= sum[i] {
qmax.Remove(qmax.Back())
}
qmax.PushBack(i)
}
max := sum[qmax.Front().Value.(int)]
L := 0
for ; i < N; L, i = L+1, i+1 {
if qmax.Front().Value.(int) == L {
qmax.Remove(qmax.Front())
}
for qmax.Len() > 0 && sum[qmax.Back().Value.(int)] <= sum[i] {
qmax.Remove(qmax.Back())
}
qmax.PushBack(i)
max = getMax(max, sum[qmax.Front().Value.(int)]-sum[L])
}
for ; L < N-1; L++ {
if qmax.Front().Value.(int) == L {
qmax.Remove(qmax.Front())
}
max = getMax(max, sum[qmax.Front().Value.(int)]-sum[L])
}
return max
}
func getMin(a int, b int) int {
if a < b {
return a
} else {
return b
}
}
func getMax(a int, b int) int {
if a > b {
return a
} else {
return b
}
}The results are as follows :
边栏推荐
- [log service CLS] Tencent cloud log4j/logback log collection best practices
- Remote connection raspberry pie in VNC Viewer Mode
- MySQL toolset: the official performance testing tool mysqlslap
- Nature刊登量子计算重大进展:有史以来第一个量子集成电路实现
- At? Let's blow the air conditioner together!
- Paper: Google TPU
- 还在担心漏测吗?快来使用jacoco统计下代码覆盖率
- 中国产品经理的没落:从怀恋乔布斯开始谈起
- Teach you how to view version information with mongodb
- 一文理解OpenStack网络
猜你喜欢

60 个神级 VS Code 插件!!

nifi从入门到实战(保姆级教程)——环境篇

Logging is not as simple as you think

Understanding openstack network

高速公路服务区智能一体机解决方案

CAP:多重注意力机制,有趣的细粒度分类方案 | AAAI 2021

还在担心漏测吗?快来使用jacoco统计下代码覆盖率

The penetration of 5g users of operators is far slower than that of 4G. The popularity of 5g still depends on China Radio and television

运营商5G用户渗透远远比4G慢,5G的普及还得看中国广电

【附下载】汉化版Awvs安装与简单使用
随机推荐
Instruction document for online written examination assistance of smart side school recruitment
在Gradle 中对Junit5 测试框架引用
nifi从入门到实战(保姆级教程)——环境篇
60 个神级 VS Code 插件!!
【云原生 | Kubernetes篇】Kubernetes基础入门(三)
FreeRTOS新建任务不执行问题解决办法
clang: warning: argument unused during compilation: ‘-no-pie‘ [-Wunused-command-line-argument]
个人常用的高效工具
Linux记录-4.22 MySQL5.37安装(补充)
国产芯片的赶超,让美国手机芯片龙头高通害怕了,出招应对竞争
Binary computing
Design of CAN bus controller based on FPGA (Part 2)
leetcode 139. Word break word split (medium)
MySQL toolset: the official export tool mysqlpump
Flink Kubernetes Application部署
10 hands-free idea plug-ins. These codes do not need to be written (the second bullet)
Firefox browser uses plug-ins to set up proxy
打破内存墙的新利器成行业“热搜”!持久内存让打工人也能玩转海量数据+高维模型
Flink kubernetes application deployment
PHP export data as excel table