当前位置:网站首页>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
边栏推荐
- MySQL index
- mysql设置密码时报错 Your password does not satisfy the current policy requirements(修改·mysql密码策略设置简单密码)
- Two methods of generating excel table with PHP
- 云管平台中租户以及多租户概念简单说明
- DRF use: get request to get data (small example)
- Excel提取重复项
- Coding skills - Global exception capture & unified return body & Business exception
- Leetcode25 question: turn the linked list in a group of K -- detailed explanation of the difficult questions of the linked list
- pdf 提取文字
- DRF learning notes (III): model class serializer modelserializer
猜你喜欢

MySQL high version report SQL_ mode=only_ full_ group_ By exception

Test novice learning classic (with ideas)

DRF learning notes (IV): DRF view

COMS Technology

Reduce PDF document size (Reprint)

EXE程序加密锁

The difference and use between get request and post request

For enterprise operation and maintenance security, use the cloud housekeeper fortress machine!

编码技巧——全局异常捕获&统一的返回体&业务异常

新版jmeter函数助手不在选项菜单下-在工具栏中
随机推荐
t5 学习
Addition of large numbers
Introduction to JWT
JSP基础
Simulation生成报表
Product axure9 English version, using repeater repeater to realize drop-down multi selection box
DRF learning notes (III): model class serializer modelserializer
Mapreduce实例(三):数据去重
将IAR工程文件夹转移目录后无法进入函数定义解决
Sudden! 28 Chinese entities including Hikvision / Dahua / Shangtang / Kuangshi / ITU / iFLYTEK have been blacklisted by the United States
Chuanyin holdings disclosed that it was prosecuted by Huawei: a case has been filed, involving an amount of 20million yuan
webRTC中的coturn服务安装
Duplicate numbers in array
The difference between select/poll/epoll
Axure 安装图标字体元件库
my_ls小结
重新配置cubemx后,生成的代码用IAR打开不成功
MySQL索引
MapReduce instance (III): data De duplication
Draw circuit diagram according to Verilog code