当前位置:网站首页>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 :
边栏推荐
- Istio FAQ: region awareness does not take effect
- 国产最长寿的热销手机,苹果也不是对手,总算让国产手机找回面子
- How to use nested tags in thymeleaf3 Tags
- Istio practical skill: enable accesslog locally
- 运营商5G用户渗透远远比4G慢,5G的普及还得看中国广电
- 刚刚阿里面软件测试回来,3+1面任职阿里P7,年薪28*15薪
- Apple is no match for the longest selling mobile phone made in China, and has finally brought back the face of the domestic mobile phone
- 如何实现容器内的SqlServer的数据库迁移
- Linux record -4.22 MySQL 5.37 installation (supplementary)
- PHP application container deployment practice
猜你喜欢

Why is it easy for enterprises to fail in implementing WMS warehouse management system

为什么企业实施WMS仓储管理系统很容易失败

Jenkins 镜像无法更新插件中心的3种解决方法

用 Oasis 开发一个跳一跳(一)—— 场景搭建

Still worried about missing measurements? Let's use Jacobo to calculate the code coverage

60 个神级 VS Code 插件!!

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

SIGGRAPH 2022 | 真实还原手部肌肉,数字人双手这次有了骨骼、肌肉、皮肤

国产芯片的赶超,让美国手机芯片龙头高通害怕了,出招应对竞争

【云原生 | Kubernetes篇】Kubernetes基础入门(三)
随机推荐
Is it safe to open an account for flush stock on mobile phone!
CAP:多重注意力机制,有趣的细粒度分类方案 | AAAI 2021
如何扩展aws主机上的磁盘空间
Intelij 中的 Database Tools可以连接但是无法显示SCHEMA, TABLES
MySQL binlog
Apple is no match for the longest selling mobile phone made in China, and has finally brought back the face of the domestic mobile phone
MongoDB入门实战教程:学习总结目录
The cold winter can't stop the determination to enter the big factory. The Android interview has a complete knowledge structure, and everything you need to master in the interview is here!
刚刚阿里面软件测试回来,3+1面任职阿里P7,年薪28*15薪
实现领域驱动设计 - 使用ABP框架 - 领域逻辑 & 应用逻辑
熬夜整理出的软件测试【高频】面试题大全(2022最新)
The catch-up of domestic chips has scared Qualcomm, the leader of mobile phone chips in the United States, and made moves to cope with the competition
Decomposition of Uber dependency injection into dig source code analysis
Istio FAQ: region awareness does not take effect
[log service CLS] a taste of Tencent cloud log service CLS
安裝ImageMagick7.1庫以及php的Imagick擴展
运营商5G用户渗透远远比4G慢,5G的普及还得看中国广电
Flink kubernetes application deployment
How to obtain ECS metadata
Rush for IPO, Hello, I'm in a hurry