And the largest contiguous sub-array

1: Title Description

Enter an integer array, the array has a positive but also negative. One or more continuous integer array composed of a subarray. Seeking the maximum of all the sub-arrays and.

Required time complexity is O (n).

 

Example 1:

Input: nums = [-2,1, -3,4, -1,2,1, -5,4]
Output: 6
Explanation: continuous subarray [4, -1,2,1], and the most, 6.
 

prompt:

. 1 <= arr.length <= 10. 5 ^
-100 <= ARR [I] <= 100
Note: The same problems present title 53 and the master station: https: //leetcode-cn.com/problems/maximum-subarray/

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/lian-xu-zi-shu-zu-de-zui-da-he-lcof
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

2: Title Analysis

  Questions asked time complexity is O (n), then it is clear that violent search methods can not meet the requirements. So how do O (n), only the case over and over again listing the group will be able to obtain results, it is to meet the subject requirements.

2.1: greedy algorithm

 

 

 

2.2: Dynamic Programming

 

Guess you like

Origin www.cnblogs.com/dazhu123/p/12588125.html