본문 바로가기

Algorithm/Code Fights (Code Signal)

Aracade Intro #17 ArrayChange, Codefights, CodeSignal, 코드파이트, 코드시그널, c++ java c# scalar

Aracade Intro #17 ArrayChange, Codefights, CodeSignal, 코드파이트, 코드시그널, c++ java c# scalar



 Q.


 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.


 정수 배열을 입력받고, 인덱스 순서로 따라올라가는데.. 한 행동에 한개의 숫자 1씩을 증가시킬수 있다. 전부 오름차순이 되도록 해야된다. 전부 오름차순이 되는데 움직인 개수를 출력하라.



e.g.


Input -> [1, 1, 1]


Output -> arrayChange(inputArray) = 3

 

1 -> [1, 2, 1]

2 -> [1, 2, 2]

3 -> [1, 2, 3]



Input -> [2, 3, 3, 5, 5, 5, 4, 12, 12, 10, 15]


Output -> arrayChange(inputArray) = 13

 

1 -> [2, 3, 4, 5, 5, 5, 4, 12, 12, 10, 15]

2 -> [2, 3, 4, 5, 6, 5, 4, 12, 12, 10, 15]

3 -> [2, 3, 4, 5, 6, 6, 4, 12, 12, 10, 15]

4 -> [2, 3, 4, 5, 6, 7, 4, 12, 12, 10, 15]

5 -> [2, 3, 4, 5, 6, 7, 5, 12, 12, 10, 15]

6 -> [2, 3, 4, 5, 6, 7, 6, 12, 12, 10, 15]

7 -> [2, 3, 4, 5, 6, 7, 7, 12, 12, 10, 15]

8 -> [2, 3, 4, 5, 6, 7, 8, 12, 12, 10, 15]

9 -> [2, 3, 4, 5, 6, 7, 8, 12, 13, 10, 15]

10 -> [2, 3, 4, 5, 6, 7, 8, 12, 13, 11, 15]

11 -> [2, 3, 4, 5, 6, 7, 8, 12, 13, 12, 15]

12 -> [2, 3, 4, 5, 6, 7, 8, 12, 13, 13, 15]

13 -> [2, 3, 4, 5, 6, 7, 8, 12, 13, 14, 15]




//Process

//1. Input integer array

//2. Iterate from begin+1 to the end

// 2.1. Check if frontIndex integer is bigger than rear one

// 2.2. If so -> Iterate till rearIndex integer is bigger than front one

//  2.2.1. Add 1 to rearIndex integer

//  2.2.2. Count++

//3. Return count



//처리과정

//1. 정수배열을 입력받는다.

//2. 시작+1 부터 끝까지 반복한다.

// 2.1. 앞 인덱스 정수가 뒤 인덱스 정수보다 큰지 확인한다.

// 2.2. 앞이 더 크면 -> 뒤인덱스 정수가 앞인덱스 정수보다 클때까지 반복한다.

//  2.2.1. 뒤 인덱스 정수에 플러스 1

//  2.2.2. 카운트를 센다.

//3. 카운트를 반환한다.



Code.. let me see code...


코드.. 코드를 보자...



int arrayChange(std::vector<int> inputArray) {

    int length = inputArray.size();

    int count = 0;

    for (int i = 1; i < length; ++i) {

        while (inputArray[i-1] >= inputArray[i]) {

            ++count;

            ++inputArray[i];

        }

    }

    return count;

}



Something else...



2018/10/24 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #17 AreSimilar?, Codefights, CodeSignal, 코드파이트, 코드시그널, c++ java c# scalar

2018/10/21 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #16 AreSimilar?, Codefights, CodeSignal, 코드파이트, 코드시그널, c++ java c# scalar

2018/10/21 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #15 AddBorder, Codefights, CodeSignal, 코드파이트, 코드시그널, c++ java c# scalar

2018/10/20 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #14 AlternatingSums, Codefights, CodeSignal

2018/10/19 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #13 ReverseParentheses, Codefights, CodeSignal

2018/10/08 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #12 SortByHeight

2018/10/06 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #11 IsLucky

2018/10/03 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #10 CommonCharacterCount

2018/09/26 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #9 AllLongestStrings

2018/09/24 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #8 MatrixElementsSum

2018/09/23 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #7 AlmostIncreasingSequence

2018/09/22 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #6 MakeArrayConsecutive2

2018/09/21 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #5 ShapeArea

2018/09/19 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #4 AdjacentElementsProduct

2018/09/16 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #3 CheckPalindrome

2018/09/16 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #2 CenturyFromYear

2018/09/16 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #1 Add