본문 바로가기

코딩 테스트

[Hacker Rank] (Easy) Compare the Triplets

728x90

문제


Alice and Bob each created one problem for HackerRank. A reviewer rates the two challenges, awarding points on a scale from 1 to 100 for three categories: problem clarity, originality, and difficulty.

The rating for Alice's challenge is the triplet a = (a[0], a[1], a[2]), and the rating for Bob's challenge is the triplet b = (b[0], b[1], b[2]).

The task is to find their comparison points by comparing a[0] with b[0], a[1] with b[1], and a[2] with b[2].

  • If a[i] > b[i], then Alice is awarded 1 point.
  • If a[i] < b[i], then Bob is awarded 1 point.
  • If a[i] = b[i], then neither person receives a point.

Comparison points is the total points a person earned.

Given a and b, determine their respective comparison points.

 

Example

a = [1, 2, 3]
b = [3, 2, 1]

  • For elements *0*, Bob is awarded a point because a[0] .

The return array is [1, 1] with Alice's score first and Bob's second.

 

Function Description

Complete the function compareTriplets in the editor below.

compareTriplets has the following parameter(s):

  • int a[3]: Alice's challenge rating
  • int b[3]: Bob's challenge rating

 

Return

  • int[2]: Alice's score is in the first position, and Bob's score is in the second.

Input Format

The first line contains 3 space-separated integers, a[0], a[1], and a[2], the respective values in triplet a.
The second line contains 3 space-separated integers, b[0], b[1], and b[2], the respective values in triplet b.

Constraints

  • 1 ≤ a[i] ≤ 100
  • 1 ≤ b[i] ≤ 100

 

- 입력 예시 -

 

// (case 1)
17 28 30
99 16 8

 

 출력 예시  -

 

// (case 1)
2 1

정답 코드


<  내 정답 코드  >

 

class Result3 {

    public static List<Integer> compareTriplets(List<Integer> a, List<Integer> b) {
        List<Integer> scoreBoard = new ArrayList<>(Arrays.asList(0, 0));
        final int A_INDEX = 0;
        final int B_INDEX = 1;

        for (int index = 0; index < a.size(); index++) {
            int score;

            if (a.get(index) > b.get(index)) {
                score = scoreBoard.get(A_INDEX);
                scoreBoard.set(A_INDEX, ++score);
                continue;
            }
            if (a.get(index) < b.get(index)) {
                score = scoreBoard.get(B_INDEX);
                scoreBoard.set(B_INDEX, ++score);
                continue;
            }
            continue;
        }
        return scoreBoard;
    }
}

 

<  타인 답변 코드  >

 

public class Problem3 {

    public static List<Integer> compareTriplets(List<Integer> a, List<Integer> b) {
        int left = 0;
        int right = 0;
        for (int i = 0; i < a.size(); i++) {
            if (a.get(i) > b.get(i)) {
            	left++;
            }
            if (a.get(i) < b.get(i)) {
            	right++;
            }
        }
        List<Integer> anwers = new ArrayList<>();
        anwers.add(left);
        anwers.add(right);
        return anwers;
    }
}

이것을 주의하자!


-  내 코드는 처음부터 ArrayList 로 저장하기 위해서 매번 비교할 때마다 해당 List에 저장된 요소값을 불러온 다음 + 1 을 하고 다시 저장한다. 이를 위해서 . set( ) 메서드를 사용했다.

 

-  . set( int  index Object  obj ) 메서드는 해당 인덱스 자리에 새로운 값으로 변경한다.

 

-  다른 사람의 코드를 보면, 처음부터 리스트를 만들지 않고 일단 변수 두 개를 선언한 다음 변화가 있을 때마다 + 1 을 해준다. 그리고 마지막에 그 변수를 List 에 저장해서 반환한다. 이 코드가 훨씬 깔끔하고 효율적이다..!