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

猜你喜欢

转载自www.cnblogs.com/YD2018/p/9363216.html