Commit beb751fa authored by zhayao's avatar zhayao
Browse files

keep going

parent b7972593
......@@ -1641,13 +1641,30 @@ public:
//hard to understand...... easy method but tried a lot of times
// https://www.youtube.com/watch?v=2MmGzdiKR9Y
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int max = nums[0];
int culumative = nums[0];
for(int i=1; i<nums.size(); i++){
int temp = culumative + nums[i];
culumative = std::max(nums[i], temp);
max = std::max(culumative, max);
}
return max;
}
};
Input: [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Not 7-1 = 6, as selling price needs to be larger than buying price.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment