python_最大子数组

伪代码

在这里插入图片描述
在这里插入图片描述

算法导论版python实现

'''
Description:
Version: 2.0
Author: xuchaoxin
Date: 2021-03-19 22:11:17
LastEditors: xuchaoxin
LastEditTime: 2021-03-20 08:23:01
'''
"""
初始化总和变量
方案一:
初始化为0总是合适的(累乘变量初始化为1也总是合适的)
因为0与任何数相加都不会改变被加和的数的大小
方案二
初始化为输入数据的第一个元素
在累加是记得去掉首元素
所以为了避免忘记去重,初始化为0还是更常用
"""
def cross_subarray(list, low, mid, high):
"""conquer the cross situation
Args:
list (List): [description]
low ([int]): [description]
mid ([int]): [description]
high ([int]): [description]
Returns:
[tuple]: [triple]
""" """ """
sum = 0
left_sum_to_max = list[mid]
max_left_indice = mid
for i in range(low, mid):
sum += list[i]
if sum > left_sum_to_max:
left_sum_to_max = sum
max_left_indice = i
sum = 0
right_sum_to_max = list[mid+1]
max_right_indice=mid+1
for i in range(mid+1, high):
sum += list[i]
if sum > right_sum_to_max:
right_sum_to_max = sum
max_right_indice=i
return max_left_indice,max_right_indice,left_sum_to_max+right_sum_to_max
def max_subarray(list,low,high):
"""conquer the subarray problem
Args:
list (List): the list to be find the max subarray
low (int): left indice
high (int): right indice
Returns:
tuple: triple
"""
""" 递归出口(返回) """
if high==low:
return low,high,list[0]
else:
mid=(low+high)//2
left_tuple=max_subarray(list,low,mid)
right_tuple=max_subarray(list,mid+1,high)
cross_tuple=cross_subarray(list,low,mid,high)
if left_tuple[2]>=right_tuple[2] and left_tuple[2]>=cross_tuple[2]:
return left_tuple
elif cross_tuple[2]>=left_tuple[2] and cross_tuple[2]>=right_tuple[2]:
return cross_tuple
else:
return right_tuple
def main():
list=[13,-3,-25,20,-3,-16,-23,18,20,-7,12,-5,-22,15,-4,7]
list1=[1,2,-1,5,6,-8,3]
print("runing..")
print(max_subarray(list,0,len(list1)-1))
""" start the program """
main()
posted @   xuchaoxin1375  阅读(3)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2022-08-26 math_高数公式每日一过_part2(private)
点击右上角即可分享
微信分享提示