본문 바로가기

Algorithm/Code Fights (Code Signal)

Aracade Intro #14 AlternatingSums, Codefights, CodeSignal

Aracade Intro #14 AlternatingSums, Codefights, CodeSignal


코드파이트, 코드시그널



Q.

 You are given an array of positive integers - the weights of the people. Return an array of two integers, where the first element is the total weight of team 1, and the second element is the total weight of team 2 after the division is complete.



 양의 정수 배열을 입력받고(사람들 몸무게), 두개의 정수를 반환하는데..

홀수번째 배열의 합(첫번째팀), 짝수번째 사람들 무게의 합(두번째팀) 을 반환한다.



e.g.


Input     a = [50, 60, 60, 45, 70]

Output    alternatingSums(a) = [180, 105]



//Process

//1. Input integer vector

//2. Iterate from begin to the end

// 2.1. If it's odd index, -> add to firstTeamWeight

// 2.2. If it's even index, -> add to secondTeamWeight

//3. Return teams' weight


//처리과정

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

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

// 2.1. 해당 인덱스가 홀수면 -> 값을 firstTeamWeight 에 더한다.

// 2.2. 해당 인덱스가 짝수면 -> 값을 secondTeamWeight 에 더한다.

//3. 팀 무게를 반환한다.




Code... Let me see code!!!

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




std::vector<int> alternatingSums(std::vector<int> a) {

    std::vector<int> teamsWeight(2);

    

    int length = a.size();

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

        if (i % 2 == 0) {

            teamsWeight.insert(teamsWeight.begin(), teamsWeight.at(0) + a.at(i));

            teamsWeight.erase(teamsWeight.begin() + 1);

        }

        else {

            teamsWeight.insert(teamsWeight.begin() + 1, teamsWeight.at(1) + a.at(i));

            teamsWeight.erase(teamsWeight.begin() + 2);

        }

    }

    return teamsWeight;

}







Something else...



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

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

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

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

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

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

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

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

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

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

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

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

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