zoukankan      html  css  js  c++  java
  • [LeetCode] Maximum 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.

    More practice:

    If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.

    最笨的方法就是把所有i个数之和(1<=i<=n)的结果全算出来,比较最大的数返回。

    但在leetcode里这样做常常Time Limit Exceeded!!穷举的方法如下:(结果是对的)

    int maxSubArray(int A[], int n) {
        int max = A[0];
        for(int i=1;i<=n;i++)  // i个数相加
        {
            for(int j=0;j<=n-i;j++)//连续相加的数的起始位置j
            {
                int sum = 0;
                int j0 = j;
                for(int k=1;k<=i;k++)
                        sum += A[j0++];
                if(max<sum)
                    max = sum;
            }
        }
        return max;   
    }

    所以要思考简便的方法。

    1)Kadane算法(线型时间算法),见http://blog.csdn.net/joylnwang/article/details/6859677

    2)动态规划

    3)分治(这是LeetCode要求的算法)

    ~~~~各个算法都实现一下~(以下是LeetCode讨论里的经典做法)

     方法1:

    class Solution {
     public: int maxSubArray(int A[], int n) { 
         int s[n]; 
         int max = s[0] = A[0]; 
         for (int i = 1; i < n; i++) {
            s[i] = s[i-1] > 0 ? (A[i] + s[i-1]) : A[i]; 
            max = std::max(max, s[i]); 
         }
         return max;
     }
    };

    方法二:分治

    Step1. Select the middle element of the array. So the maximum subarray may contain that middle element or not.

    Step 2.1 If the maximum subarray does not contain the middle element, then we can apply the same algorithm to the the subarray to the left of the middle element and the subarray to the right of the middle element.

    Step 2.2 If the maximum subarray does contain the middle element, then the result will be simply the maximum suffix subarray of the left subarray plus the maximum prefix subarray of the right subarray

    Step 3 return the maximum of those three answer.

    Here is a sample code for divide and conquer solution. Please try to understand the algorithm before look at the code

    class Solution {
    public:
        int maxSubArray(int A[], int n) {
            // IMPORTANT: Please reset any member data you declared, as
            // the same Solution instance will be reused for each test case.
            if(n==0) return 0;
            return maxSubArrayHelperFunction(A,0,n-1);
        }
    
        int maxSubArrayHelperFunction(int A[], int left, int right) {
            if(right == left) return A[left];
            int middle = (left+right)/2;
            int leftans = maxSubArrayHelperFunction(A, left, middle);
            int rightans = maxSubArrayHelperFunction(A, middle+1, right);
            int leftmax = A[middle];
            int rightmax = A[middle+1];
            int temp = 0;
            for(int i=middle;i>=left;i--) {
                temp += A[i];
                if(temp > leftmax) leftmax = temp;
            }
            temp = 0;
            for(int i=middle+1;i<=right;i++) {
                temp += A[i];
                if(temp > rightmax) rightmax = temp;
            }
            return max(max(leftans, rightans),leftmax+rightmax);
        }
    };
  • 相关阅读:
    给数组赋值nan
    loc和iloc的区别
    爬虫26-部署crawl爬虫
    爬虫25-scrapy框架详解
    爬虫24-scrapy框架部署
    爬虫23-验证码识别
    爬虫22-使用selenium爬取信息
    爬虫21-selenium用法
    爬虫20-浏览器自动运行简单方法
    爬虫19-线程生产者和消费者以及队列
  • 原文地址:https://www.cnblogs.com/Xylophone/p/3813139.html
Copyright © 2011-2022 走看看