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 be
arrayMaxConsecutiveSum(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 is 8.

我的解答:

第一种:
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的更多相关文章

  1. Code Signal_练习题_digitDegree

    Let's define digit degree of some positive integer as the number of times we need to replace this nu ...

  2. Code Signal_练习题_Knapsack Light

    You found two items in a treasure chest! The first item weighs weight1 and is worth value1, and the ...

  3. Code Signal_练习题_growingPlant

    Each day a plant is growing by upSpeed meters. Each night that plant's height decreases by downSpeed ...

  4. Code Signal_练习题_differentSymbolsNaive

    Given a string, find the number of different characters in it. Example For s = "cabca", th ...

  5. Code Signal_练习题_firstDigit

    Find the leftmost digit that occurs in a given string. Example For inputString = "var_1__Int&qu ...

  6. Code Signal_练习题_extractEachKth

    Given array of integers, remove each kth element from it. Example For inputArray = [1, 2, 3, 4, 5, 6 ...

  7. Code Signal_练习题_stringsRearrangement

    Given an array of equal-length strings, check if it is possible to rearrange the strings in such a w ...

  8. 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) + ...

  9. Code Signal_练习题_depositProfit

    You have deposited a specific amount of money into your bank account. Each year your balance increas ...

随机推荐

  1. Swift 里 Array (四) Accessing Elements

    根据下标取值 关键代码如下: func _getElement( _ index: Int, wasNativeTypeChecked: Bool, matchingSubscriptCheck: _ ...

  2. Linux链接脚本学习--lds(转)

    Linux链接脚本学习--lds 一.概论 ld: GNU的链接器. 用来把一定量的目标文件跟档案文件链接在一起,并重新定位它们的数据,链接符号引用. 一般编译一个程序时,最后一步就是运行ld进行链接 ...

  3. Python(27)--文件相关处理的应用(增、删、改、查)

    文件名为message,文件内容如下: global log 127.0.0.1 local2 daemon maxconn 256 log 127.0.0.1 local2 info default ...

  4. python3.6使用f-string来格式化字符串

    这里的f-string指的是以f或F修饰的字符串,在字符串中使用{}来替换变量,表达式和支持各种格式的输出.详细的格式化定义可以看官方文档 >>> a, b = 30, 20 > ...

  5. 公共技术点( Java 反射 Reflection)

    转载路径:http://p.codekk.com/blogs/detail/5596953ed6459ae7934997c5 本文为 Android 开源项目源码解析 公共技术点中的 Java 反射 ...

  6. sdcard 导入文件错误

    把 AVI 文件导入到 sdcard 时,报 Failed to push selection: Read-only file system 错误. 解决办法: 1.在命令行中输入:adb shell ...

  7. discuz 文件模板edit

    1.修改title Power by discuz! 位置:template/default/common   --->header_common.htm 2.discuz.htm 文件路径(修 ...

  8. jar包运行main程序

    当把java项目打包成jar后,如何运行main函数呢? 第一种:指定运行main类: 1 java -cp test.jar com.hk.app.Application 第二种:在MANIFEST ...

  9. Maven 进阶

    一.Maven 版本管理 Maven 的推荐版本号约定为:主版本号.次版本号.增量版本号-<里程碑版本> 开发中的版本要以 -SNAPSHOT 结尾,因为这种快照版本是支持 jar 包被覆 ...

  10. GitHub多人协作简明教程

    本文面向已经了解/熟悉git基本命令但是并不熟悉如何使用GitHub进行多人协作开发项目的同学. 为了简单起见,这里假设只有两个开发人员,HuanianLi 和 DaxiangLi.他们在GitHub ...