标签:cto div array bsp col max style subarray sub
1 class Solution 2 { 3 public: 4 int maxSubArray(vector<int>& nums) 5 { 6 int n = nums.size(); 7 vector<int> dp(n + 5,0); 8 dp[0] = nums[0]; 9 int res = nums[0]; 10 for(int i = 1;i < n;i ++) 11 { 12 dp[i] = max(nums[i],nums[i] + dp[i - 1]); 13 res = max(res,dp[i]); 14 } 15 return res; 16 } 17 };
动态规划:dp[i] = max(nums[i],nums[i] + dp[i-1]),其中dp[0] = nums[0]
标签:cto div array bsp col max style subarray sub
原文地址:https://www.cnblogs.com/yuhong1103/p/12521112.html