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 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:
-30000 <= A[i] <= 30000
1 <= A.length <= 30000
- 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的更多相关文章
- [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 ...
- 918. Maximum Sum Circular Subarray
Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...
- [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 ...
- Maximum Sum Circular Subarray LT918
Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...
- 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 ...
- 动态规划-Maximum Subarray-Maximum Sum Circular Subarray
2020-02-18 20:57:58 一.Maximum Subarray 经典的动态规划问题. 问题描述: 问题求解: public int maxSubArray(int[] nums) { i ...
- 689. Maximum Sum of 3 Non-Overlapping Subarrays三个不重合数组的求和最大值
[抄题]: In a given array nums of positive integers, find three non-overlapping subarrays with maximum ...
- [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 ...
- [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 ...
随机推荐
- Django之form表单详解
构建一个表单 假设你想在你的网站上创建一个简单的表单,以获得用户的名字.你需要类似这样的模板: <form action="/your-name/" method=" ...
- MySQL 是怎样运行的:从根儿上理解 MySQL:字符集和比较规则
本文章借鉴自https://juejin.im/book/5bffcbc9f265da614b11b731 字符集和比较规则简介 一些重要的字符集 ASCII字符集 共收录128个字符,包括空格.标点 ...
- 【Leetcode】【简单】【26. 删除排序数组中的重复项】【JavaScript】
题目描述 26. 删除排序数组中的重复项 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 ...
- 六、用DataContractSerialize类序列化XML
一.层次结构 基类:XmlObjectSerializer 派生类: DataContractSerializer NetDataContractSerializer DataContractJson ...
- 一次vaccum导致的事故
1. 问题出现 晚上9点,现场报系统查询慢,运维查询zabbix后发现postgres最近几天的IOWait很大 2. 追踪问题 查询数据库,发现很多SQL堵住了 原因是真正创建index,导致表锁住 ...
- Python之Linux命令
1.查看当前文件路径 : pwd LangYingdeMacBook-Pro:Users langying$ pwd /Users 2.切换目录 cd 例如:切换到根目录 : cd / 回到 ...
- DOM设置css样式
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- jQuery理解与运用
1. 什么是jQuery 它是一个轻量级的javascript类库,别人写好的一个类. 2. jQuery优点 2.1 总是面向集合 2.2 多行操作集于一行 注1:就一个类“jQuery ...
- Codeforces Round #528 (Div. 2)题解
Codeforces Round #528 (Div. 2)题解 A. Right-Left Cipher 很明显这道题按题意逆序解码即可 Code: # include <bits/stdc+ ...
- 03_Elastic部署
ES集群部署 elastic不能用root用户去启动,否则会报错,所以创建elastic用户 1.创建elastic用户 $ useradd elastic -s /sbin/nologin 2..部 ...