Continuous Subarray Sum II
Description
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
思路:
分两种情况讨论:
- 最大数组仍然是中间的某一段
- 最大数组是去掉了中间的一段之后剩下的部分
第一种情况用传统的最大子数组做法走一遍(参考题解)。第二种做法稍微想一下就可以证明中间被去掉的那一段是整个数组的 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的更多相关文章
- Continuous Subarray Sum II(LintCode)
Continuous Subarray Sum II Given an circular integer array (the next element of the last element i ...
- [LintCode] Continuous Subarray Sum II
Given an integer array, find a continuous rotate subarray where the sum of numbers is the biggest. Y ...
- [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 ...
- LintCode 402: Continuous Subarray Sum
LintCode 402: Continuous Subarray Sum 题目描述 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的下标 ...
- 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题是存储的当前累加和的 ...
- [LintCode] Continuous Subarray Sum 连续子数组之和
Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...
- Continuous Subarray Sum
Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...
- [LeetCode] Continuous Subarray Sum 连续的子数组之和
Given a list of non-negative numbers and a target integer k, write a function to check if the array ...
- [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 ...
随机推荐
- Windows平台为 ElasticSearch 6.x 安装 Head 客户端插件
背景知识 ES 5.x 以上版本安装 head 需要 node 和 grunt 环境(之前的直接用plugin命令即可安装). 操作步骤 1. 安装 ElasticSearch 6.x,访问 http ...
- 数组,const,#define
#include<stdio.h> #define COUNT 6 int main(){// const类型的常量不能作为数组的个数,大部分编译器不支持// const int C ...
- 【scratch3.0教程】1.3 了解scratch界面内容
第3课 了解Scratch界面内容 1.认识Scratch界面 双击桌面快捷方式,启动Scratch程序.我们来认识一下Scratch界面吧! (1)指令区 Scratch程序一共有运动.外观.声音. ...
- AS3.0频谱-01
AS3.0频谱系列-01: package fengzi.spectrum { //import fengzi.colors.GetColor; import flash.display.Sprite ...
- VirtualBox导入OVA文件文档教程
1 2 修改框住的路径,最好不要在C盘 3 取消检查更新 4 5 6 7 8 9 10 11 等待加载完成:加载完成后 OVA文件导入成功 作者:含笑半步颠√ 博客链接:https://www.cnb ...
- git学习笔记 ---删除文件
在Git中,删除也是一个修改操作,我们实战一下,先添加一个新文件test.txt到Git并且提交: $ git add test.txt $ git commit -m "add test. ...
- go语言学习 ---struct 对象的序列化和反序列化(xml)
实例1: //main package main import ( "encoding/xml" "fmt" ) type person struct { Na ...
- Dubbo学习摘录(一)
Dubbo基于自定义配置,实现自己的Bean. 通常,完成一个自定义配置需要以下步骤: 设计配置属性和JavaBean: 编写XSD文件: 编写NamespaceHandler和BeanDefinit ...
- Oracle数据库中导出某张表到SQL并关联更新
首先想到查询出结果,然后导出为SQL文件: 先导出表结构 1 在桌面建立对应的sql空文件 2 toos-->export userObjects 3 在对话框中选择你要导出的表 4 勾选上si ...
- ADO.NET 二(Connection)
C# 语言中 Connection 类是 ADO.NET 组件连接数据库时第一个要使用的类,也是通过编程访问数据库的第一步. 接下来了解一下 Connection 类中的常用属性和方法,以及如何连接 ...