Solving arrayChange in CodeSignal Arcade Problem

Viewed 44

I am currently working on the arrayChange level of Code Fights Arcade. This is the objective: 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.

I have written this code in Python 3:

def solution(inputArray):
    counter = 0
    for i in range(len(inputArray)-1):
        if inputArray[i] < inputArray[i+1]:
            continue
        else:
            while inputArray[i+1] <= inputArray[i]:
                inputArray[i+1] += 1
                counter += 1
    return counter

It passes all the non-hidden tests, but fails the 7th hidden test. Can someone please help me figure out what bug my code has? Thank you so much in advance!

1 Answers

Please check your problem with this solution.

def solution(inputArray):
    res = 0
    for i in range(len(inputArray) - 1):
        a = inputArray[i]
        b = inputArray[i + 1]

        if a >= b:
            steps = a - b + 1
            inputArray[i + 1] += steps
            res += steps
    return res
Related