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
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.
 
class Solution {
public:
int maxSubarraySumCircular(vector<int>& A) {
int total = ;
int curmax = , maxsum = INT32_MIN;
int curmin = , minsum = INT32_MAX;
for(int a : A) {
curmax = max(curmax + a, a);
maxsum = max(maxsum, curmax);
curmin = min(curmin + a, a);
minsum = min(minsum, curmin);
total += a;
}
return maxsum > ? max(maxsum, total - minsum) : maxsum;
}
};

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. CentOS7.X+LAMP环境下安装zabbix4.X

    1.安装zabbix仓库 rpm -ivh https://repo.zabbix.com/zabbix/4.2/rhel/7/x86_64/zabbix-release-4.2-1.el7.noar ...

  2. charles overvoew

    本文参考:charles overvoew 这里是请求文件的概览 对HTTP/s分析非常重要 基本上常见的HTTP相关字段都有: Charles is a web proxy (HTTP Proxy ...

  3. mybatis遍历map

    mytabis是可以遍历map的,试过很多其他的方法都不行,最终使用如下方法是可以的: 1.mapper.java如下(注意要加@Param注解,否则不行,我就在这里折腾了不少时间): int upd ...

  4. python小知识点汇总

    一 写mysql 场景:自动化用例中需要构造数据,写入redis 表中已有该primary_key的记录,在现有基础上更新字段 表中无该记录,需要插入 # 请求传入table字段,db.table,c ...

  5. 【转】如何在TensorFlow中高效使用数据集

    本文主要记录tensorflow一个比较好用的API:Dataset,feed-dict 是向 TensorFlow 传递信息最慢的方式,应该尽量避免使用.向模型提供数据的正确方式是使用输入管道,这样 ...

  6. Educational Codeforces Round 41 (Rated for Div. 2) D. Pair Of Lines (几何,随机)

    D. Pair Of Lines time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  7. linux网络编程之共享内存介绍

    今天是个好日子,洋人之节乃全球同庆,圣诞一来感觉就要过年了,不过今晚心情有点打折扣,给心爱的人打电话没有打通,本想在平安夜送上快乐的祝福给她,糟糕的心情让自己好像泄了气的皮球一样,无精打彩,心情灰暗, ...

  8. MySql 数据库 SQLException: The user specified as a definer ('root'@'%') does not exist 错误原因及解决办法

    The user specified as a definer ('root'@'%') does not exist 此种报错主要是针对访问视图文件引起的(没有权限) 经查明:是用户root并没有获 ...

  9. 物联网之窄带物联网(NB-IOT)

    NB-IoT即窄带物联网(Narrow Band Internet of Things),NB-IOT构建在蜂窝网络之上,只消耗大约180KHZ的带宽,可直接部署于GSM(2G).UMTS(3G).L ...

  10. sql server 存储过程中,调用事务 tran

      Sql Server 2005/2008中提供了begin tran,commit tran和rollback tran来使用事务. begin tran表示开始事务, commit tran表示 ...