Question

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
Accepted
5,448
Submissions
19,793
 
Seen this question in a real interview before?

Answer

  有两种情况。
  
  第一,子数组只有中间部分,我们知道如何找到最大子数组求和。
  第二,是子数组头阵的一部分,尾巴数组的一部分。
  最大的结果等于总和减去最小值子数组只子数组求和。

    int maxSubarraySumCircular(vector<int>& A) {
int total = , maxSum = -, curMax = , minSum = , curMin = ;
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;
}

Leetcode Week5 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. 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 ...

  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. 918. Maximum Sum Circular Subarray

    Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...

  5. Maximum Sum Circular Subarray LT918

    Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...

  6. 动态规划-Maximum Subarray-Maximum Sum Circular Subarray

    2020-02-18 20:57:58 一.Maximum Subarray 经典的动态规划问题. 问题描述: 问题求解: public int maxSubArray(int[] nums) { i ...

  7. LeetCode 689. Maximum Sum of 3 Non-Overlapping Subarrays

    原题链接在这里:https://leetcode.com/problems/maximum-sum-of-3-non-overlapping-subarrays/ 题目: In a given arr ...

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

  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. 蓝桥杯2015年省赛C/C++大学B组

    1. 奖券数目 有些人很迷信数字,比如带“4”的数字,认为和“死”谐音,就觉得不吉利.虽然这些说法纯属无稽之谈,但有时还要迎合大众的需求.某抽奖活动的奖券号码是5位数(10000-99999),要求其 ...

  2. Spring学习笔记:自动创建Proxy

    为什么需要自动创建Proxy 手动为所有需要代理的类用ProxyFactoryBean创建代理Proxy需要大量的配置. 这样如果需要代理的类很多,配置就很繁琐,而且也不便于xml配置的维护. 因此S ...

  3. Hadoop fs 使用方法

    hdfs的基本命令 hdfs dfs -help    查看帮助 在HDFS的文件系统中,HDFS只支持绝对路径 1.-ls: 显示目录信息 hadoop fs -ls / 列出指定目录下的内容 2. ...

  4. pytorch之 RNN regression

    关于RNN模型参数的解释,可以参看RNN参数解释 1 import torch from torch import nn import numpy as np import matplotlib.py ...

  5. C标准库与嵌入式

    stddef.h,其中包括size_t,sizeof函数返回值,不同平台的大小不一致 Size and pointer difference types[edit] The C language sp ...

  6. 解析如何防止XSS跨站脚本攻击

    2012-11-20 09:03 (分类:网络安全) 这些规则适用于所有不同类别的XSS跨站脚本攻击,可以通过在服务端执行适当的解码来定位映射的XSS以及存储的XSS,由于XSS也存在很多特殊情况,因 ...

  7. zabbix-server配置文件详解

    zabbix官方文档:https://www.zabbix.com/documentation/4.0/zh/manual zabbix-server端的配置文件在/etc/zabbix/zabbix ...

  8. windows下python3使用pip安装scrapy提示安装失败

    我的环境:     python3.6,     win10,      原因:不能成功安装twisted,因为twisted与高版本的python有兼容问题. 解决:1,先下载twisted二进制文 ...

  9. 解决IIS程序池回收webapi定时程序造成的影响

    问题描述: webapi中有一个定时器线程,在iis程序池在1740分钟回收后,定时器中止 问题解决步骤: 1.设置程序池定期回收,设置每天定时回收 2.在windows自带的任务计划中,添加一条任务 ...

  10. 此Flash Player 与您的地区不相容,请重新安装Adobe Flash Player问题解决

    flash29老版本安装说明: 如果你是Google Chrome 54及以上版本,那么直接安装 install_flash_player_**_ppapi.exe 即可,Chrome 能识别加载,无 ...