Offer to prove safety and maximum python [30.] consecutive sub-array implementation

Title Description

HZ occasionally get some professional issues to flicker those non-computer science students. After the test group will finish today, he spoke up: in the old one-dimensional pattern recognition, it is often necessary to calculate the maximum and continuous sub-vector when the vector is a positive whole number, the problem solved. However, if the vector contains a negative number, it should contain a negative number, and expect a positive number next to it would make up for it? For example: {6, -3, -2,7, -15,1,2,2}, and the maximum successive sub-vectors of 8 (beginning from 0, up to the third). To an array and returns its maximum continuous subsequence and you will not be fooled him live? (Sub-vector length is at least 1)

Code

# -*- coding:utf-8 -*-
class Solution:
    def FindGreatestSumOfSubArray(self, array):
        # write code here
        k = 0
        res = []
        for i in array:
            k += 1
            res.append(i)
            for j in array[k:]:
                i +=j
                res.append(i)
        return max(res)
Published 99 original articles · won praise 6 · views 3969

Guess you like

Origin blog.csdn.net/weixin_42247922/article/details/104004582