Easy

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.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer inputArray

    Guaranteed constraints:
    3 ≤ inputArray.length ≤ 10^5,
    -10^5 ≤ inputArray[i] ≤ 10^5.

  • [output] integer

    • The minimal number of moves needed to obtain a strictly increasing sequence from inputArray.
      It's guaranteed that for the given test cases the answer always fits signed 32-bit integer type.

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

// Arrays are already defined with this interface:
// typedef struct arr_##name {
//   int size;
//   type *arr;
// } arr_##name;
//
// arr_##name alloc_arr_##name(int len) {
//   arr_##name a = {len, len > 0 ? malloc(sizeof(type) * len) : NULL};
//   return a;
// }
//
//
int arrayChange(arr_integer inputArray)
{
	int count=0;

	for(int i=1;i<inputArray.size;i++)
		if(inputArray.arr[i]<=inputArray.arr[i-1])
		{
			count+=inputArray.arr[i-1]-inputArray.arr[i]+1;
			inputArray.arr[i]=inputArray.arr[i-1]+1;
		}

	return count;
}

 

728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> areEquallyStrong  (0) 2020.04.05
<Codesignal> palindromeRearranging  (0) 2020.04.05
<Codesignal> Are Similar?  (0) 2020.04.05
<Codesignal> Add Border  (0) 2020.04.05
<Codesignal> alternatingSums  (0) 2020.04.05

+ Recent posts