918. Maximum Sum Circular Subarray
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]when0 <= i < A.length, andC[i+A.length] = C[i]wheni >= 0.)Also, a subarray may only include each element of the fixed buffer
Aat most once. (Formally, for a subarrayC[i], C[i+1], ..., C[j], there does not existi <= k1, k2 <= jwithk1 % A.length = k2 % A.length.)
Example 1:
Input: [1,-2,3,-2]
Output: 3
Explanation: Subarray [3] has maximum sum 3Example 2:
Input: [5,-3,5]
Output: 10
Explanation: Subarray [5,5] has maximum sum 5 + 5 = 10Example 3:
Input: [3,-1,2,-1]
Output: 4
Explanation: Subarray [2,-1,3] has maximum sum 2 + (-1) + 3 = 4Example 4:
Input: [3,-2,2,-3]
Output: 3
Explanation: Subarray [3] and [3,-2,2] both have maximum sum 3Example 5:
Input: [-2,-3,-1]
Output: -1
Explanation: Subarray [-1] has maximum sum -1
Note:
-30000 <= A[i] <= 300001 <= A.length <= 30000
Approach #1: Array. [Java]
class Solution {
public int maxSubarraySumCircular(int[] A) {
int curMax = 0, sumMax = -30000,
curMin = 0, sumMin = 30000, total = 0;
for (int i = 0; i < A.length; ++i) {
curMax = Math.max(curMax + A[i], A[i]);
sumMax = Math.max(sumMax, curMax);
curMin = Math.min(curMin + A[i], A[i]);
sumMin = Math.min(curMin, sumMin);
total += A[i];
}
return sumMax > 0 ? Math.max(sumMax, total - sumMin) : sumMax;
}
}
Analysis:
There are two case.
The first is that the subarray take only a middle part, and we know how to find the max subarray sum.
The second is that the subarray take a part of head array and a part of tail array.
We can transfer this case to the first one.
The maximum result equals to the total sum minus the minimum subarray sum.
Here is a diagram by @mototix:
So the max subarray cricular sum equals to
max(the max subarray sum, the total sum - the min subarray sum)
Corner case:
Just one to pay attention:
If all number are negative, maxSum = max(A) and minSum = sum(A). In this case, max(maxSum, total - minSum) = 0, which means the sum of an empty subarray. According to the deacription, We need to return the max(A), instead of sum of an empty subarray. So we return the maxSum to handle this corner case.
Complexity:
One pass, time O(N).
No extra space, space O(1)
Reference:
https://leetcode.com/problems/maximum-sum-circular-subarray/discuss/178422/One-Pass
918. Maximum Sum Circular Subarray的更多相关文章
- 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 ...
- [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 ...
- [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 ...
- Maximum Sum Circular Subarray LT918
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 ...
随机推荐
- 使用jmeter工具测试上传接口
1.方法选择post:上传都是post上传. 2.路径输入正确的上传接口路径,并勾选Use multipart/form-data for POST 3.添加文件,文件路径尽量不要有中文,防止编码问题 ...
- C#调用默认浏览器打开网页的几种方法
private void button1_Click(object sender, EventArgs e) { //从注册表中读取默认浏览器可执行文件路径 RegistryKey key = Reg ...
- OpenSSL编程
简介 OpenSSL是一个功能丰富且自包含的开源安全工具箱.它提供的主要功能有:SSL协议实现(包括SSLv2.SSLv3和TLSv1).大量软算法(对称/非对称/摘要).大数运算.非对称算法密钥生成 ...
- Mysql的内存优化
老师 vi mysqld_safe# executing mysqld_safe 后面增加export LD_PRELOAD=/usr/local/lib/libtcmalloc.so 可以做一 ...
- process概念
multiprocess: multiprocess.cpu_count():统计cpu核数 multiprocess.active_chirdren():获取所有的子进程 multiprocess. ...
- cocos2d-x 3.4版本,videoPlayer和webView上添加sprite等cocos控件
本帖源于小弟自己在项目过程中为了解决在cocos2d-x中实现ios里videoPlayer自定义控件的需求,所以挖出来大神的一个帖子http://www.cocoachina.com/bbs/rea ...
- flask_web表单
一.配置 1.为了能够处理 web 表单,我们将使用 Flask-WTF,该扩展封装了 WTFForms 并且恰当地集成进 Flask 中.许多 Flask 扩展需要大量的配置,因此我们将要在 mic ...
- 如何更改myecelipse、eclipse的Project Explorer的背景颜色
这个是我研究了很久发现的,因为myecelipse.eclipse本身是随着系统的颜色改变而改变的,windows系统都会随着系统改变而改变的,所以找了很多资料都没能改变它的背景色,今天发现了一个不错 ...
- 优秀前端工程师必备:" checkbox & radio--单钩 & 多钩 "大比较:你是♂||♀ , 还是 ♂&♀
1 单选: type="radio" 需求: 男女input只能选择一个 <input type="radio" name="sex" ...
- Spinner功能和用法
书中只是简单写了选择的界面,没有写出选择之后的结果显示,我做了进一步功能. MainActivity.java public class MainActivity extends Activity { ...
