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:

  1. Input: [1,-2,3,-2]
  2. Output: 3
  3. Explanation: Subarray [3] has maximum sum 3

Example 2:

  1. Input: [5,-3,5]
  2. Output: 10
  3. Explanation: Subarray [5,5] has maximum sum 5 + 5 = 10

Example 3:

  1. Input: [3,-1,2,-1]
  2. Output: 4
  3. Explanation: Subarray [2,-1,3] has maximum sum 2 + (-1) + 3 = 4

Example 4:

  1. Input: [3,-2,2,-3]
  2. Output: 3
  3. Explanation: Subarray [3] and [3,-2,2] both have maximum sum 3

Example 5:

  1. Input: [-2,-3,-1]
  2. Output: -1
  3. Explanation: Subarray [-1] has maximum sum -1

Note:

  1. -30000 <= A[i] <= 30000
  2. 1 <= A.length <= 30000
Runtime: 76 ms, faster than 100.00% of C++ online submissions for Maximum Sum Circular Subarray.
Memory Usage: 13.2 MB, less than 0.77% of C++ online submissions for Maximum Sum Circular Subarray.
 
  1. class Solution {
  2. public:
  3. int maxSubarraySumCircular(vector<int>& A) {
  4. int total = ;
  5. int curmax = , maxsum = INT32_MIN;
  6. int curmin = , minsum = INT32_MAX;
  7. for(int a : A) {
  8. curmax = max(curmax + a, a);
  9. maxsum = max(maxsum, curmax);
  10. curmin = min(curmin + a, a);
  11. minsum = min(minsum, curmin);
  12. total += a;
  13. }
  14. return maxsum > ? max(maxsum, total - minsum) : maxsum;
  15. }
  16. };

LC 918. Maximum Sum Circular Subarray的更多相关文章

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

  2. 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. 689. Maximum Sum of 3 Non-Overlapping Subarrays三个不重合数组的求和最大值

    [抄题]: In a given array nums of positive integers, find three non-overlapping subarrays with maximum ...

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

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

随机推荐

  1. Django之form表单详解

    构建一个表单 假设你想在你的网站上创建一个简单的表单,以获得用户的名字.你需要类似这样的模板: <form action="/your-name/" method=" ...

  2. MySQL 是怎样运行的:从根儿上理解 MySQL:字符集和比较规则

    本文章借鉴自https://juejin.im/book/5bffcbc9f265da614b11b731 字符集和比较规则简介 一些重要的字符集 ASCII字符集 共收录128个字符,包括空格.标点 ...

  3. 【Leetcode】【简单】【26. 删除排序数组中的重复项】【JavaScript】

    题目描述 26. 删除排序数组中的重复项 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 ...

  4. 六、用DataContractSerialize类序列化XML

    一.层次结构 基类:XmlObjectSerializer 派生类: DataContractSerializer NetDataContractSerializer DataContractJson ...

  5. 一次vaccum导致的事故

    1. 问题出现 晚上9点,现场报系统查询慢,运维查询zabbix后发现postgres最近几天的IOWait很大 2. 追踪问题 查询数据库,发现很多SQL堵住了 原因是真正创建index,导致表锁住 ...

  6. Python之Linux命令

    1.查看当前文件路径  :  pwd LangYingdeMacBook-Pro:Users langying$ pwd /Users 2.切换目录 cd 例如:切换到根目录  : cd /   回到 ...

  7. DOM设置css样式

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  8. jQuery理解与运用

    1. 什么是jQuery 它是一个轻量级的javascript类库,别人写好的一个类. 2. jQuery优点 2.1 总是面向集合  2.2 多行操作集于一行      注1:就一个类“jQuery ...

  9. Codeforces Round #528 (Div. 2)题解

    Codeforces Round #528 (Div. 2)题解 A. Right-Left Cipher 很明显这道题按题意逆序解码即可 Code: # include <bits/stdc+ ...

  10. 03_Elastic部署

    ES集群部署 elastic不能用root用户去启动,否则会报错,所以创建elastic用户 1.创建elastic用户 $ useradd elastic -s /sbin/nologin 2..部 ...