leetcode 53-Maximum Subarray(medium)
Posted yshi12
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode 53-Maximum Subarray(medium)相关的知识,希望对你有一定的参考价值。
Given an integer array nums
, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
1. use an array to store the sum of 0->i num in nums;(the difference between two sum in this array is the subarray sum)
2. finding the maximum array is to find the maximum of the difference(a latter sum minus a former sum)
class Solution { public int maxSubArray(int[] nums) { if(nums.length==0) return 0; int sum=0; int[] record=new int[nums.length]; for(int i=0;i<nums.length;i++){ sum+=nums[i]; record[i]=sum; } int max=Integer.MIN_VALUE; int min=0; for(int i=0;i<nums.length;i++){ if(record[i]-min>max) max=record[i]-min; if(record[i]<min) min=record[i]; } return max; } }
2. DP: https://leetcode.com/problems/maximum-subarray/discuss/20193/DP-solution-and-some-thoughts
Analysis of this problem:
Apparently, this is a optimization problem, which can be usually solved by DP. So when it comes to DP, the first thing for us to figure out is the format of the sub problem(or the state of each sub problem). The format of the sub problem can be helpful when we are trying to come up with the recursive relation.
At first, I think the sub problem should look like: maxSubArray(int A[], int i, int j)
, which means the maxSubArray for A[i: j]. In this way, our goal is to figure out what maxSubArray(A, 0, A.length - 1)
is. However, if we define the format of the sub problem in this way, it‘s hard to find the connection from the sub problem to the original problem(at least for me). In other words, I can‘t find a way to divided the original problem into the sub problems and use the solutions of the sub problems to somehow create the solution of the original one.
So I change the format of the sub problem into something like: maxSubArray(int A[], int i)
, which means the maxSubArray for A[0:i ] which must has A[i] as the end element. Note that now the sub problem‘s format is less flexible and less powerful than the previous one because there‘s a limitation that A[i] should be contained in that sequence and we have to keep track of each solution of the sub problem to update the global optimal value. However, now the connect between the sub problem & the original one becomes clearer:
maxSubArray(A, i) = maxSubArray(A, i - 1) > 0 ? maxSubArray(A, i - 1) : 0 + A[i];
class Solution { public int maxSubArray(int[] nums) { int[] dp=new int[nums.length]; dp[0]=nums[0]; int max=nums[0]; for(int i=1;i<nums.length;i++){ dp[i]=nums[i]+(dp[i-1]>0?dp[i-1]:0); if(max<dp[i]) max=dp[i]; } return max; } }
in fact no need for the dp array, since we only need to store the max and the biggest sum array ending at i-1 position
class Solution { public int maxSubArray(int[] nums) { int max=nums[0]; int pre=nums[0]; for(int i=1;i<nums.length;i++){ pre=nums[i]+(pre>0?pre:0); max=Math.max(max, pre); } return max; } }
以上是关于leetcode 53-Maximum Subarray(medium)的主要内容,如果未能解决你的问题,请参考以下文章