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. Go语言实现:【剑指offer】用两个栈实现队列

    该题目来源于牛客网<剑指offer>专题. 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. Go语言实现: var list1 = list.New( ...

  2. redis命令总结与持久化

    上篇redis文章为大家介绍了redis与它的部署工作.这次我们来说一下redis的操作命令与持久化 一.命令总结 1)String操作 6379> set k1 v1 #设定值 6379> ...

  3. C++ 基础--虚构函数

    virtual 函数 示例代码如下: #include <stdio.h> class base { public: virtual void name(){printf("ba ...

  4. 09-SpringMVC03

    今日知识 1. SpringMVC自定义异常处理 2. SpringMVC的interceptor(过滤器) SpringMVC自定义异常处理 1.web.xml正常写 <servlet> ...

  5. 01-Maven

    今日知识 1. Maven 2. 依赖管理 2. 项目构建 Maven 1. Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具. 2. Ma ...

  6. 数据算法 --hadoop/spark数据处理技巧 --(3.左外连接 4.反转排序)

    三. 左外连接 考虑一家公司,比如亚马逊,它拥有超过2亿的用户,每天要完成数亿次交易.假设我们有两类数据,用户和交易: users(user_id,location_id) transactions( ...

  7. ELF文件之七——使用链接脚本-2个函数-data-bss-temp-call

    main.c int enable; ; int main() { int temp; add(); ; } int add() { ; } o反汇编的地址都是0起始,elf的地址都是映射后的地址. ...

  8. ospf路由协议源码学习

    目前,主要有两个版本的源码实现,一是quagga,一是bird. quagga的代码大概有3-4万行,有提到unnumbered interface, bird的代码大概1万行,但没有提到unnumb ...

  9. iptbales 允许访问vsftp

    1.允许20 21 端口iptables -I INPUT -p tcp -m multiport --dport 20,21 -j ACCEPT 2.允许关联包通过iptables -A INPUT ...

  10. SHELL下打包文件

    SHELL下打包文件 在我们拿下webshell的时候,想要获取数据或者源码往往会用菜刀或者蚁剑去打包,但是这个时候往往就会出现很多问题,列如打包失败,或者是打包得不完整等等. 这个时候如果对方是wi ...