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 bearrayChange(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
以上是关于Code Signal_练习题_arrayChange的主要内容,如果未能解决你的问题,请参考以下文章
Code Signal_练习题_stringsRearrangement