Continuous Subarray Sum II

 

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

Give [3, 1, -100, -3, 4], return [4,1].

先是一个超时了的代码。。虽然剪了枝但还是会超时。

Time Limit Exceeded 91% test cases passed. Total Runtime: 12957 ms

 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
*/
public ArrayList<Integer> continuousSubarraySumII(int[] A) {
int l = A.length;
int s = 0;
int e = 0;
int ms = 0;
int me = 0;
int sum = A[0];
int max = A[0];
ArrayList<Integer> list = new ArrayList(); if(l == 1){
list.add(0);
list.add(0);
return list;
} for(int j = 0;j<l;j++) {
if(A[j] <= 0) continue;
sum = A[j];
s = j;
e = j;
for(int i = (j+1)%l;j!=i;i = (i+1)%l){
if(A[i] < 0) {
if(sum > max) {
max = sum;
ms = s;
me = e;
}
} if(sum < 0) {
sum = A[i];
s = i;
e = i;
}else {
sum += A[i];
e = i;
}
}
} if(max > sum) {
list.add(ms);
list.add(me);
}else {
list.add(s);
list.add(e);
}
return list;
}
}

然后很仔细的想了一下。设0<=a<=b<=A.length,最大和区间只有两种情况:[a,b]和[b,A.length-1]U[0,a];只要找出这两种情况下的最大区间和,比较一下大的那个就是所求区间。

第一种情况就是求不头尾相连数组A的最大和区间。第二种是求不头尾相连A的最小子区间[a+1,b-1](或者说是-A的最大区间和)。

敲出一段乱糟糟的代码终于AC了,容我去撸一把。

Accepted Total Runtime: 12746 ms 100% test cases passed.

 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
*/
public ArrayList<Integer> continuousSubarraySumII(int[] A) {
int[] nums = A;
int sum = nums[0];
int allsum = nums[0];
int max = nums[0];
int min = nums[0];
int msum = nums[0];
int mins = 0;
int mine = 0;
int s1 = 0;
int e1 = 0;
int s = 0;
int e = 0;
int ms = 0;
int me = 0;
ArrayList<Integer> list = new ArrayList<Integer>(); for(int i = 1 ;i < nums.length ;i++) {
allsum += nums[i];
if(nums[i] < 0) {
if(sum > max) {
max = sum;
ms = s;
me = e;
}
} if(nums[i] > 0) {
if(msum < min) {
min = msum;
mins = s1;
mine = e1;
}
} if(sum < 0) {
sum = nums[i];
s = i;
e = i;
}else {
sum+=nums[i];
e++;
} if(msum > 0) {
msum = nums[i];
s1 = i;
e1 = i;
}else {
msum+=nums[i];
e1++;
}
} if(sum > max) {
max = sum;
ms = s;
me = e;
}
if(msum < min) {
min = msum;
mins = s1;
mine = e1;
}
int val = allsum - min;
if(val > max && (mins != 0 && mine != A.length-1)) {
ms = mine + 1;
me = mins - 1;
} list.add(ms);
list.add(me);
return list;
}
}

还有一个想法是将A拷贝一份接上,然后相当于求这整个数组的最大和区间。想法是设置一个int flag允许遍历数组尾部两次;同时限制区间长度不能超过A.length。但是WA了。快折腾一下午了实在不想搞了先放着以后想起来再说把。

Wrong Answer Total Runtime: 8168 ms 78% test cases passed.

 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
*/
public ArrayList<Integer> continuousSubarraySumII(int[] A) {
int l = A.length;
int s = 0;
int e = 0;
int ms = 0;
int me = 0;
int sum = A[0];
int max = A[0];
int flag = 2;//允许遍历数组尾部2次
ArrayList<Integer> list = new ArrayList(); if(l == 1){
list.add(0);
list.add(0);
return list;
} int i = 1;
while(flag > 0) {
if(i == l - 1) flag--;
if(A[i] < 0) {
if(sum > max) {
max = sum;
ms = s;
me = e;
}
} if(sum < 0) {
sum = A[i];
s = i;
e = 0;
}else {
if(e + 1 > l - 1) break;
sum += A[i];
e++;
} i = (i + 1) % l;
} if(max > sum) {
list.add(ms);
list.add((me+ms)%l);
}else {
list.add(s);
list.add((e+s)%l);
}
return list;
}
}

Continuous Subarray Sum II(LintCode)的更多相关文章

  1. [LintCode] Continuous Subarray Sum II

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

  2. LeetCode:40. Combination Sum II(Medium)

    1. 原题链接 https://leetcode.com/problems/combination-sum-ii/description/ 2. 题目要求 给定一个整型数组candidates[ ]和 ...

  3. Continuous Subarray Sum II

    Description Given an circular integer array (the next element of the last element is the first eleme ...

  4. [LeetCode] Combination Sum II (递归)

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  5. LeetCode Path Sum II (DFS)

    题意: 给一棵二叉树,每个叶子到根的路径之和为sum的,将所有可能的路径装进vector返回. 思路: 节点的值可能为负的.这样子就必须到了叶节点才能判断,而不能中途进行剪枝. /** * Defin ...

  6. LeetCode Combination Sum II (DFS)

    题意: 在集合candidates中选出任意多个元素,使得他们的和为target,返回所有的组合,以升序排列. 思路: 难点在于如何去重,比如集合{1,1,2},target=3,那么只有一个组合就是 ...

  7. 【树】Path Sum II(递归)

    题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...

  8. Single Number II(LintCode)

    Single Number II Given 3*n + 1 numbers, every numbers occurs triple times except one, find it. Examp ...

  9. LintCode 402: Continuous Subarray Sum

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

随机推荐

  1. [洛谷P4774] [NOI2018]屠龙勇士

    洛谷题目链接:[NOI2018]屠龙勇士 因为markdown复制过来有点炸格式,所以看题目请戳上面. 题解: 因为杀死一条龙的条件是在攻击\(x\)次,龙恢复\(y\)次血量\((y\in N^{* ...

  2. 计数排序Counting sort

    注意与基数排序区分,这是两个不同的排序 计数排序的过程类似小学选班干部的过程,如某某人10票,作者9票,那某某人是班长,作者是副班长 大体分两部分,第一部分是拉选票和投票,第二部分是根据你的票数入桶 ...

  3. 自己实现KNN算法

    import numpy as np from math import sqrt from collections import Counter class KNNClassifier(object) ...

  4. 【BZOJ4517】【SDOI2016】排列计数 [数论]

    排列计数 Time Limit: 60 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description 求有多少种长度为 n 的序列 A, ...

  5. 【BZOJ】1697: [Usaco2007 Feb]Cow Sorting牛排序

    [算法]数学置换 [题意]给定n个数,要求通过若干次交换两个数的操作得到排序后的状态,每次交换代价为两数之和,求最小代价. [题解] 考虑置换的定义:置换就是把n个数做一个全排列. 从原数组到排序数组 ...

  6. 【BZOJ】1609: [Usaco2008 Feb]Eating Together麻烦的聚餐

    [算法]动态规划 [题解]DP有个特点(递推的特点),就是记录所有可能状态然后按顺序转移. 最优化问题中DP往往占据重要地位. f[i][j]表示前i头奶牛,第i头改为号码j的最小改动数字,这样每头奶 ...

  7. Spring Session加Redis(山东数漫江湖)

    session是一个非常常见的概念.session的作用是为了辅助http协议,因为http是本身是一个无状态协议.为了记录用户的状态,session机制就应运而生了.同时session也是一个非常老 ...

  8. 撩下Cookie和Session

    Cookie Cookie其实还可以用在一些方便用户的场景下,设想你某次登陆过一个网站,下次登录的时候不想再次输入账号了,怎么办?这个信息可以写到Cookie里面,访问网站的时候,网站页面的脚本可以读 ...

  9. PAT L1-009 N个数求和(运用GCD进行通分)

    题目链接:https://www.patest.cn/contests/gplt/L1-009 题目: 本题的要求很简单,就是求N个数字的和.麻烦的是,这些数字是以有理数“分子/分母”的形式给出的,你 ...

  10. MSSQL 数据库性能优化

    优化数据库的注意事项: 1.关键字段建立索引. 2.使用存储过程,它使SQL变得更加灵活和高效. 3.备份数据库和清除垃圾数据. 4.SQL语句语法的优化. 5.清理删除日志. SQL语句优化的基本原 ...