A variation to a classical DP: LCS.
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
class Solution { public: /** * @param A an integer array * @return A list of integers includes the index of * the first number and the index of the last number */ vector<int> continuousSubarraySum(vector<int>& A) { vector<int> ret; size_t len = A.size(); int curr = A[0]; int sum = A[0], csum = A[0]; int s = 0, e = 0, ss = 0, ee = 0; for (int i = 1; i < len; i++) { int nsum = csum + A[i]; if (A[i] > nsum) { ss = ee = i; csum = A[i]; } else { ee = i; csum = nsum; } if(csum > sum) // update global record { sum = nsum; s = ss; e = ee; } } ret.push_back(s); ret.push_back(e); return ret; } };