Description

Given an circular integer array (the next element of the last element is the first element), find a continuous subarray in it, where the sum of numbers is the biggest. Your code should return the index of the first number and the index of the last number.

If duplicate answers exist, return any of them.

Example

Example 1:

Input: [3, 1, -100, -3, 4]
Output: [4, 1]

Example 2:

Input: [1,-1]
Output: [0, 0]

Challenge

O(n) time

思路:

分两种情况讨论:

  1. 最大数组仍然是中间的某一段
  2. 最大数组是去掉了中间的一段之后剩下的部分

第一种情况用传统的最大子数组做法走一遍(参考题解)。第二种做法稍微想一下就可以证明中间被去掉的那一段是整个数组的 minimum subarray。
所以求一遍 minimum subarray 之后,比较两种情况, 取最优解即可

需要特殊考虑 minimum subarray 是取了所有数的情况。

public class Solution {
/*
* @param A: An integer array
* @return: A list of integers includes the index of the first number and the index of the last number
*/ class Result{
public int maxSum;
public int leftIdx, rightIdx;
} // coef = 1: find the maximum non-empty subarray
// coef = -1: find the maximum non-empty subarray
// A[i] *= coef
Result findMax(int[] A, int coef) {
// Sj // S{i-1} // i-1
int j, nowSum = 0, prevMinSum = 0, prevMinIdx = -1;
Result res = new Result();
res.maxSum = Integer.MIN_VALUE;
for (j = 0; j < A.length; ++j) {
nowSum += A[j] * coef;
// Sj- prevMinSum
if (nowSum - prevMinSum > res.maxSum) {
res.maxSum = nowSum - prevMinSum;
res.leftIdx = prevMinIdx; // i - 1
res.rightIdx = j;
} if (nowSum < prevMinSum) {
prevMinSum = nowSum;
prevMinIdx = j;
}
} return res;
} public List<Integer> continuousSubarraySumII(int[] A) {
Result max = findMax(A, 1);
Result min = findMax(A, -1);
min.maxSum *= -1; int totSum = 0;
for (int i = 0; i < A.length; ++i) {
totSum += A[i];
} List<Integer> res = new ArrayList<>();
if (max.maxSum >= totSum - min.maxSum) {
res.add(max.leftIdx + 1);
res.add(max.rightIdx);
}
else {
// special case
if (min.leftIdx == -1 && min.rightIdx == A.length - 1) {
res.add(max.leftIdx + 1);
res.add(max.rightIdx);
}
else {
// use complementary interval for min interval
// [min.leftIdx+1...min.rightIdx]
// min.rightIdx + 1 ... len-1, 0, 1, ... min.leftIdx
res.add(min.rightIdx + 1);
res.add(min.leftIdx);
}
} return res;
}
}

  

Continuous Subarray Sum II的更多相关文章

  1. Continuous Subarray Sum II(LintCode)

    Continuous Subarray Sum II   Given an circular integer array (the next element of the last element i ...

  2. [LintCode] Continuous Subarray Sum II

    Given an integer array, find a continuous rotate subarray where the sum of numbers is the biggest. Y ...

  3. [LintCode] Subarray Sum & Subarray Sum II

    Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...

  4. LintCode 402: Continuous Subarray Sum

    LintCode 402: Continuous Subarray Sum 题目描述 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的下标 ...

  5. leetcode 560. Subarray Sum Equals K 、523. Continuous Subarray Sum、 325.Maximum Size Subarray Sum Equals k(lintcode 911)

    整体上3个题都是求subarray,都是同一个思想,通过累加,然后判断和目标k值之间的关系,然后查看之前子数组的累加和. map的存储:560题是存储的当前的累加和与个数 561题是存储的当前累加和的 ...

  6. [LintCode] Continuous Subarray Sum 连续子数组之和

    Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...

  7. Continuous Subarray Sum

    Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...

  8. [LeetCode] Continuous Subarray Sum 连续的子数组之和

    Given a list of non-negative numbers and a target integer k, write a function to check if the array ...

  9. [Swift]LeetCode523. 连续的子数组和 | Continuous Subarray Sum

    Given a list of non-negative numbers and a target integer k, write a function to check if the array ...

随机推荐

  1. 备份数据库中的某个表的数据报错Statement violates GTID consistency

    1.错误描述 执行CREATE TABLE tig_pairs_20190521 AS SELECT *FROM tig_pairs报错: 1 queries executed, 0 success, ...

  2. RPC和RestFul

    什么是REST REST是一种架构风格,指的是一组架构约束条件和原则.满足这些约束条件和原则的应用程序或设计就是 RESTful.REST规范把所有内容都视为资源,网络上一切皆资源. REST并没有创 ...

  3. golang方法和函数的区别

  4. 【LEETCODE】59、数组分类,适中级别,题目:39、48、64

    package y2019.Algorithm.array.medium; import java.util.*; /** * @ProjectName: cutter-point * @Packag ...

  5. windows10环境下的RabbitMQ安装_笔记

    原文:https://blog.csdn.net/weixin_39735923/article/details/79288578 第一步:下载并安装erlang原因:RabbitMQ服务端代码是使用 ...

  6. Ubuntu /etc/security/limits.conf 不生效问题

    一.问题描述 修改 /etc/security/limits.conf ,重启之后不生效 内容如下: * soft nofile * hard nofile root soft nofile root ...

  7. git 学习笔记 ---安装

    Git是什么? Git是目前世界上最先进的分布式版本控制系统(没有之一). 安装Git 在Linux上安装Git 首先,你可以试着输入git,看看系统有没有安装Git: $ git The progr ...

  8. helm chart应用使用示例

    有两种方法 一是按照正常流程直接用yaml文件的形式发布应用到k8s集群上 二是把生成好的yaml文件发布到kubeapps应用商店,在应用商店中操作发布应用到k8s集群中 这里采用第二种方式的变种方 ...

  9. Java日志logback使用

    pom中添加: <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</ ...

  10. springMvc之常用注解介绍

    @requestbody和@requestparam的用法  获取请求参数的方法 get请求: 直接获取request    如: public String getHtml(HttpServletR ...