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 ...
随机推荐
- iframe父窗口和子窗口之间的调用
1>父窗口获取子窗口 js方法 document.getElementById('if1').contentWindow.document: window.frames["if1&qu ...
- 什么是springMvc的参数绑定?
参数绑定通俗来讲就是从页面传过来的数据通过SpringMvc进行接收.接收的数据类型可以有: (1)SpringMvc默认支持的类型:request.session.application等. (2) ...
- css样式: 宽高按一定比例进行自适应
纯 CSS 实现高度与宽度成比例的效果 最近在做一个产品列表页面,布局如右图所示.页面中有若干个 item,其中每个 item 都向左浮动,并包含在自适应浏览器窗口宽度的父元素中. item 元素的 ...
- 深入浅出 JMS(一) - JMS 基本概念
深入浅出 JMS(一) - JMS 基本概念 一.JMS 是个什么鬼 JMS 是 Java Message Service 的简称,即 Java 消息服务.什么是消息服务呢,我们来看一下 Oracle ...
- org.apache.commons札记
StringUtils.isBlank(null); //trueStringUtils.isBlank(""); //trueStringUtils.isBlank(" ...
- 彻底测试全部拷贝list相关操作的区别python
1.用浅拷贝后修改数字,可以起到与原数据分离的效果 import copy origin = [, , [, ]] #origin 里边有三个元素:, ,[, ] cop1=origin.copy() ...
- 测试这个才可以打包 我的PYQt matplotlib numpy 等程序
from distutils.core import setup import py2exe import matplotlib import sys import FileDialog import ...
- 20155207 2016-2017-2 《Java程序设计》第九周学习总结
20155207 2016-2017-2 <Java程序设计>第九周学习总结 教材学习内容总结 第16章 整合数据库 16.1 JDBC入门 16.1.1 JDBC简介 数据库本身是个独立 ...
- java.lang.NoClassDefFoundError Could not initialize class 异常的处理
class,forname的配置文件出问题核对url数据库中的名字和bean中名字不同没有把jar包变成build path
- HDU6029 Graph Theory 2017-05-07 19:04 40人阅读 评论(0) 收藏
Graph Theory Time Limit: 2000/1000 M ...
