*[codility]MaxDoubleSliceSum
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的更多相关文章
- Codility NumberSolitaire Solution
1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...
- codility flags solution
How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...
- GenomicRangeQuery /codility/ preFix sums
首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...
- *[codility]Peaks
https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...
- *[codility]Country network
https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...
- *[codility]AscendingPaths
https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...
- *[codility]Fish
https://codility.com/demo/take-sample-test/fish 一开始习惯性使用单调栈,后来发现一个普通栈就可以了. #include <stack> us ...
- *[codility]CartesianSequence
https://codility.com/programmers/challenges/upsilon2012 求笛卡尔树的高度,可以用单调栈来做. 维持一个单调递减的栈,每次进栈的时候记录下它之后有 ...
- [codility]CountDiv
https://codility.com/demo/take-sample-test/count_div 此题比较简单,是在O(1)时间里求区间[A,B]里面能被K整除的数字,那么就计算一下就能得到. ...
随机推荐
- 关于ionic的跨域问题
例如你的api原地址请求是 http://10.100.100.100:8080/service/, 1.那么你应该在项目内api请求改成 /service/, 注意红色部分是ionic serve ...
- .NET SDK和下载
http://blogs.msdn.com/b/dotnet/p/dotnet_sdks.aspx .NET SDK和下载 您可以通过下载.NET框架针对包和软件开发工具包,并使用它们与Visual ...
- 关于python的环境变量问题
我的ubuntu安装python后,查看所有的环境变量,发现没有PYTHONPATH?对我使用python没太大影响,自己写的模块的路径问题有很多方法解决.但是现在我想将我写的模块放在一个包里,要用到 ...
- 1106. Lowest Price in Supply Chain (25)
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...
- Python 学习教程
<Core Python Programming>勘误参考表 http://starship.python.net/crew/wesc/cpp/errata2.htm 笨办法学 Pytho ...
- (菜鸟要飞系列)五,基于Asp.Net MVC5的后台管理系统(添加数据表的分页功能)
献上代码 ) { List<UserModel> arrayUserModel = new List<UserModel>(); string strText = Reques ...
- LCA专题
标签(空格分隔): LCA 我的个人网站挂了,最近就先用这个来写博客吧.以后争取在这个网站写一些与OI无关的个人爱好的东西. 题目来源:code[VS] 倍增--在线算法 用 $f[i][j]$ 记录 ...
- 微软职位内部推荐-Sr SDE for Win Apps Ecosystem
微软近期Open的职位: Job posting title: Senior Software Design Engineer Location: China, Beijing Level: 63 D ...
- Elasticsearch 5.0
Elasticsearch 5.0 使用ES的基本都会使用过head,但是版本升级到5.0后,head插件就不好使了.下面就看看如何在5.0中启动Head插件吧! 官方粗略教程 Running wit ...
- js之正则表达式(下)
1.分组之exec返回数组 1>非分组匹配的exec返回数组: var pattern =/\d+[a-z]+/; var str='234google'; alert(pattern.exec ...