Maximum Sum Circular Subarray LT918
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:
-30000 <= A[i] <= 300001 <= A.length <= 30000
Maximum Sum Circular Subarray LT918的更多相关文章
- 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 ...
- [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 ...
- 918. Maximum Sum Circular Subarray
Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...
- [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 ...
- 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 ...
- 动态规划-Maximum Subarray-Maximum Sum Circular Subarray
2020-02-18 20:57:58 一.Maximum Subarray 经典的动态规划问题. 问题描述: 问题求解: public int maxSubArray(int[] nums) { i ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- 十进制 -> 十六进制
x /16 依次取余 ,最先余作十六进制的最低 字节有效位,最后的余数 作最高的字节有效位,其中我们需要注意理解 的是 一个 数据的 最高字节及 内存的 高地址及低地址 更进一步的就算机器存储的 ...
- Burp Suite设置代理
一.确认代理信息 打开Burp Suite,查看proxy->options,看到Interface一栏为127.0.0.1:8080. 二.在Firefox中设置代理服务器 可以下载工具Fox ...
- 《2018面向对象程序设计(Java)课程学习进度条》
周次 (阅读/编写)代码行数 发布博客量/博客评论数量 课堂/课余学习时间(小时) 最满意的编程任务 第一周 50/40 1/0 6/4 九九乘法表 第二周 100/80 1/0 6/8 实验5,6, ...
- [C#]打包项目[转]
原文:https://www.cnblogs.com/danyu/p/7243706.html 加入自定义操作:https://blog.csdn.net/ristal/article/details ...
- Android App专项测试(压力测试)
转载https://blog.csdn.net/qq_29794757/article/details/64160303 转载https://blog.csdn.net/xuejiaodream/ar ...
- JetBrains 产品线破解方法
参考: 1.https://www.jianshu.com/p/f404994e2843 2.https://xclient.info/s/intellij-idea.html#versions 3. ...
- @PostConstruct注解小结
1.在具体Bean的实例化过程中,@PostConstruct注解的方法,会在构造方法之后,init方法之前进行调用2.在项目中@PostConstruct主要应用场景是在初始化Servlet时加载一 ...
- springboot 开启事务以及手动提交事务
添加依赖,sprongboot 会默认开启事务管理 org.springframework.boot spring-boot-starter-jdbc 在需要的服务类里添加注解 @Autowired ...
- a 标签实现分享功能
在网页中,经常会用到分享功能,例如分享到qq,分享到微信,分享到微博等,但是怎么实现呢?一直没有想清楚这个问题,觉得好高大上的样子,于是在网上找了一些资料,也没有看出个什么所以然来: 于是有些心急了, ...
- Jenkins+Git+Maven构建并部署springboot(构建多模块中的单个模块)
主要思路:1.jenkins从git中拉取项目源码:jenkins使用maven构建并将生成的jar包通过shell脚本启动. 环境:环境:Centos7.Maven3.5.3.git(单机) 准备工 ...