본문 바로가기

Algorithm/Code Fights (Code Signal)

Aracade Intro #16 AreSimilar?, Codefights, CodeSignal, 코드파이트, 코드시그널, c++ java c# scalar

 Aracade Intro #16 AreSimilar?, Codefights, CodeSignal, 코드파이트, 코드시그널, c++ java c# scalar



Q.


 Two arrays are called similar if one can be obtained from another by swapping at most one pair of elements in one of the arrays.


Given two arrays a and b, check whether they are similar.


 

 입력받은 두개의 배열 (a, b) 중에서, 어느 한개의 배열에서 한 쌍의 요소들 자리만 바꿔서 (위치는 서로 떨어져 있어도 상관없음) a, b 배열이 같아지면, 비슷하다는 것이 true 이다.


e.g.


 Input -> a = [1, 2, 3] and b = [1, 2, 3]

 Output -> areSimilar(a, b) = true.


The arrays are equal, no need to swap any elements.


 Input -> a = [1, 2, 3] and b = [2, 1, 3]

 Output -> areSimilar(a, b) = true.


We can obtain b from a by swapping 2 and 1 in b.


 Input -> a = [1, 2, 2] and b = [2, 1, 1]

 Output -> areSimilar(a, b) = false.


Any swap of any two elements either in a or in b won't make a and b equal.



//Process

//1. Input two integer vectors

//2. Iterate from length of any vector

// 2.1. Check if it's diff value in the same index in each vectors

// 2.2. If it's diff -> save index, and count

//3. If count is zero -> it's similar

// 3.1. If count is 2 -> check if it's same after swap index

//4. Return result



//처리과정

//1. 정수 배열 두개를 입력받는다.

//2. 배열 한개의 길이까지 반복한다. (같음)

// 2.1. 두 배열 같은 인덱스에 다른 값인지 확인해서

// 2.2. 다르면 인덱스 따로 저장하고, 카운트를 센다

//3. 카운트가 0이면 -> 트루

// 3.1. 카운트가 2이면 -> 해당 인덱스들 바꿔서 값을 비교해서 같으면 트루

//4. 결과값 출력




bool areSimilar(std::vector<int> a, std::vector<int> b) {

    bool areSimilar = false;

    int lengthA = a.size();

    

    int count = 0;

    int arr[2] = { 0, };

    for (int i = 0; i < lengthA && count < 3; ++i) {

        if (a[i] != b[i]) {

            if (count < 2) {

                arr[count] = i;

            }

            ++count;

        }

    }

    if (count == 2) {

        if (a[arr[0]] == b[arr[1]]

           && a[arr[1]] == b[arr[0]]) {

            areSimilar = true;

        }

    }

    else if (count == 0)

    {

        areSimilar = true;

    }

    return areSimilar;

}





Something else...




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