Max double slice sum 的解法
1. 上题目:
Task description
A non-empty zero-indexed array A consisting of N integers is given.
A triplet (X, Y, Z), such that 0 ≤ X < Y < Z < N, is called a double slice.
The sum of double slice (X, Y, Z) is the total of A[X + 1] + A[X + 2] + ... + A[Y − 1] + A[Y + 1] + A[Y + 2] + ... + A[Z − 1].
For example, array A such that:
A[0] = 3
A[1] = 2
A[2] = 6
A[3] = -1
A[4] = 4
A[5] = 5
A[6] = -1
A[7] = 2
contains the following example double slices:
- double slice (0, 3, 6), sum is 2 + 6 + 4 + 5 = 17,
- double slice (0, 3, 7), sum is 2 + 6 + 4 + 5 − 1 = 16,
- double slice (3, 4, 5), sum is 0.
The goal is to find the maximal sum of any double slice.
Write a function:
int solution(int A[], int N);
that, given a non-empty zero-indexed array A consisting of N integers, returns the maximal sum of any double slice.
For example, given:
A[0] = 3
A[1] = 2
A[2] = 6
A[3] = -1
A[4] = 4
A[5] = 5
A[6] = -1
A[7] = 2
the function should return 17, because no double slice of array A has a sum of greater than 17.
Assume that:
- N is an integer within the range [3..100,000];
- each element of array A is an integer within the range [−10,000..10,000].
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.
Copyright 2009–2015 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
大体意思很简单,就是求以X为中心的不包含X的左右两段slice的和的最大值。
2.题目分析
这个题目我乍看之下,觉得是一个很动态的问题,有三个变量,一个是中心点X,一个是左边界Y,一个是右边界Z;有了这三个点才能确定出一个确定的double slice。暴力算法的时间复杂度有O(N3). 但是题目要求O(N)。所以肯定存在一种简便算法来完成这个操作,即通过空间来换时间。
我们冷静的分析一下。
首先,这个最大点,肯定是以中间点X为轴左右两段相加和。对于X来说,这个点的max等于左边的max加上右边的max。即我们只要知道,加到X-1处左边能够加到的最大max,和,从右加到X+1处,右边的max,两者相加就为以X点为轴的max。对于其他左sum与右sum来说,必然小于这个max。
那么我们再以O(N)的时间复杂度遍历所有X点,找出最大的即可~
所以现在的问题就变成,找出每一个点X,左侧的连续max slice sum,以及右侧是max slice sum。
这时,我们惊喜的发现,通过递推的算法,我们又可以通过O(N)的时间复杂度完成这个问题~
1.如果我们确定了i点左侧最大sum,那么i+1点最侧最大的sum就是 max((sum[i]+A[i]),0)
2.通过递推,可以用线性时间复杂度完成 foreMax[] 的构建。
3.同理,可以完成postMax[]的构建。
通过这两个数组在每一个节点上的求和,我们便可找到最大的double slice sum的值哦~
时间复杂度为 O(3*N), 空间复杂度为O(2*N);
3.结果:

// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n"); int solution(int A[], int N) {
// write your code in C99
int foreMax[N];
int postMax[N];
A[]=;
A[N-]=; foreMax[]=;
// foreMax[1]=0;
postMax[N-]=;
int i;
for(i=;i<N;i++)
{
if(foreMax[i-]+A[i-]>)
{
foreMax[i]=foreMax[i-]+A[i-];
}
else
{
foreMax[i]=;
}
} for(i=N-;i>=;i--)
{
if((postMax[i+]+A[i+])>)
{
postMax[i]=postMax[i+]+A[i+];
}
else
{
postMax[i]=;
}
}
int max = ; for(i=;i<N-;i++)
{
if(foreMax[i]+postMax[i]>max)
{
max = foreMax[i]+postMax[i];
}
}
return max;
}
Max double slice sum 的解法的更多相关文章
- python 13:数字列表统计方法(min(list)、max(list)、sum(list))
numbers = list(range(1,11)) print(numbers) print(min(numbers)) #获得列表最小值 print(max(numbers)) #获得列表最大值 ...
- elasticsearch 聚合函数 max double精度损失bug
测试样例数据{ "size" : 0, "query" : { "bool" : { "must" : { " ...
- 询问任意区间的min,max,gcd,lcm,sum,xor,or,and
给我们n个数,然后有m个询问,每个询问为L,R,询问区间[L,R]的最大最小值,最小公约数,最大公约数,和,异或,或,且 这些问题通通可以用RMQ的思想来解决. 以下用xor来作为例子 设dp[i][ ...
- Hive函数:SUM,AVG,MIN,MAX
转自:http://lxw1234.com/archives/2015/04/176.htm,Hive分析窗口函数(一) SUM,AVG,MIN,MAX 之前看到大数据田地有关于max()over(p ...
- C# 中奇妙的函数–6. 五个序列聚合运算(Sum, Average, Min, Max,Aggregate)
今天,我们将着眼于五个用于序列的聚合运算.很多时候当我们在对序列进行操作时,我们想要做基于这些序列执行某种汇总然后,计算结果. Enumerable 静态类的LINQ扩展方法可以做到这一点 .就像之前 ...
- HDU 1003 Max Sum(AC代码)
#include <stdio.h> int main(){ int i,t,j,n,x; int start,end,temp,max,sum; scanf("%d" ...
- HDU--1003 Max Sum(最大连续子序列和)
Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum ...
- Hive学习之路 (十三)Hive分析窗口函数(一) SUM,AVG,MIN,MAX
数据准备 数据格式 cookie1,, cookie1,, cookie1,, cookie1,, cookie1,, cookie1,, cookie1,, 创建数据库及表 create datab ...
- hdu1024 Max Sum Plus Plus 滚动dp
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
随机推荐
- .Net中DLL冲突解决(真假美猴王)
<西游记>中真假美猴王让人着实难以区分,但是我们熟知了其中的细节也不难把他们剥去表象分别出来.对问题不太关心的可以直接调到文中关于.Net文件版本的介绍 问题 最近在编译AKKA.net ...
- Android LayoutInflater.inflate(int resource, ViewGroup root, boolean attachToRoot)的参数理解
方法inflate(int resource, ViewGroup root, boolean attachToRoot) 中 第一个参数传入布局的资源ID,生成fragment视图,第二个参数是视图 ...
- Maven与Ant比较
Maven与Ant比较 0 « 上一篇:Jenkins学习三:介绍一些Jenkins的常用功能» 下一篇:Jenkins学习四:Jenkins 邮件配置 posted @ 2015-03-25 16: ...
- ORA-00054: resource busy and acquire with NOWAIT specified
删除表时遇到 ORA-00054:资源正忙,要求指定NOWAIT 错误.以前在灾备中心遇到过. 资源被锁定了,没有办法删除. 报错日志:ORA-00054: resource busy and acq ...
- 【repost】js 常见错误类型
1)SyntaxError SyntaxError是解析代码时发生的语法错误 // 变量名错误 var 1a; // 缺少括号 console.log 'hello'); (2)Referenc ...
- [java]设计模式1-单例模式
单例模式:单例模式确保某个类只有一个实例,而且自行实例化并向整个系统提供这个实例.在计算机系统中,线程池.缓存.日志对象.对话框.打印机.显卡的驱动程序对象常被设计成单例.这些应用都或多或少具有资源管 ...
- [LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- 【原】Learning Spark (Python版) 学习笔记(四)----Spark Sreaming与MLlib机器学习
本来这篇是准备5.15更的,但是上周一直在忙签证和工作的事,没时间就推迟了,现在终于有时间来写写Learning Spark最后一部分内容了. 第10-11 章主要讲的是Spark Streaming ...
- C# 7.0 新功能代码范例
随着新版本的IDE Visual Studio 15 紧锣密鼓的开发中,2016年8月24日,微软发布了与之配套的C# 7.0 preview 的新特性. 其主要特性有: 内联声明out变量 (Out ...
- Spring缓存机制的理解
在spring缓存机制中,包括了两个方面的缓存操作:1.缓存某个方法返回的结果:2.在某个方法执行前或后清空缓存. 下面写两个类来模拟Spring的缓存机制: package com.sin90lzc ...