Codility---MaxSliceSum
A non-empty zero-indexed array A consisting of N integers is given. A pair of integers (P, Q), such that 0 ≤ P ≤ Q < N, is called a slice of array A. Thesum of a slice (P, Q) is the total of A[P] + A[P+1] + ... + A[Q].
Write a function:
class Solution { public int solution(int[] A); }
that, given an array A consisting of N integers, returns the maximum sum of any slice of A.
For example, given array A such that:
A[0] = 3 A[1] = 2 A[2] = -6 A[3] = 4 A[4] = 0
the function should return 5 because:
- (3, 4) is a slice of A that has sum 4,
- (2, 2) is a slice of A that has sum −6,
- (0, 1) is a slice of A that has sum 5,
- no other slice of A has sum greater than (0, 1).
Assume that:
- N is an integer within the range [1..1,000,000];
- each element of array A is an integer within the range [−1,000,000..1,000,000];
- the result will be an integer within the range [−2,147,483,648..2,147,483,647].
Complexity:
- expected worst-case time complexity is O(N);
- expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).
Elements of input arrays can be modified
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.Arrays;
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
int max_ending = A[0];
int max_slice = A[0];
for(int i=1; i<A.length; i++){
max_ending = Math.max(max_ending + A[i], A[i]);
max_slice = Math.max(max_slice, max_ending);
}
return max_slice;
}
}
https://codility.com/demo/results/trainingDWA6ZF-77M/
Codility---MaxSliceSum的更多相关文章
- 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]MaxDoubleSliceSum
https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后 ...
- *[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整除的数字,那么就计算一下就能得到. ...
随机推荐
- 分位数和分位线(Quantiles and Percentiles)
分位数有种积分(累积)的含义在. 分位数(即将数据由低至高排列,小于该数的数据占总体的比例达到时最终落到的数): 10%:3000元 20%:5200元 50%:20000元 80%:41500元 9 ...
- Cordova 返回键切换后台
这里需要用到 cordova-plugin-backbutton 这个插件 1.安装插件,命令窗口输入(当前目录是你项目所在的目录) cordova plugin add cordova-plugin ...
- 图片及js的预加载
loadImage : function (url, dataObj, callback, errorCallback) { var self = this; var img = new Image( ...
- yii2.0保留CSS样式的引入
<link rel="stylesheet" href="http://cdn.staticfile.org/twitter-bootstrap/3.2.0/css ...
- url操作等
取得url ?及以前: baseUrl = url.substr(0,url.indexOf('?')+1) searchParam = searchParam.slice(0, -1);//去掉最后 ...
- 用实例讲DynamicResource与StaticResource的区别
原文:用实例讲DynamicResource与StaticResource的区别 之前我的博客文章"WPF中的资源(Resource)"中概略性地提到过DynamicResourc ...
- Cocos2d-x3.1下实现相似iOS页面滑动指示圆点
原文地址:http://blog.csdn.net/qqmcy/article/details/37612457 代码下载:http://download.csdn.net/detail/qqmcy/ ...
- git clone命令简介
git clone: 正如上图,当我们打开终端的情况下,默认我们所在的目录是在/home/shiyanlou的,大家可以在终端输入以下命令把目录切换到桌面cd /home/Desktop这个时候输入 ...
- 关于WPF你应该知道的2000件事
原文 关于WPF你应该知道的2000件事 以下列出了迄今为止为WPF博客所知的2,000件事所创建的所有帖子. 帖子总数= 1,201 动画 #7 - 基于属性的动画 #686 - 使用动画制作图像脉 ...
- MVC WebApi
两种web服务 •SOAP风格:基于方法,产品是WebService •REST风格:基于资源,产品是WebAPI 对于数据的增.删.改.查,提供相对的资源操作,按照请求的类型进行相应处理,主要包括G ...