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.

    click to show more practice.

    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.

    1.linear time method.

    2.

    public class Solution {
        public int maxSubArray(int[] A) {
            int len = A.length;
            int curr = A[0], next = A[0];
            for(int i = 1; i < len; ++i){
                next = (next <= 0) ? A[i] : next + A[i];
                curr = Math.max(curr, next); 
            }
            return curr;   
        }
    }
    

      

    divide and conquer method.

    public class Solution {
    	/**
    	 * This program is an implementation of divide-and-conquer method
    	 * @param A --an array of integer
    	 * @return the maximum sum of a subarray
    	 * @author Averill Zheng
    	 * @version 2014-06-04
    	 * @since JDK 1.7
    	 */
    	public int maxSubArray(int[] A) {
            return maxSubArrayHelper(A, 0, A.length);
        }
    	/**
    	 * The function is used to find the maximum sum of a contiguous subarray between index i (inclusive) and j
    	 * (exclusive).
    	 * @param A --an integer array.
    	 * @param i --start index which is included.	  
    	 * @param j --end index which is exclusive.
    	 * @return int --maximum sum of a contiguous subarray.
    	 */
    	private int maxSubArrayHelper(int[] A, int i, int j){
    		int max = Integer.MIN_VALUE;
    		if(j > i){
    			if(j == i + 1)
    				max = A[i];
    			else{
    				int mid = i + (j - i) / 2;
    				int leftMax = maxSubArrayHelper(A, i, mid);
    				int rightMax = maxSubArrayHelper(A,mid, j);
    				int crossMax = crossMax(A, i, j, mid);
    				max = Math.max(Math.max(leftMax, crossMax), rightMax);
    			}
    		}
    		return max;
    	}
    	private int crossMax(int[] A, int i, int j, int mid){
    		//[i,mid) is the first half and [mid, j) is the second half
    		//both of them contains at least one element
    		
    		//left max
    		int leftMax = A[mid - 1];
    		int leftSum = A[mid - 1];
    		for(int k = mid - 2; k > i - 1; --k){
    			leftSum += A[k];
    			leftMax = (leftMax < leftSum) ? leftSum : leftMax;
    		}
    		
    		//right max
    		int rightMax = A[mid];
    		int rightSum = A[mid];
    		for(int k = mid + 1; k < j; ++k){
    			rightSum += A[k];
    			rightMax = (rightMax < rightSum) ? rightSum : rightMax; 
    		}
    		return leftMax + rightMax;
    	}
    }
    

      

  • 相关阅读:
    如何将一棵树转化成二叉树
    雪碧图的使用
    CSS简介,引入方式,文字和文本样式设置
    表格Table和表单元素
    html 中< col>标签和< colgroup>标签的区别
    Emmet的HTML语法(敲代码的快捷方式)
    抖音风格字体效果
    几种有效减小电脑系统盘使用量的方法
    ubuntu 机器名称修改方法
    Ubuntu 为基于X应用程序增加启动项的正确做法
  • 原文地址:https://www.cnblogs.com/averillzheng/p/3771905.html
Copyright © 2011-2022 走看看