문제 링크
https://school.programmers.co.kr/learn/courses/30/lessons/77484
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
나의 풀이
아웃풋으로 구해야하는 것은 결국 나의 최대 등수, 최소 등수만 구하면 된다.
그래서 0의 카운트도 0이 다 맞았을 경우, 0이 다 틀렸을 경우 이렇게 2개의 상황만 추가해주면된다.
- 1등부터 6등까지 몇 개를 맞추면 되는지 map을 사용해서 키, 밸류를 설정
- lottos를 순회하면서 win_nums의 번호가 있는지 확인하고 있다면 최소 카운트 증가
- 그리고 0이면 0을 관리하는 카운트 증가
- 순회가 끝나고 최대 갯수에 최소 개수 + 제로 카운트
- 각 최대, 최소 개수의 밸류값을 answer에 넣어줌
나의 코드
#include <string>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
vector<int> solution(vector<int> lottos, vector<int> win_nums) {
vector<int> answer;
int min_count = 0, max_count = 0;
map<int, int> score;
score[6] = 1;
score[5] = 2;
score[4] = 3;
score[3] = 4;
score[2] = 5;
score[1] = 6;
score[0] = 6;
for (int value : lottos)
{
if (value == 0)
max_count++;
if (find(win_nums.begin(), win_nums.end(), value) - win_nums.begin() != win_nums.size())
min_count++;
}
max_count+=min_count;
answer.push_back(score[max_count]);
answer.push_back(score[min_count]);
return answer;
}
'코딩테스트 준비' 카테고리의 다른 글
[ C++ ] 프로그래머스 체육복 (0) | 2025.03.13 |
---|---|
[ C++ ] 프로그래머스 카드 뭉치 (0) | 2025.02.10 |
[C++] 프로그래머스 3진법 뒤집기 (0) | 2025.01.16 |
[C++] 프로그래머스 자릿수 더하기 (0) | 2024.12.26 |
코딩테스트 공부 순서 (3) | 2024.10.22 |