One, 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)
Second, problem-solving ideas
This question is beginning to see a bit ignorant, or even thought of using loops and arrays to traverse and record their own knowledge base does not even close!
Later, see a classmate with dynamic programming to do, forgive me for this was unclear. But big brother said very clearly, I have a basic understanding of the point. Direct solution to a problem of students who cut:
Cattle passenger link:
https://www.nowcoder.com/questionTerminal/459bd355da1549fa8a49e350bf3df484?f=discussion
Third, the code
public class Solution {
public int FindGreatestSumOfSubArray(int[] array) {
//动态规划
int maxArr=array[0];//包含当前元素array[i]在内的一个连续子,数组的最大值
int maxVal=array[0];//记录所有子数组中的最大值
for(int i=1;i<array.length;i++){
maxArr=Math.max(maxArr+array[i],array[i]);
maxVal=Math.max(maxVal,maxArr);
}
return maxVal;
}