# 생각
최고 순위는 모르는 숫자가 다 일치한다는 가정으로 볼 수 있어
모르는 숫자의 개수 + 일치한 숫자의 개수가 되고
최저 순위는 모르는 숫자가 다 일치하지 않는다는 가정으로 볼 수 있어
일치한 숫자의 개수만 된다
탐색을 통해 0의 개수와 일치하는 숫자의 개수를 찾아 저장해주면 쉽게 해결 가능하다
# 전체 코드
#include <bits/stdc++.h>
using namespace std;
vector<int> solution(vector<int> lottos, vector<int> win_nums) {
vector<int> answer;
int rank[7] = { 6,6,5,4,3,2,1 };
int correct = 0;
int unknown = 0;
for (auto& i : lottos)
{
if (i == 0) unknown++;
}
for (auto& idx : win_nums)
{
auto it = find(lottos.begin(), lottos.end(), idx);
if (it != lottos.end()) correct++;
}
int tpRank = rank[unknown + correct];
int btRank = rank[correct];
answer.push_back(tpRank);
answer.push_back(btRank);
return answer;
}
출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges
'Algorithm > 프로그래머스' 카테고리의 다른 글
[Lv.1] 완주하지 못한 선수 with cpp (0) | 2022.02.28 |
---|---|
[Lv.1] 크레인 인형뽑기 게임 with cpp (0) | 2022.02.20 |
[Lv.1] 키패드 누르기 with cpp (0) | 2022.02.20 |
[Lv.1] 신규 아이디 추천 with cpp (0) | 2022.02.10 |
[Lv.1] 신고 결과 받기 with cpp (0) | 2022.01.29 |