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. 嵌入式02 STM32 实验01 端口复用和重映射

    内设与外设: 端口复用和端口重映射都需要了解内设和外设,那么什么是内设?什么是外设? 内设:单片机内部集成的功能一般包括:串口模块.SPI模块(Serial Peripheral Interface  ...

  2. Java基础系列3:多线程超详细总结

    该系列博文会告诉你如何从入门到进阶,一步步地学习Java基础知识,并上手进行实战,接着了解每个Java知识点背后的实现原理,更完整地了解整个Java技术体系,形成自己的知识框架. 1.线程概述 几乎所 ...

  3. Rediskey生命周期管理-key聚合

    | 导语 Redis中的数据以k-v的方式组织:为了方便管理key一般具有某些特定的模式:有些key直接是由mysql中的表行数据转化而来:如果要对Redis的key进行生命周期管理,由于Redis的 ...

  4. (转)数据库函数解析JSON字符串

    一.返回单行单列 二.返回表 三.SQL206版本开始支持 SELECT * FROM OPENJSON(@JsonStr)

  5. JavaScript之条件语句

    (1)if条件语句 // if(条件){当条件为真,存在即为真.当条件为false null 0 undefined中任意一种时,则表示不存在,不存在即为假} if(条件){ 条件为真时执行的代码 } ...

  6. 强大的display:grid

    自从用习惯flex布局我基本已经不怎么使用float了. 现在又出现了grid布局,就像flex的升级版,布局上十分强大. 主要属性: grid-template-columns://竖向排列 gri ...

  7. jenkins报错 Upgrading Jenkins. Failed to update the default Update Site 'default'. Plugi

    解决方案: jenkins\hudson.model.UpdateCenter.xml 文件, 将 url 中的 https://updates.jenkins.io/update-center.js ...

  8. MySQL Replication--修复从库上单个数据库的数据

    问题描述 由于运维失误,从库未及时设置read_only,导致从库上某库数据被修改,由于整个实例数据量较大,重做成本较高,而该数据库数据较少,因此考虑如何修复该数据库的数据. 操作前提 1.复制使用位 ...

  9. 编写订单支付api中遇到的问题

    首先我是按照已经有的已经有的支付api去编写订单支付api,但是由于两者是有区别的,所以类似去搬用难免会出问题,首先我是套用已经写好的model,然后写相应的serializer,实现序列化之后就开始 ...

  10. 【转】基于TMS320C6455的千兆以太网设计

    基于TI公司最新DSP芯片TMS320C6455.设计并实现了以太网通信软硬件接口.采用TMS320C6455片内以太网接口模块EMAC/MDIO,结合片外AR8031 PHY芯片,在嵌入式操作系统D ...