https://codility.com/demo/take-sample-test/max_double_slice_sum

两个最大子段和相拼接,从前和从后都扫一遍。注意其中一段可以为0。还有最后和最前面一个不可能取到~

#include <vector>

using namespace std;

int solution(vector<int> &A) {
vector<int> maxEnd;
maxEnd.resize(A.size());
maxEnd[0] = 0;
for (int i = 1; i < A.size(); i++) {
maxEnd[i] = max(0, maxEnd[i - 1] + A[i]);
}
vector<int> maxBegin;
maxBegin.resize(A.size());
maxBegin[A.size() - 1] = 0;
for (int i = A.size() - 2; i >= 0; i--) {
maxBegin[i] = max(0, maxBegin[i + 1] + A[i]);
}
int result = maxEnd[0] + maxBegin[2];
for (int i = 0; i + 2 < A.size(); i++) {
int current = maxEnd[i] + maxBegin[i + 2];
result = max(result, current);
}
return result;
}

  

*[codility]MaxDoubleSliceSum的更多相关文章

  1. Codility NumberSolitaire Solution

    1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...

  2. codility flags solution

    How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...

  3. GenomicRangeQuery /codility/ preFix sums

    首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...

  4. *[codility]Peaks

    https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...

  5. *[codility]Country network

    https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...

  6. *[codility]AscendingPaths

    https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...

  7. *[codility]Fish

    https://codility.com/demo/take-sample-test/fish 一开始习惯性使用单调栈,后来发现一个普通栈就可以了. #include <stack> us ...

  8. *[codility]CartesianSequence

    https://codility.com/programmers/challenges/upsilon2012 求笛卡尔树的高度,可以用单调栈来做. 维持一个单调递减的栈,每次进栈的时候记录下它之后有 ...

  9. [codility]CountDiv

    https://codility.com/demo/take-sample-test/count_div 此题比较简单,是在O(1)时间里求区间[A,B]里面能被K整除的数字,那么就计算一下就能得到. ...

随机推荐

  1. 如何在java类中读取Properties配置文件

    在com.example包下有一个test.properties文件和测试类PropertyReadTest.java. test.properties 文件内容: author=zeige  tea ...

  2. AngularJS(12)-BootStrap集成

    AngularJS 的首选样式表是 Bootstrap, Bootstrap 是目前最受欢迎的前端框架. <!DOCTYPE html> <html lang="en&qu ...

  3. Mongodb地理空间索引

    1.索引: 建立索引既耗时也费力,还需要消耗很多资源.使用{"bakckground":true}选项可以使这个过程在后台完成,同时正常处理请求.如果不包括background 这 ...

  4. SequoiaDB 与 Hive 集成

    SequoiaDB与Hadoop部署 SequoiaDB与Hadoop在物理上部署方案如下图所示,部署建议如下: l  SequoiaDB与Hadoop部署在相同的物理设备上,以减少Hadoop与Se ...

  5. Response.Redirect和Server.Transfer

    今天又比较闲,逛了逛园子,看看asp.net的内容,看到一篇关于这两个的比较: http://www.cnblogs.com/yunfeng8967/archive/2008/03/06/109323 ...

  6. Python学习笔记1——人人都爱列表

    一些BIF函数在列表中的应用: Python 3.3.4 (v3.3.4:7ff62415e426, Feb 10 2014, 18:13:51) [MSC v.1600 64 bit (AMD64) ...

  7. postgreSQL数据库(索引、视图)

    索引的含义与特点 索引是一个单独的.存储在磁盘上的数据库结构,它们包含对数据所有记录的引用指针,postgresql列类型都可以被索引,对相关列索引是提高查询操作效率的最佳途径.例如,查询select ...

  8. TCP相关知识

    1. TCP与TCP/IP协议族 TCP是TCP/IP协议族中运输层的一个协议.TCP/IP,即传输控制协议/网间协议,是一个工业标准的协议集,包含了运输层.网络层和链路层的协议,其结构如下图所示:其 ...

  9. Aspose 导出excel小demo

    //转为pdf         private void CelltoPDF(string cellPath, string pdfPath)         {             Workbo ...

  10. Oracle 存储过程实例

    create or replace procedure PCREPORT is startDate DATE; --起始如期 nowTime DATE; --当前日期 nowTime2 DATE; - ...