zoukankan      html  css  js  c++  java
  • Maximum Sum Subarray

    题目:

    Find the contiguous subarray within an array (containing at least one number) which has the largest sum.

    For example, given the array [−2,1,−3,4,−1,2,1,−5,4],
    the contiguous subarray [4,−1,2,1] has the largest sum = 6.

    代码:

     1 public class Solution {
     2     public int maxSubArray(int[] nums) 
     3     {
     4         int len = nums.length;
     5         int SubSum = nums[0];
     6         int MaxSum = nums[0];
     7         for(int i=1;i<len;i++)
     8         {
     9             SubSum=Math.max(nums[i],SubSum+nums[i]);
    10             MaxSum=Math.max(MaxSum,SubSum);
    11             
    12         }
    13         
    14         return MaxSum;
    15         
    16     }
    17 }

    DP Solution:

    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:

    public int maxSubArray(int[] A) {
            int n = A.length;
            int[] dp = new int[n];//dp[i] means the maximum subarray ending with A[i];
            dp[0] = A[0];
            int max = dp[0];
    
            for(int i = 1; i < n; i++){
                dp[i] = A[i] + (dp[i - 1] > 0 ? dp[i - 1] : 0);
                max = Math.max(max, dp[i]);
            }
    
            return max;
    } 

    reference: https://leetcode.com/discuss/19614/dp-solution-%26-some-thoughts

  • 相关阅读:
    对百度搜索法的分析评价
    第二阶段第十次spring会议
    课下作业——典型用户和用处场景
    第二阶段第九次spring会议
    第二阶段第八次spring会议
    第二阶段第七次spring会议
    第二阶段第六次spring会议
    第二阶段第五次spring会议
    第二阶段第四次spring会议
    第二阶段第三次spring会议
  • 原文地址:https://www.cnblogs.com/hygeia/p/4643898.html
Copyright © 2011-2022 走看看