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

solution(inputArray) = 3.

Input/Output

풀이

int solution(int[] inputArray) {
    // 증가 수열을 만들때 쓰인 수의 합
    int sum = 0;
    
    for (int i = 0; i < inputArray.length - 1; i ++) {
        // 현재 인덱스의 숫자가 다음 인덱스의 숫자보다 크거나 같을 경우
        if (inputArray[i] >= inputArray[i + 1]) {
            // 현재 숫자 - 다음 숫자 + 1 을 더해준다.
            int n = inputArray[i] - inputArray[i + 1] + 1;
            // 더한 값 갱신
            inputArray[i + 1] += n;
            // 누적
            sum += n;
        }
    }
    
    return sum;
}