Python algorithms and data structures--the maximum value of all sub-arrays

Source: Internet
Author: User

Python algorithms and data structures--the maximum value of all sub-arrays

Xuan Soul Studio-Xuan Soul

Xuan Soul Studio Secretary Xuan Soul studio? Yesterday

Title: Enter an array of shapes with positive and negative numbers in the array. One or more consecutive integers in an array make up a sub-array, each of which has a and.
The maximum value for the and of all sub-arrays. Requires a time complexity of O (n).

This topic has several solutions, such as the ability to use a two-dimensional array before each of the data and then in the size of the comparison, but the time of responsibility is O (N2).
Think of another way of thinking, because it is to the maximum number, then do not need to store, only need to find the maximum value on it. But in order to find the maximal sum of the subsequence, and to skip the case where the addition is negative, this block notes the last if in the code.
The basic idea: a number of a number to add, after the sum and the maximum number and the current number of comparisons, to find the largest, if the sum is negative, then the cumulative zeroing
Code-----------

#-*-Coding:utf-8-*-"" "Title: Enter an array of shapes with positive and negative numbers in the array. One or more consecutive integers in an array make up a sub-array, each of which has a and. The maximum value for the and of all sub-arrays. Requires a time complexity of O (n). Basic idea: A number of a number to add, after the sum and the maximum number and the current number of comparisons to find the largest, if the sum is negative, then add 0 "" "If __name__ = =" __main__ ": #初始化数组, test data #dataList = [ -3,-10, 30,-5,-6,-100,300] #dataList = [ -3,-10,-30,-5,-6,-1,-100,-300] #dataList = [ -3,-10,0,-5,-6,-100,-300] dataList = [3,10,0,5,6,100,300] #dataList = [0,0,0,0,0,0,0] #prd_data用来记录前面累加的数, once the accumulated value is negative, clear 0 pre_data = datalist[0] #用来记录        Maximum value Max_data = Pre_data #遍历数据组进行累加和大小对比 for i in range (len (dataList)): Currdata = Datalist[i] #第一个数用来做初始化, from the second count if i==0:continue else: #相加后进行temp_data, Max_data, and currdata size comparison , find the largest temp_data = Currdata + pre_data if temp_data > Currdata:if temp_data >                    Max_data:max_data = Temp_data else:if currdata > Max_data: Max_data = Currdata #如After the sum is negative, then clear 0, because once the negative number in the addition will only make the maximum value smaller #这块注意 ********** must be added to be negative, but not the numbers itself is negative *********** #因为本身是负数, but the sum is positive, after It is helpful if temp_data > 0:pre_data = Temp_data else:pre_data = 0 Print Max_data

Boutique network security video recommendation-Legendary black Unicorn

Quality Network Security Course recommendation--"password security attack and defense technology explaining"

WEB Security Introductory Course recommended--web Security: Vulnerability principle

Welcome to the Xuan Soul Studio vertical subscription number "vernacular algorithm".

High-quality algorithm course recommended, scan code to learn more:

Python algorithms and data structures--the maximum value of all sub-arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.