A variation to a classical DP: LCS.

class Solution {
public:
/**
* @param A an integer array
* @return A list of integers includes the index of
* the first number and the index of the last number
*/
vector<int> continuousSubarraySum(vector<int>& A) {
vector<int> ret; size_t len = A.size(); int curr = A[];
int sum = A[], csum = A[];
int s = , e = , ss = , ee = ;
for (int i = ; i < len; i++)
{
int nsum = csum + A[i];
if (A[i] > nsum)
{
ss = ee = i;
csum = A[i];
}
else
{
ee = i;
csum = nsum;
} if(csum > sum) // update global record
{
sum = nsum;
s = ss;
e = ee;
}
} ret.push_back(s);
ret.push_back(e);
return ret;
}
};

LintCode "Continuous Subarray Sum"的更多相关文章

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

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

  3. LintCode 402: Continuous Subarray Sum

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

  4. Continuous Subarray Sum II(LintCode)

    Continuous Subarray Sum II   Given an circular integer array (the next element of the last element i ...

  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. Continuous Subarray Sum

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

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

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

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

  9. Continuous Subarray Sum LT523

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

随机推荐

  1. 深入理解JVM内部结构(转)

    图中显示的组件将会在下面两部分中进行逐一的解释.第一部分涉及JVM为每一个线程都会创建的组件:第二部分则是独立于线程进行创建的组件.   1. Thread       Thread是一个程序中的一个 ...

  2. Flume NG 简介及配置实战

    Flume 作为 cloudera 开发的实时日志收集系统,受到了业界的认可与广泛应用.Flume 初始的发行版本目前被统称为 Flume OG(original generation),属于 clo ...

  3. Trinity 安装

    http://trinityrnaseq.github.io/ 安装包下载地址: https://github.com/trinityrnaseq/trinityrnaseq/releases 解压 ...

  4. 如何用cufflinks 拼出一个理想的注释文件

    后记: cufflinks安装: 下载安装包, 不要下载source code ,直接下载binary.    Source code    Linux x86_64 binary http://cu ...

  5. SFTP 上传文件夹

    使用sftp上传文件夹时若使用如下命令并不work: put /media/Research/GWAS_Class/* Desktop/ 此时,需要添加一个参数 -r, 另外在目标文件夹下面建立一个同 ...

  6. codeforce Group Photo 2 (online mirror version)

    题目大意: 有n个矩形在地上排成一列,不可重叠,已知他们的宽度w和高度h,现在使至多[n / 2]个矩形旋转90度,问最后可以用多小的矩形恰好覆盖这n个矩形,求满足条件的最小矩形面积. n, w, h ...

  7. FHS目录配置下,常见的几个问题及解答

    请说明/bin与/usr/bin目录所放置的执行文件有何不同之处? /bin主要放置在开机时,以及进入单人维护模式后还能够被使用的指令,至于/usr/bin则是大部分软件提供的指令放置处. 请说明/b ...

  8. 移动互联网实战--Apple的APNS桩推送服务的实现(1)

    前记: 相信大家在搞IOS推送服务的开发时, 会直接使用javapns api来简单实现, 调试也直连Apple的APNS服务(产品/测试版)来实现. 很少有人会写个APNS的桩服务, 事实也是如此. ...

  9. IDOC、ALE、EDI三者之间的区别于联系

    EDI (Electronic data interchange) - 是指按照同一规定的一套通用标准格式,将标准的经济信息,通过通信网络传输,在贸易伙伴的电子计算机系统之间进行数据交换和自动处理.由 ...

  10. 15. 3Sum

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...