当前位置:网站首页>Four solutions of maximum sub segment and go
Four solutions of maximum sub segment and go
2022-07-27 16:27:00 【Wang Hao】
Maximum sub sum Go Four solutions of four
Preface
When the teacher assigned his homework, he encountered , Then although this problem is not very difficult , But such a problem with multiple solutions is worth recording , The code is directly on the back .
One 、 Code implementation ?
package main
import (
"fmt"
"math"
"math/rand"
"time"
)
var arr []int
func init() {
// fmt.Println(" Maximum sub segment and calculation ")
rand.Seed(time.Now().UnixNano())
for i := 0; i < 10; i++ {
item := rand.Intn(20) - 10
arr = append(arr, item)
}
}
// Method 1 : Divide and conquer O(nlogn)
func method1() {
// Calculate the maximum
getMax := func(args... int) int {
var maxData int = int(math.Inf(-1));
for _, item := range args {
if item > maxData {
maxData = item
}
}
return maxData
}
var maxsum func(l, r int) int
maxsum = func(l, r int) int{
if l == r {
return arr[l]
}
mid := (l + r) / 2
lsum := maxsum(l, mid)
rsum := maxsum(mid + 1, r)
sum1, sum2 := 0, 0
lefts, rights := 0, 0
for i := mid; i >= l; i-- {
lefts += arr[i]
if lefts > sum1 {
sum1 = lefts
}
}
for i := mid + 1; i <= r ; i++ {
rights += arr[i]
if rights > sum2 {
sum2 = rights
}
}
midsum := sum1 + sum2
return getMax(lsum, rsum, midsum)
}
ans := maxsum(0, len(arr) - 1)
if ans < 0 {
ans = 0 }
fmt.Println("maxData:", ans)
}
// The most optimal
// Method 2 : In data structure o(n)
func method2() {
var sum int = 0;
var maxData int = 0;
for _, item := range arr {
sum += item
if sum < 0 {
sum = 0
}
if sum > maxData {
maxData = sum
}
}
fmt.Println("maxData:", maxData)
}
// The brute force algorithm O(n^2)
func method3() {
n := len(arr)
maxData := int(math.Inf(-1))
sum := 0
for i := 0; i < n; i++ {
for j := i; j < n; j++ {
sum += arr[j]
if sum > maxData {
maxData = sum
}
}
sum = 0
}
fmt.Println("maxData: ", maxData)
}
// Method four : Dynamic programming o(n)
func method4() {
n := len(arr)
ans := int(math.Inf(-1))
dp := make([]int, n + 1)
for i := 1; i <= n; i++ {
if dp[i-1] + arr[i - 1] > arr[i - 1] {
dp[i] = dp[i-1] + arr[i - 1]
} else {
dp[i] = arr[i - 1]
}
if dp[i] > ans {
ans = dp[i]
}
}
fmt.Println(" Maximum sub sum ",ans)
}
func main() {
fmt.Println("data:",arr,"length:",len(arr))
method1()
method2()
method3()
method4()
}
summary
Tips : Here is a summary of the article :
Algorithm is still very important , You can practice more and record it
边栏推荐
猜你喜欢

training on multiple GPUs pytorch

Install MySQL using CentOS yum

Wechat applet personal number opens traffic master

Leetcode234 question - simple method to judge palindrome linked list

Your password does not satisfy the current policy requirements (modify MySQL password policy setting simple password)

CCF-201312-1

时间序列-ARIMA模型

Find active SQL connections in SQL Server

Yys mouse connector

DRF learning notes (II): Data deserialization
随机推荐
2.2 JMeter基本元件
Addition of large numbers
Nacos
Personal perception of project optimization
爬取常见英文名
mysql设置密码时报错 Your password does not satisfy the current policy requirements(修改·mysql密码策略设置简单密码)
The first week of C language learning - the history of C language
2021-06-02
第21回---第30回
excel skill
Common problems of mobile terminal H5
TP5 -- query field contains a certain --find of search criteria_ IN_ SET
The difference and use between get request and post request
Redis简介与使用
For enterprise operation and maintenance security, use the cloud housekeeper fortress machine!
DEX and AMMS of DFI security
知网、万方数据库免费下载论文------比连接学校内网速度快数倍不止(有的学校万方数据库不支持下载)
JSP基础
Scratch crawler framework
Paper_ Book