Virgil

53. Maximum SubarrayVirgilC++

C++

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
class Solution {
 public:
  int maxSubArray(vector<int>& nums) {
    vector<int> dp(nums.size());
    dp[0] = nums[0];
    int maxn = dp[0];

    for (int i = 1; i < nums.size(); i++) {
      dp[i] = max(dp[i - 1] + nums[i], nums[i]);
      maxn = max(dp[i], maxn);
    }

    return maxn;
  }
};