Given an integer array, find a continuous rotate subarray 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 their are duplicate answer, return anyone. The answer can be rorate array or non- rorate array)

 Example

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

分析:

此题是Continuous Subarray Sum的升级版本。对于给定序列A, Continuous Subarray Sum中,子序列为A{i -> j}, 要求 0 <= i <= j < size(A),此为经典动态规划问题。在这里,子序列除了有之前的形式外,还允许rotate subarray,即子序列允许从尾部延续到头部,形式为A{0 -> i, j -> size(A) - 1}。

解法一:

因为允许尾部到头部形成子序列。通常的想法是把数据copy一份连到尾部。此时我们可以用Continous Subarray Sum的解法来做。但是有一点区别,我们的子序列长度不能超过size(A)。因此,我们依然可以用动态规划来做,不过需要考虑到序列的长度。为了实现简便,下面给出一个O(N^2) 非动态规划的解法,利用了累计数列和条件剪枝。不过最后三个数据还是会超时。

vector<int> continuousSubarraySumII(vector<int>& A) {
// Write your code here
vector<int> result(, );
int n = A.size();
if(n < ) return result;

     //duplicate array
vector<int> B(A);
B.insert(B.end(), A.begin(), A.end());

//cumsum can help to calculate the sum from B(i) to B(j) with O(1) time
vector<int> cumsum;
cumsum.push_back(B[]);
for(int i = ;i < B.size();++i)
cumsum.push_back(cumsum[i - ] + B[i]); int maxVal = B[], left = , right = ;
for(int s = ;s < n;++s){
//there is no need to start from an negative number, this pruning is useful
if(B[s] <= ) continue;
for(int e = s; e < s + n;++e){
int cur = ;
if(s == ) cur = cumsum[e];
else cur = cumsum[e] - cumsum[s - ]; if(cur > maxVal){
maxVal = cur;
left = s;
right = e;
}
}
}
result[] = left%n;
result[] = right%n;
return result;
}

解法二:

进一步分析发现,第二种subarray其实和第一种是相关的。我们可以通过剪掉最小连续子序列得到第二种subarray。这里需要注意当所有数字为负的情况。

vector<int> continuousSubarraySumII(vector<int>& A) {
// Write your code here
vector<int> result(, );
int n = A.size();
if(n < ) return result; vector<int> posMax(n, ), posMaxIdx(n, ), posMin(n, ), posMinIdx(n, );
posMax[] = A[];
posMin[] = A[];
posMaxIdx[] = ;
posMinIdx[] = ;
int sum = A[], maxVal = A[], minVal = A[],
maxL = , maxR = , minL = , minR = ; for(int i = ;i < n;++i){
sum += A[i];
//max subArray
if(posMax[i - ] > ){
posMax[i] = posMax[i - ] + A[i];
posMaxIdx[i] = posMaxIdx[i - ];
}else{
posMax[i] = A[i];
posMaxIdx[i] = i;
}
//min subArray
if(posMin[i - ] < ){
posMin[i] = posMin[i - ] + A[i];
posMinIdx[i] = posMinIdx[i - ];
}else{
posMin[i] = A[i];
posMinIdx[i] = i;
} if(posMax[i] > maxVal){
maxVal = posMax[i];
maxL = posMaxIdx[i];
maxR = i;
}
if(posMin[i] < minVal){
minVal = posMin[i];
minL = posMinIdx[i];
minR = i;
}
} int val = sum - minVal;
if(val <= maxVal || (minL == && minR == n - )){
result[] = maxL;
result[] = maxR;
}else{
result[] = minR + ;
result[] = minL - ;
} return result;
}

[LintCode] 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 连续子数组之和

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

  3. Continuous Subarray Sum II

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

  4. LintCode "Continuous Subarray Sum"

    A variation to a classical DP: LCS. class Solution { public: /** * @param A an integer array * @retu ...

  5. LintCode 402: Continuous Subarray Sum

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

  6. [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 ...

  7. 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题是存储的当前累加和的 ...

  8. Continuous Subarray Sum

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

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

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

随机推荐

  1. sql批量获取wordpress所有留言者的邮件地址

    如果你的wordpress博客有很多读者互动的话,他们的留言都会留下具体的联系邮箱,我们如何批量导出这些联系信息呢?可以试试下面的sql语句 SELECT DISTINCT comment_autho ...

  2. NGUI 学习笔记实战——制作商城UI界面

    http://www.cnblogs.com/chongxin/p/3876575.html Unity3D的uGUI听说最近4.6即将推出,但是目前NGUI等UI插件大行其道并且已经非常成熟,所以我 ...

  3. c3p0数据库连接池

    C3P0: 一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展.目前使用它的开源项目有Hibernate,Spring等. 默认情况下(即没有配置连接池的 ...

  4. HTML 笔记,持续更新

    一.文本格式化标签 <b> 定义粗体文本. <big> 定义大号字. <em> 定义着重文字. <i> 定义斜体字. <small> 定义小 ...

  5. bellman ford优先队列优化简介模板

    #include<iostream>#include<cstdio>#include<utility>#include<queue>#include&l ...

  6. 能用Shell就别编程-海量文本型数据的处理

    对于txt文本类数据,优先采用shell脚本,实在不行才用Python,Java,MySQL 1) Shell命令行或脚本的处理速度极快,比Java快得多. 2) Shell代码量少,几个命令就能完成 ...

  7. sqlserver 中的NOLOCK、HOLDLOCK、UPDLOCK、TABLOCK、TABLOCKX

    1.NOLOCK(不加锁) 此选项被选中时,SQL Server 在读取或修改数据时不加任何锁. 在这种情况下,用户有可能读取到未完成事务(Uncommited Transaction)或回滚(Rol ...

  8. codeforces 479C Exams 解题报告

    题目链接:http://codeforces.com/problemset/problem/479/C 题目意思:简单来说,就是有个人需要通过 n 门考试,每场考试他可以选择ai, bi 这其中一个时 ...

  9. Intellj IDEA快捷键

    Alt+回车 导入包,自动修正 Ctrl+N   查找类 Ctrl+Shift+N 查找文件 Ctrl+Alt+L  格式化代码 Ctrl+Alt+O 优化导入的类和包 Alt+Insert 生成代码 ...

  10. myeclipse2013和以后版本破解

    当你下到最新版的myeclipse-blue的时候你是否会为注册激活而烦恼呢,别担心,其实激活也就那么点事儿,请遵循我如下做法就可以了: 1.运行jdk下面的cracker.jar文件来打开生成活跃码 ...