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

Idea 1. Similar to Maximum Subarray LT53, the difference is the circular array part, the subarray could be the tail(A[j..n-1]) plus the head(A[0..i]) (i+1 < j), if the subarray of tail + head is the maximum, the subarray in the middle is A[i+1, j-1] has the minSum, once the minSum is found, the maxSum is Sum - minSum, except maxSum = 0 if all values in the array are negative, as minSum == Sum.

Note. negative arrays

Time complexity: O(n)

Space complexity: O(1)

 class Solution {
public int maxSubarraySumCircular(int[] A) {
int maxSoFar = 0;
int minSoFar = 0; int sum = 0;
int minSum = Integer.MAX_VALUE;
int maxSum = Integer.MIN_VALUE; for(int a: A) {
maxSoFar = a + Math.max(maxSoFar, 0);
maxSum = Math.max(maxSum, maxSoFar); minSoFar = a + Math.min(minSoFar, 0);
minSum = Math.min(minSum, minSoFar); sum += a;
} if(sum == minSum) {
return maxSum;
}
return Math.max(maxSum, sum - minSum);
}
}

Idea 2. 另外一种方法求2个intervals (head + tail)组成的,如果直接从左到右+从右到左的和,可能中间有重合,固定一边,求另外一边的和的最大值,保证j >= i+2.

Time complexity: O(n)

Space complexity: O(n)

 class Solution {
public int maxSubarraySumCircular(int[] A) {
int curr = 0;
int n = A.length;
int maxSum = Integer.MIN_VALUE; for(int a: A) {
curr = a + Math.max(curr, 0);
maxSum = Math.max(maxSum, curr);
} int[] rightMaxSum = new int[n];
rightMaxSum[n-1] = A[n-1];
curr = A[n-1];
for(int i = n-2; i >= 0; --i) {
curr += A[i];
rightMaxSum[i] = Math.max(curr, rightMaxSum[i+1]);
} curr = 0;
int result = maxSum;
for(int i = 0; i+2 < n; ++i) {
curr += A[i];
result = Math.max(result, curr + rightMaxSum[i+2]);
} return result;
}
}

Idea 3. Similar to sliding window minValue, build a min deque for prefix sum, for each prefix sum, find the minimum of previous prefixSum so that the subarray sum ending here is the maximu, since it's circular array, j - i <= n, remove the invalid previous index. Note: store the index

Time complexity: O(n)

Space complexity: O(n)

 class Solution {
public int maxSubarraySumCircular(int[] A) {
Deque<Integer> minPrefix = new LinkedList<>();
minPrefix.addLast(0); int n = A.length; int[] prefix = new int[2*n + 1];
for(int i = 0; i < 2*n; ++i) {
prefix[i+1] = prefix[i] + A[i%n];
}
int result = Integer.MIN_VALUE;
for(int i = 1; i <= 2*n; ++i) { if(i - minPrefix.getFirst() > n) {
minPrefix.removeFirst();
} result = Math.max(result, prefix[i] - prefix[minPrefix.getFirst()]); while(!minPrefix.isEmpty() && prefix[minPrefix.getLast()] >prefix[i]) {
minPrefix.removeLast();
}
minPrefix.add(i); } return result;
}
}

Note:

  1. -30000 <= A[i] <= 30000
  2. 1 <= A.length <= 30000

Maximum Sum Circular Subarray LT918的更多相关文章

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

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

  3. 918. Maximum Sum Circular Subarray

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

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

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

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

  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. 消息队列(Message Queue)简介及其使用

    消息队列(Message Queue)简介及其使用 摘要:利用 MSMQ(Microsoft Message Queue),应用程序开发人员可以通过发送和接收消息方便地与应用程序进行快速可靠的通信.消 ...

  2. Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class

    Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdb ...

  3. 深入理解Java虚拟机读书笔记2----垃圾收集器与内存分配策略

    二 垃圾收集器与内存分配策略 1 JVM中哪些内存需要回收?     JVM垃圾回收主要关注的是Java堆和方法区这两个区域:而程序计数器.虚拟机栈.本地方法栈这3个区域随线程而生,随线程而灭,随着方 ...

  4. c语言小项目---通讯录2.0

    自从上次通讯录项目被字符串项目整的自闭了之后,用了5天时间重新整理了一下通讯录的思路,并且能够正常的使用,今天按模块把基于链表的通讯录2.0版本记录一下,供后续积累经验. 首先总结一下 通讯录2.0版 ...

  5. 尚硅谷springboot学习34-整合SpringData JPA

    SpringData简介

  6. 创建第一个vue实例

    一.vue安装与下载 1. 官网下载  下载地址 选择开发版本 2. 打开sublime,新建vue文件夹,将下载好的代码vue.js放入vue文件夹中. 3. 新建index.html文件,在hea ...

  7. 初识Scratch 3.0

    之前在帮朋友搜集少儿编程教育资料的时候,发现了麻省理工开发的积木式编程语言的Scratch,最近有空玩了下,感觉很惊艳,我能想象用它做一些有趣的事情,Scratch把编程元素变成像乐高积木一样,可以通 ...

  8. SpringBoot之基础

    简介 背景 J2EE笨重的开发 / 繁多的配置 / 低下的开发效率 / 复杂的部署流程 / 第三方技术集成难度大 特点 ① 快速创建独立运行的spring项目以及主流框架集成 ② 使用嵌入式的Serv ...

  9. Virtualbox下克隆CentOS 网络配置

    Virtualbox下克隆虚拟机非常容易,也使得我们在平常搭建测试环境方便了许多.不过克隆以后的虚机并不能够直接联网,这是由于网卡的MAC地址引起的.在"控制->复制"弹出的 ...

  10. html5下F11全屏化的几点注意

    1.实现全屏化 var docElm = document.documentElement; //W3C if (docElm.requestFullscreen) { docElm.requestF ...