Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty subarray of C.

Here, a circular array means the end of the array connects to the beginning of the array.  (Formally, C[i] = A[i] when 0 <= i < A.length, and C[i+A.length] = C[i] when i >= 0.)

Also, a subarray may only include each element of the fixed buffer A at most once.  (Formally, for a subarray C[i], C[i+1], ..., C[j], there does not exist i <= k1, k2 <= j with k1 % A.length = k2 % A.length.)

Example 1:

Input: [1,-2,3,-2]
Output: 3
Explanation: Subarray [3] has maximum sum 3

Example 2:

Input: [5,-3,5]
Output: 10
Explanation: Subarray [5,5] has maximum sum 5 + 5 = 10

Example 3:

Input: [3,-1,2,-1]
Output: 4
Explanation: Subarray [2,-1,3] has maximum sum 2 + (-1) + 3 = 4

Example 4:

Input: [3,-2,2,-3]
Output: 3
Explanation: Subarray [3] and [3,-2,2] both have maximum sum 3

Example 5:

Input: [-2,-3,-1]
Output: -1
Explanation: Subarray [-1] has maximum sum -1

Idea 1. Similar to Maximum Subarray LT53, the difference is the circular array part, the subarray could be the tail(A[j..n-1]) plus the head(A[0..i]) (i+1 < j), if the subarray of tail + head is the maximum, the subarray in the middle is A[i+1, j-1] has the minSum, once the minSum is found, the maxSum is Sum - minSum, except maxSum = 0 if all values in the array are negative, as minSum == Sum.

Note. negative arrays

Time complexity: O(n)

Space complexity: O(1)

 class Solution {
public int maxSubarraySumCircular(int[] A) {
int maxSoFar = 0;
int minSoFar = 0; int sum = 0;
int minSum = Integer.MAX_VALUE;
int maxSum = Integer.MIN_VALUE; for(int a: A) {
maxSoFar = a + Math.max(maxSoFar, 0);
maxSum = Math.max(maxSum, maxSoFar); minSoFar = a + Math.min(minSoFar, 0);
minSum = Math.min(minSum, minSoFar); sum += a;
} if(sum == minSum) {
return maxSum;
}
return Math.max(maxSum, sum - minSum);
}
}

Idea 2. 另外一种方法求2个intervals (head + tail)组成的,如果直接从左到右+从右到左的和,可能中间有重合,固定一边,求另外一边的和的最大值,保证j >= i+2.

Time complexity: O(n)

Space complexity: O(n)

 class Solution {
public int maxSubarraySumCircular(int[] A) {
int curr = 0;
int n = A.length;
int maxSum = Integer.MIN_VALUE; for(int a: A) {
curr = a + Math.max(curr, 0);
maxSum = Math.max(maxSum, curr);
} int[] rightMaxSum = new int[n];
rightMaxSum[n-1] = A[n-1];
curr = A[n-1];
for(int i = n-2; i >= 0; --i) {
curr += A[i];
rightMaxSum[i] = Math.max(curr, rightMaxSum[i+1]);
} curr = 0;
int result = maxSum;
for(int i = 0; i+2 < n; ++i) {
curr += A[i];
result = Math.max(result, curr + rightMaxSum[i+2]);
} return result;
}
}

Idea 3. Similar to sliding window minValue, build a min deque for prefix sum, for each prefix sum, find the minimum of previous prefixSum so that the subarray sum ending here is the maximu, since it's circular array, j - i <= n, remove the invalid previous index. Note: store the index

Time complexity: O(n)

Space complexity: O(n)

 class Solution {
public int maxSubarraySumCircular(int[] A) {
Deque<Integer> minPrefix = new LinkedList<>();
minPrefix.addLast(0); int n = A.length; int[] prefix = new int[2*n + 1];
for(int i = 0; i < 2*n; ++i) {
prefix[i+1] = prefix[i] + A[i%n];
}
int result = Integer.MIN_VALUE;
for(int i = 1; i <= 2*n; ++i) { if(i - minPrefix.getFirst() > n) {
minPrefix.removeFirst();
} result = Math.max(result, prefix[i] - prefix[minPrefix.getFirst()]); while(!minPrefix.isEmpty() && prefix[minPrefix.getLast()] >prefix[i]) {
minPrefix.removeLast();
}
minPrefix.add(i); } return result;
}
}

Note:

  1. -30000 <= A[i] <= 30000
  2. 1 <= A.length <= 30000

Maximum Sum Circular Subarray LT918的更多相关文章

  1. LC 918. Maximum Sum Circular Subarray

    Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...

  2. [Swift]LeetCode918. 环形子数组的最大和 | Maximum Sum Circular Subarray

    Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...

  3. 918. Maximum Sum Circular Subarray

    Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...

  4. [LeetCode] 918. Maximum Sum Circular Subarray 环形子数组的最大和

    Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...

  5. Leetcode Week5 Maximum Sum Circular Subarray

    Question Given a circular array C of integers represented by A, find the maximum possible sum of a n ...

  6. 动态规划-Maximum Subarray-Maximum Sum Circular Subarray

    2020-02-18 20:57:58 一.Maximum Subarray 经典的动态规划问题. 问题描述: 问题求解: public int maxSubArray(int[] nums) { i ...

  7. [LeetCode] Maximum Sum of 3 Non-Overlapping Subarrays 三个非重叠子数组的最大和

    In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. E ...

  8. [Swift]LeetCode689. 三个无重叠子数组的最大和 | Maximum Sum of 3 Non-Overlapping Subarrays

    In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. E ...

  9. [leetcode]689. Maximum Sum of 3 Non-Overlapping Subarrays三个非重叠子数组的最大和

    In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. E ...

随机推荐

  1. py库:numpy

    http://www.numpy.org/ numpy官网 http://cwiki.apachecn.org/pages/viewpage.action?pageId=10030181 scikit ...

  2. 在下载SOPC代码的过程中遇到的一些错误

    (1)Error (209015): Can't configure device. Expected JTAG ID code 0x02D120DD for device 2, but found ...

  3. spark单机搭建

    说明:单机版的Spark的机器上只需要安装Scala和JDK即可,其他诸如Hadoop.Zookeeper之类的东西可以一概不安装 只需下载如下三个包 1.安装jdk 配置环境变量 vim /etc/ ...

  4. python练习题_03

    1.写函数: 根据范围获取其中3和7整除的所有数的和,并返回调用者: 符合条件的数字个数以及符合条件的数字的总和,如:def func(start,end): def func(start,end): ...

  5. ---rk3288 mipi 整发(适用于新版的kernel 4.4 )

    http://www.pianshen.com/article/7245318143/ 老的Anroid 5.1 下 Linux 3.10 的数据的名字和 处理方式有不少不同 不过rk3128 还在走 ...

  6. NSIS 查找目录下的所有文件夹

    在制作安装包的时候,需要查看下某一目录下的所有文件夹的名称.经过查资料发现需要用到NSIS 的一个插件Locate.下载该插件后,会在NSIS的安装目录 ...Program Files\NSIS\P ...

  7. Kafka运维填坑(转)

    前提: 只针对Kafka 0.9.0.1版本; 说是运维,其实偏重于问题解决; 大部分解决方案都是google而来, 我只是作了次搬运工; 有些问题的解决方案未必一定是通用的, 若应用到线上请慎重; ...

  8. java学习-- String

    String 类的实例是不可改变的,所以你一旦创建了 String 对象,那它的值就无法改变了 String 类是不可改变的解析,例如: String s = "Google"; ...

  9. PowerDesigner工具将表字段转成java实体

    首先将数据库的表导出为SQL文件.下载安装PowerDesigner工具. 下面以图文的形式讲解: 图   (1) 图   (2) 图   (3) 图   (4) 图   (5) 图   (6) 图 ...

  10. laravel passport加密jwt格式的access_token中的sub(user_id)字段

    在很多需求我们不希望别人知道用户在我们表中的 user_id :但是又想用数据库的自增 id 功能:一般时候在取出用户后加密 user_id 加密即可:但是总有那么几个不经意间就可能把我们的 user ...