Code Signal_练习题_arrayChange

Posted yd2018

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Code Signal_练习题_arrayChange相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

Code Signal_练习题_Minesweeper

Code Signal_练习题_stringsRearrangement

Code Signal_练习题_firstDigit

Code Signal_练习题_digitDegree

Code Signal_练习题_matrixElementsSum

Code Signal_练习题_alphabeticShift