当前位置:网站首页>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 :
边栏推荐
- Using alicloud RDS for SQL Server Performance insight to optimize database load - first understanding of performance insight
- 构建Go命令行程序工具链
- 安装ImageMagick7.1库以及php的Imagick扩展
- great! The novel website project is completely open source
- 还在担心漏测吗?快来使用jacoco统计下代码覆盖率
- Teach you how to view version information with mongodb
- 如何轻松实现在线K歌房,与王心凌合唱《山海》
- Istio FAQ: return 426 status code
- How to expand disk space on AWS host
- 【C语言刷题——Leetcode12道题】带你起飞,飞进垃圾堆
猜你喜欢

MongoDB入门实战教程:学习总结目录

【应用推荐】最近大火的Apifox & Apipost 上手体验与选型建议

微信公众号调试与Natapp环境搭建

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

几种常见的DoS攻击

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

Linux record -4.22 MySQL 5.37 installation (supplementary)

【C语言刷题——Leetcode12道题】带你起飞,飞进垃圾堆

Using alicloud RDS for SQL Server Performance insight to optimize database load - first understanding of performance insight

【云原生 | Kubernetes篇】Kubernetes基础入门(三)
随机推荐
60 divine vs Code plug-ins!!
Fine! Huawei firewall dual computer hot standby Technology: HRP, vgmp, VRRP
great! The novel website project is completely open source
How to efficiently transfer enterprise business data?
Convert text to hexadecimal, and reverse
Recommend several super practical data analysis tools
微信公众号调试与Natapp环境搭建
如何扩展aws主机上的磁盘空间
【云原生 | Kubernetes篇】Kubernetes基础入门(三)
Step by step import RHEL image to Tencent cloud
如何在Thymeleaf3标签中使用嵌套标签
日志记录真没你想的那么简单
MySQL toolset: the official performance testing tool mysqlslap
Wi-Fi 7 来啦,它到底有多强?
Install the imagemagick7.1 library and the imageick extension for PHP
Golang+redis distributed mutex
Motion planning of floating base robot
几种常见的DoS攻击
不忘初心
FreeRTOS新建任务不执行问题解决办法