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. 常见的几种异常类型 Exception

    常见异常类型:Java中的异常分为两大类: 1.Checked Exception(非Runtime Exception) 2.Unchecked Exception(Runtime Exceptio ...

  2. Python之让 字符串内的转义字符 不做任何处理

    一.在字符串前面加上 'r' 就可以了 print("\ntext_1") print(r"\ntest_2") 二.在转义字符的 '\' 前面再加一个 '\' ...

  3. Qt 5.12 LTS 部署

    1. 拷贝release生成的exe到一个独立的目录deploy 2. windeployqt.exe A_Toolkit.exe 3. 将qt\qt5.12.5\tool\mingw730_64\b ...

  4. Numpy学习笔记(上篇)

    目录 Numpy学习笔记(上篇) 一.Jupyter Notebook的基本使用 二.Jpuyter Notebook的魔法命令 1.%run 2.%timeit & %%timeit 3.% ...

  5. Android--ScrollView边界回弹效果

    /* * Copyright (C) 2006 The Android Open Source Project * * Licensed under the Apache License, Versi ...

  6. git push proxy 取消不掉 can not prox....

    使用这个折腾了半天 git config --global --unset http.proxy git config --global --unset https.proxy 没用,原来实现项目目录 ...

  7. 1+X证书学习日志——css 3D效果+立方体效果的实现

    形成一个3D的空间 transform-style: preserve-3d; ### 3D在2D的基础上,多了这些内容 位移 transform:translateZ(); 旋转 transform ...

  8. 安卓MediaPlayer框架之Binder机制

    Binder简介 Binder是Android系统进程间通信的主要方式之一. 1.在ASOP中,Binder使用传统的C/S通信方式:即一个进程作为服务端提供诸如视音频解封装,解码渲染,地址查询等各种 ...

  9. Jmeter学习笔记(七)——监听器元件之察看结果树

    在jmeter中,如果我们需要查看请求结果就需要添加查看结果树,这个监听器元件有那些功能呢? 一.察看结果树界面如下 二.察看结果树界面功能说明 1.所有数据写入文件 (1)文件名:可以通过浏览,选择 ...

  10. sqlserver TOP 问题(转载)

          来谈谈SQL数据库中"简单的"SELECT TOP—可能有你从未注意到的细节 首先从博客园的Jerome Wong网友说起 他提出了一个这样的问题 本人写了好几年SQL ...