Given an integer array nums
, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
Example:
Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.
Follow up:
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
题目大意:求数组最大子数组之和
解题思路:
<note:find this explanation in disscuss of LeetCode>
Let's think about optimal solution. Let's say we have the optimal solution which we found after traversing the array till ith positition. What can we say about the solution.
i) Since it is at position i it should include nums[i]
ii) Secondly, it either includes dp[i-1] or it doesn't. If including dp[i-1] increases dp[i] then we would have included dp[i-1] otherwise not.
Based on above thought, we can write our dp equation as:
dp[i] = max(dp[i-1] + nums[i], nums[i])
We can go and use the bottom up approach and find the maximum sum but we don't know which value of i has the maximum sum.
In order to do that we need to keep track of maximum sum encountered so far. It's similar to finding a maximum number in an array.
which just boils down to :
maxSum = max(dp[i], maxSum)
Now Let's observe the solution and we can notice we don't care for what was in the dp[i-2] we just need to keep track of last entry which gives us hint on how to optimize the space complexity down to O(1).
So instead of maintaining an array we can use a single variable.
思路:
首先根据数组最后一个元素num[n-1] 与最大子数组的关系,分为以下三种情况:
public int maxSubArray(int[] nums) {
int[] dp = new int[nums.length];
dp[0] = nums[0];
int max = dp[0];
for (int i = 1;i<dp.length;i++){
dp[i] = Math.max(dp[i-1]+nums[i],nums[i]);
max = Math.max(max,dp[i]);
}
return max;
}
优化的动态规划方法。实际上,解法一中的dp数组的中间值我们并不关心,我们只是想知道最后dp[n-1],所以这个dp数组可以用一个变量代替。如此,可以将空间复杂度压缩到O(1)级别。
public int maxSubArray(int[] nums) {
int curSum = nums[0];
int resSum = nums[0];
for (int i = 1;i<nums.length;i++){
curSum = Math.max(curSum+nums[i],nums[i]);
resSum = Math.max(curSum,resSum);
}
return resSum;
}
Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.
Note that you cannot sell a stock before you buy one.
Example 1:
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.
Example 2:
Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.
利用LeetCode53 的方法计算最大的子数组。
public int maxSubArray(int[] nums) {
int curSum = nums[0];
int resSum = nums[0];
for (int i = 1;i<nums.length;i++){
curSum = Math.max(curSum+nums[i],nums[i]);
resSum = Math.max(curSum,resSum);
}
return resSum;
}
public int maxProfit(int[] prices) {
if (prices ==null||prices.length==0) return 0;
int[] diff = new int[prices.length];
diff[0] = 0;
for (int i = 1 ; i< prices.length ;i++){
diff[i] = prices[i]-prices[i-1];
}
return maxSubArray(diff);
}
采用双指针法。当prices[begin]<prices[end]时候就可以求出来一个差值,所有的差值中求最大的值就是要求的结果。
public int maxProfit(int[] prices) {
int begin = 0;
int end = 1;
int profit = 0;
while (begin<prices.length&&end<prices.length){
if (prices[begin]>prices[end]){
begin = end;
end++;
continue;
}
profit = Math.max(profit,prices[end] - prices[begin]);
end++;
}
return profit;
}
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).
Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
Example 1:
Input: [7,1,5,3,6,4]
Output: 7
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
Example 2:
Input: [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
engaging multiple transactions at the same time. You must sell before buying again.
Example 3:
Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.
题目大意:可以购买多次,但是必须卖出后才能进行下一次的购买。
计算所有的prices[i]<prices[i+1]的差值之和。
public int maxProfit(int[] prices) {
int maxProfit =0;
if(prices.length<1) return maxProfit;
for(int i=0;i<prices.length-1;i++){
if(prices[i]<prices[i+1]){
maxProfit+=prices[i+1]-prices[i];
}
}
return maxProfit;
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。