首页 技术 正文
技术 2022年11月14日
0 收藏 403 点赞 4,389 浏览 2932 个字

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
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,985
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,501
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,345
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,128
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,763
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,839