• LintCode-Maximum Subarray II


    Given an array of integers, find two non-overlapping subarrays which have the largest sum.

    The number in each subarray should be contiguous.

    Return the largest sum.

    Note

    The subarray should contain at least one number

    Example

    For given [1, 3, -1, 2, -1, 2], the two subarrays are [1, 3] and [2, -1, 2] or [1, 3, -1, 2] and [2], they both have the largest sum 7.

    Challenge

    Can you do it in time complexity O(n) ?

    Analysis:

    We need two non-overlapping subarrays, so there must be some point X so that the maximum subarray before X (not necessarily end at X) + the maximum subarray after X is max.

    So, we first calculate the max subarray end at each point from left to right and from right to left;

    Then, we account the max subarray before and after each point;

    At last, we find out the result.

    Solution:

     1 public class Solution {
     2     /**
     3      * @param nums: A list of integers
     4      * @return: An integer denotes the sum of max two non-overlapping subarrays
     5      */
     6     public int maxTwoSubArrays(ArrayList<Integer> nums) {
     7         if (nums.size()<2) return 0;
     8         int len = nums.size();
     9 
    10         //Calculate the max subarray from left to right and from right to left.
    11         int[] left = new int[len];
    12         left[0] = nums.get(0);
    13         for (int i=1;i<len;i++)
    14             left[i] = Math.max(left[i-1]+nums.get(i), nums.get(i));
    15         int curMax = left[0];
    16         for (int i=1;i<len;i++)
    17             if (left[i]<curMax){
    18                 left[i] = curMax;
    19             } else curMax = left[i];
    20 
    21         int[] right = new int[len];
    22         right[len-1]=nums.get(len-1);
    23         for (int i=len-2;i>=0;i--)
    24             right[i] = Math.max(right[i+1]+nums.get(i),nums.get(i));
    25         curMax = right[len-1];
    26         for (int i=len-2;i>=0;i--)
    27             if (right[i]<curMax) right[i] = curMax;
    28             else curMax = right[i];
    29 
    30         //Find out the result.
    31         int res = Integer.MIN_VALUE;
    32         for (int i=0;i<len-1;i++)
    33             if (left[i]+right[i+1]>res)
    34                 res = left[i]+right[i+1];
    35         return res;
    36     }
    37 }
  • 相关阅读:
    【c#】无法修改“xxx”的返回值,因为它不是变量
    【c#】在C#中属性不可作为 ref 或 out 参数传递
    【概念】浮点数
    【概念】Winform
    【概念】数据库、服务器、N层架构、.NET、上位机、C/S和B/S、MVC、ADO.NET
    【c#】串口通信汇总
    【总线】UART、Modbus、I2C、SPI、RS232、RS485及串口通讯常用参数
    zookeeper应用场景
    Zookeeper选举(fastleaderelection算法)
    ZAB协议
  • 原文地址:https://www.cnblogs.com/lishiblog/p/4183818.html
Copyright © 2020-2023  润新知