问题:
难度:Medium
说明:
输入一个数组,遍历数组时候是头尾相连的,然后求出数组中和最大的子序列。题目跟以前看到过的没有头尾相连的单数组情况相似,如果没有头尾相连的话,那么就一个贪心就可以处理掉了,但是头尾相连会是另外一种情况。
输入案例:
Example 1:
Input: [1,-2,3,-2]
Output: 3
Explanation: Subarray [3] has maximum sum 3
Example 2:
Input: [5,-3,5]
Output: 10
Explanation: Subarray [5,5] has maximum sum 5 + 5 = 10
Example 3:
Input: [3,-1,2,-1]
Output: 4
Explanation: Subarray [2,-1,3] has maximum sum 2 + (-1) + 3 = 4
Example 4:
Input: [3,-2,2,-3]
Output: 3
Explanation: Subarray [3] and [3,-2,2] both have maximum sum 3
Example 5:
Input: [-2,-3,-1]
Output: -1
Explanation: Subarray [-1] has maximum sum -1
我的代码:
根据之前不可循环的数组,使用贪心算法可以得出最大值,可循环数组可以认为有N个开头,然后N个开头都可以组成不一样的数组。