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. sqlserver查询区分大小写

    例子: select * from tb_students where name='jay' select * from tb_students where name='JAY' 这两句查询结果是一样 ...

  2. xmlhttp

    File an issue about the selected textFile an issue about the selected text XMLHttpRequest Living Sta ...

  3. ubuntu关闭防火墙

    https://jingyan.baidu.com/article/73c3ce283ee2c1e50343d9f6.html

  4. MetroApp保存UIEment为图片

    写本文的起因是想截取Metro App画面作为图片来使用Win8的共享. 话说自从大MS的客户端UI技术进入XAML时代之后,每次截屏的代码都不太一样,无论silverlight.WPF还是Windo ...

  5. 前端之css笔记3

    一 display属性 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  6. 2018.10.14 bzoj4571: [Scoi2016]美味(主席树)

    传送门 自认为是一道思想很妙的题. 直接分析问题. 如果没有xxx的干扰直接上可持久化01trie01trie01trie走人. 但现在有了xxx这个偏移量. 相当于把整个01trie01trie01 ...

  7. 2018.09.14 洛谷P3931 SAC E#1 - 一道难题 Tree(树形dp)

    传送门 简单dp题. f[i]表示以i为根的子树被割掉的最小值. 那么有: f[i]=min(∑vf[v],dist(i,fa))" role="presentation" ...

  8. 2018.07.09 顺序对齐(线性dp)

    顺序对齐 题目描述 考虑两个字符串右对齐的最佳解法.例如,有一个右对齐方案中字符串是 AADDEFGGHC 和 ADCDEGH. AAD~DEFGGHC ADCDE~~GH~ 每一个数值匹配的位置值 ...

  9. Django入门指南-第7章:模板引擎设置(完结)

    http://127.0.0.1:8000/ <!--templates/home.html--> <!DOCTYPE html> <html> <head& ...

  10. An existing resource has been found at location D:\Tomcat 7\apache-tomcat-7.0.55\webapps。。。

    这个错误是说你的资源丢失,就是说tomcat无法解析你的.class文件,需要自己重新配置一下. 解决方法: 右击项目名 ---> 点击properties --> 在搜索栏里 输入 WE ...