码迷,mamicode.com
首页 > 其他好文 > 详细

Code Signal_练习题_arrayChange

时间:2018-07-24 23:47:57      阅读:327      评论:0      收藏:0      [点我收藏+]

标签:element   each   sig   color   +=   gif   range   练习   技术分享   

You are given an array of integers. On each move you are allowed to increase exactly one of its element by one. Find the minimal number of moves required to obtain a strictly increasing sequence from the input.

Example

For inputArray = [1, 1, 1], the output should be
arrayChange(inputArray) = 3.

 

 

我的解答:

1 def arrayChange(inputArray):
2     count = 0
3     for i in range(len(inputArray)-1):
4         if inputArray[i] >= inputArray[i+1]:
5             count += inputArray[i] + 1 - inputArray[i + 1]
6             inputArray[i+1] = inputArray[i] + 1
7     return count

 

膜拜大佬:

技术分享图片
def arrayChange(inputArray):
    a = 0
    for i in range(1, len(inputArray)):
        if inputArray[i] <= inputArray[i - 1]:
            f = (inputArray[i - 1] - inputArray[i]) + 1
            inputArray[i] = inputArray[i - 1] + 1
            a += f
    return a
View Code

 

Code Signal_练习题_arrayChange

标签:element   each   sig   color   +=   gif   range   练习   技术分享   

原文地址:https://www.cnblogs.com/YD2018/p/9363216.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!