알고리즘/프로그래머스
2021 Dev-Matching: 웹 백엔드 개발자(상반기)(로또의 최고 순위와 최저 순위)
칼퇴시켜주세요
2022. 12. 24. 14:48
728x90
2021 Dev-Matching 상반기 1번 문제로 지워진 로또 번호를 가지고 최대 등수와 최소 등수를 구하는 문제이다.
번호는 6개 밖에 안되기 때문에 2중 for문을 돌려도 n^2 즉 36 번밖에 연산을 하지 않는다.
#include <string>
#include <vector>
using namespace std;
vector<int> solution(vector<int> lottos, vector<int> win_nums) {
vector<int> answer;
int match = 0;
int zeroCnt = 0;
for(int i = 0; i < lottos.size(); i++)
{
if(lottos[i] == 0)
{
zeroCnt++;
continue;
}
for(int j = 0; j < win_nums.size(); j++)
{
if(lottos[i] == win_nums[j])
{
match++;
}
}
}
if(match + zeroCnt >= 2)
{
answer.push_back(7 - (match + zeroCnt));
}
else
{
answer.push_back(6);
}
if(match >= 2)
{
answer.push_back(7 - match);
}
else
{
answer.push_back(6);
}
return answer;
}
반응형