Code Signal_练习题_arrayMaxConsecutiveSum
Given array of integers, find the maximal possible sum of some of its k consecutive elements.
Example
For inputArray = [2, 3, 5, 1, 6] and k = 2, the output should bearrayMaxConsecutiveSum(inputArray, k) = 8.
All possible sums of 2 consecutive elements are:
2 + 3 = 5;3 + 5 = 8;5 + 1 = 6;1 + 6 = 7.
Thus, the answer is8.
我的解答:
第一种:
def arrayMaxConsecutiveSum(inputArray, k):
return max([sum(inputArray[i:i + k]) for i in range(len(inputArray))])
第二种:
def arrayMaxConsecutiveSum(inputArray, k):
return sorted([sum(inputArray[i:i + k]) for i in range(len(inputArray))])[-1] 这两种虽然都可以运行,但是运行时间太慢了.
def arrayMaxConsecutiveSum(inputArray, k):
# return max([sum(inputArray[i:k+i]) for i in range(len(inputArray) - k + 1)])
# too slow, but works
s = m = sum(inputArray[:k])
for i in range(k, len(inputArray)):
s += inputArray[i] - inputArray[i-k]
if s > m: m = s
return m
膜拜大佬
Code Signal_练习题_arrayMaxConsecutiveSum的更多相关文章
- Code Signal_练习题_digitDegree
Let's define digit degree of some positive integer as the number of times we need to replace this nu ...
- Code Signal_练习题_Knapsack Light
You found two items in a treasure chest! The first item weighs weight1 and is worth value1, and the ...
- Code Signal_练习题_growingPlant
Each day a plant is growing by upSpeed meters. Each night that plant's height decreases by downSpeed ...
- Code Signal_练习题_differentSymbolsNaive
Given a string, find the number of different characters in it. Example For s = "cabca", th ...
- Code Signal_练习题_firstDigit
Find the leftmost digit that occurs in a given string. Example For inputString = "var_1__Int&qu ...
- Code Signal_练习题_extractEachKth
Given array of integers, remove each kth element from it. Example For inputArray = [1, 2, 3, 4, 5, 6 ...
- Code Signal_练习题_stringsRearrangement
Given an array of equal-length strings, check if it is possible to rearrange the strings in such a w ...
- Code Signal_练习题_absoluteValuesSumMinimization
Given a sorted array of integers a, find an integer x from a such that the value of abs(a[0] - x) + ...
- Code Signal_练习题_depositProfit
You have deposited a specific amount of money into your bank account. Each year your balance increas ...
随机推荐
- ASP.NET Core WebApi 项目部署到 IIS 服务器的总结
Point: - ASP.NET Core WebApi 项目 - 发布到 IIS 服务器 1. 选择 File System 2. 输入要发布到的路径 # 其它默认,直接发布 3. 打开 IIS,添 ...
- 记录jquery的ajax
1.直接干货 ajax很简单jquery有很好的支持,原生js就不写了.总的说常用的有3个方法 $.post $.get $.ajax 具体参数参考教程http://www.runoob.com/jq ...
- 没啥事用C语言写一个Trie tree玩玩,支持中英文,用g++编译通过
#include <cstdio> #include <cstdlib> #include <vector> #define ALPHABETS 2600000 # ...
- 在MongoDB中实现聚合函数
在MongoDB中实现聚合函数 随着组织产生的数据爆炸性增长,从GB到TB,从TB到PB,传统的数据库已经无法通过垂直扩展来管理如此之大数据.传统方法存储和处理数据的成本将会随着数据量增长而显著增加. ...
- CDN基本工作过程
看了一些介绍CDN的文章,感觉这篇是讲的最清楚的. 使用CDN会极大地简化网站的系统维护工作量,网站维护人员只需将网站内容注入CDN的系统,通过CDN部署在各个物理位置的服务器进行全网分发,就可以实现 ...
- JavaScript -- History
-----042-History.html----- <!DOCTYPE html> <html> <head> <meta http-equiv=" ...
- Python基础内容
1.注释 #单行注释 ‘“多行注释”’ 2.变量 Python没有声明变量的过程(动态类型) 变量名=值,如果是浮点数就定义为浮点类型,如果是整型就定义为整型,如果是字符串就定义为字符串 3.输入和输 ...
- java学习-http中get请求的非ascii参数如何编码解码探讨
# 背景: 看着别人项目代码看到一个PathUtils工具类, 里面只有一个方法,String rebuild(String Path),将路径进行URLDecoder.decode解码,避免路径中 ...
- git 删除远程分支文件夹
把不需要版本控制的文件提交到远程分支上后,需要删除远程分支上的文件,用以下操作即可: git rm -r –cached dirname //删除远程文件夹,但保留本地文件夹 git commit - ...
- springboot-28-security(一)用户角色控制
spring security 使用众多的拦截器实现权限控制的, 其核心有2个重要的概念: 认证(Authentication) 和授权 (Authorization)), 认证就是确认用户可以访问当 ...