반응형

문제 출처

https://programmers.co.kr/learn/courses/30/lessons/12987

 

코딩테스트 연습 - 숫자 게임

xx 회사의 2xN명의 사원들은 N명씩 두 팀으로 나눠 숫자 게임을 하려고 합니다. 두 개의 팀을 각각 A팀과 B팀이라고 하겠습니다. 숫자 게임의 규칙은 다음과 같습니다. 먼저 모든 사원이 무작위로 �

programmers.co.kr

 

문제 풀이

정렬한뒤 풀이하는게 핵심이다. A, B벡터 모두 가장 큰수부터 비교를 해 B가 더 크다면 승점 확보와 index를 감소시킨다.

A의 가장 큰수 vs B의 가장 큰수가 붙었을때 A가 더 크다면 B에서 이길 수 있는 수가 없으므로 A의 index를 하나 감소시켜 재 비교해주면 된다.

 

 

소스 코드

https://github.com/sw93/algorithm/blob/master/Programmers/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4%20-%20%EC%88%AB%EC%9E%90%20%EA%B2%8C%EC%9E%84.cpp

 

sw93/algorithm

problem solve. Contribute to sw93/algorithm development by creating an account on GitHub.

github.com

#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int solution(vector<int> A, vector<int> B) {
    int answer = 0;
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
    
    int aIdx = A.size()-1;
    int bIdx = B.size()-1;
    while (1) {
        if (aIdx < 0) break;
        if (A[aIdx] < B[bIdx]) {
            bIdx--;
            answer++;
        }
        aIdx--;
    }
    
    return answer;
}
반응형

+ Recent posts