Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your code should return the index of the first number and the index of the last number. (If their are duplicate answer, return anyone)
classSolution { 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){ // Write your code here vector<int> index; int max_endding_here = A[0]; int max_so_far = A[0]; int start = 0; int last_start = 0; intend = 0; bool set_i = false; for (int i = 1;i < A.size();i++) { max_endding_here = max_endding_here + A[i]; if (max_endding_here < A[i]) { max_endding_here = A[i]; set_i = true; } if (max_so_far <= max_endding_here) { max_so_far = max_endding_here; end = i; last_start = start; } if(set_i) { start = i; set_i = false; } } if (start > end) { start = last_start; } index.push_back(start); index.push_back(end); return index; } };