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

Note:

  1. -30000 <= A[i] <= 30000
  2. 1 <= 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的更多相关文章

  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. [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 ...

  3. [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 ...

  4. Maximum Sum Circular Subarray LT918

    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. PM2使用及介绍

    pm2 是一个带有负载均衡功能的Node应用的进程管理器.当你要把你的独立代码利用全部的服务器上的所有CPU,并保证进程永远都活着,0秒的重载, PM2是完美的.它非常适合IaaS结构,但不要把它用于 ...

  2. NSNotificationCenter 注意

    成对出现 意思很简单,NSNotificationCenter消息的接受线程是基于发送消息的线程的.也就是同步的,因此,有时候,你发送的消息可能不在主线程,而大家都知道操作UI必须在主线程,不然会出现 ...

  3. 着重基础之—MySql Blob类型和Text类型

    着重基础之—MySql Blob类型和Text类型 在经历了几个Java项目后,遇到了一些问题,在解决问题中体会到基础需要不断的回顾与巩固. 最近做的项目中,提供给接口调用方数据同步接口,传输的数据格 ...

  4. Oracle零碎总结:结构-工具-创建语句

    前言:Oracle内部的存储及管理结构是1.数据库系统:2.数据库实例:3.表空间,系统用户system,普通用户:表,视图,触发器,存储过程等: 一.Oracle数据库系统和数据库实例的对应关系是一 ...

  5. Matlab 中以分数显示结果

    转http://www.blogbus.com/shijuanfeng-logs/234881647.html Matlab,计算得到的结果一般是小数形式. 但为了更精确表示,我们有时候需要用到分数形 ...

  6. C语言中 指针的基础知识总结, 指针数组的理解

    1: 指针变量所占的字节数与操作系统为位数有关,64位操作系统下,占8个字节,32位操作系统下,占4个字节 2: 指针变量的本质是用来放地址,而一般的变量是放数值的3: 脱衣服法则: a[2] 变成 ...

  7. linux信号量(转载)

    本文转载自http://blog.csdn.net/qinxiongxu/article/details/7830537 信号量一.什么是信号量信号量的使用主要是用来保护共享资源,使得资源在一个时刻只 ...

  8. hdu 5036 概率+bitset

    http://acm.hdu.edu.cn/showproblem.php?pid=5036 n个房间每个房间里面有一把或多把钥匙可以打开其他的门.如果手上没有钥匙可以选择等概率随机选择一个门炸开,求 ...

  9. POJ1062不错的题——spfa倒向建图——枚举等级限制

    POJ1062 虽然是中文题目但是还是有一定几率都不准题目意思的:1.所有可能降价的措施不是降价多少钱而是降至多少钱2.等级范围:是你所走的那一条路中所有人中最好最低等级差不允许超过limit限制 思 ...

  10. <context:component-scan>自动扫描

    主要讲解自动扫描的<context:component-scan>中的2个子标签的使用方法 在Spring MVC中的配置中一般会遇到这两个标签,作为<context:compone ...