본문 바로가기

Algorithm/Code Fights (Code Signal)

Aracade Intro #22 avoidObstacles. Algorithm, 알고리즘, Codefights, CodeSignal, regx, 정규표현식, 코드파이트, 코드시그널, c++ java c# scalar

Aracade Intro #22 avoidObstacles. Algorithm, Codefights, CodeSignal, regx, 정규표현식, 코드파이트, 코드시그널, c++ java c# scalar



Q.


 You are given an array of integers representing coordinates of obstacles situated on a straight line.


 Assume that you are jumping from the point with coordinate 0 to the right. You are allowed only to make jumps of the same length represented by some integer.


 Find the minimal length of the jump enough to avoid all the obstacles.



 정수들 배열을 입력받는데, 그 배열은 직선 좌표에서 장애물이 있는 좌표를 나타낸다.


 0에서부터 오른쪽으로 점프를 한다. 하나의 숫자 길이로만 점프를 할수 있는데, 장애물 전체를 피해서 갈 수 있는 최소 점프길이를 구하라.




e.g.


Input -> inputArray = [5, 3, 6, 7, 9]


Output -> avoidObstacles(inputArray) = 4.


Check out the image below for better understanding:







//Process

//1. Input obstaclesArray

//2. Iterate jumpLength 1 to inf

// 2.1. Iterate obstaclesArray begin index to the end

//  2.1.1. If there is no remainder after dividing obstaclesArray value by jumpLength

//            -> that jumpLength is caught by obstacle

// 2.2. If it's not caught by obstacle -> return that jumpLength

//3. Finish method



//처리과정

//1. obstaclesArray 를 입력받는다.

//2. 점프길이값을 1부터 증가시키며 반복한다. inf

// 2.1. obstaclesArray 시작인덱스 값부터 끝까지 반복한다.

//  2.1.1. abstacleArrays 의 값을 점프길이값으로 나누어 떨어지는지 확인한다.

//  2.1.2. 나누어 떨어지면 -> obstacle 에 걸림

// 2.2. obstacle 에 걸리지 않았으면 점프길이값 반환

//3. 끝낸다.




Code.. lemme see code!!!!!!!


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




int avoidObstacles(std::vector<int> obstaclesArray) {

    

    int length = obstaclesArray.size();

    

    for (int jumpLength = 1; ; ++jumpLength) 

    {

        bool isCaught = false;

        for (int i = 0; i < length && isCaught == false; ++i) 

        {

            if (obstaclesArray[i] % jumpLength == 0) 

            {

                isCaught = true;

            }

        }

        if (isCaught == false) 

        {

            return jumpLength;

        }

    }

}





Something else....



2018/10/28 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #21 isIPv4Address. Algorithm, 알고리즘, Codefights, CodeSignal, regx, 정규표현식, 코드파이트, 코드시그널, c++ java c# scalar

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

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

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

2018/10/24 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #17 ArrayChange, 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