352. 로또의 최고 순위와 최저 순위

김해준's avatar
Mar 18, 2026
352. 로또의 최고 순위와 최저 순위

언어 : Java

문제

notion image
notion image
notion image

풀이 코드

import java.util.*; class Solution { public int[] solution(int[] lottos, int[] win_nums) { int[] answer = new int[2]; int min = 0; int blank = 0; Map<Integer,Integer> map = new HashMap(); for(int i = 0 ; i < lottos.length; i++){ if(lottos[i] == 0){ blank++; }else{ map.put(lottos[i],1); } } for(int i = 0 ; i < win_nums.length; i++){ if(map.containsKey(win_nums[i])){ min++; } } answer[0] = ConvertRank(min + blank); answer[1] = ConvertRank(min); return answer; } public static int ConvertRank(int count){ int result = 6; if(count == 2){ result = 5; }else if(count == 3){ result = 4; }else if(count == 4){ result = 3; }else if(count == 5){ result = 2; }else if(count == 6){ result = 1; } return result; } }

풀이 해설

1️⃣ 0(미확인 번호) 개수 + 번호 저장

for(int i = 0 ; i < lottos.length ; i++){ if(lottos[i]==0){ blank++; }else{ map.put(lottos[i],1); } }

✔ 의미

  • 0 → 번호를 모름 → 나중에 유리하게 사용 가능
  • 나머지 숫자 → map에 저장 (중복 체크 + 빠른 조회용)
👉 결과
  • blank = 모르는 숫자 개수
  • map = 확정된 번호들

2️⃣ 당첨 번호와 비교 (최소 맞은 개수)

for(int i = 0 ; i < win_nums.length ; i++){ if(map.containsKey(win_nums[i])){ min++; } }

✔ 의미

  • 확정된 번호 중 실제로 맞은 개수 = min
👉 이건 최저 순위 기준

3️⃣ 최고 / 최저 경우 계산

answer[0]=ConvertRank(min+blank); answer[1]=ConvertRank(min);

✔ 핵심 아이디어

경우
의미
min
확정적으로 맞은 개수 → 최저 순위
min + blank
0이 전부 맞았다고 가정 → 최고 순위
👉 예시
  • min = 2, blank = 3
  • 최고: 5개 맞음
  • 최저: 2개 맞음

4️⃣ 등수 변환 함수

public static int ConvertRank(intcount){ int result = 6; if(count==2){ result=5; }elseif(count==3){ result=4; }elseif(count==4){ result=3; }elseif(count==5){ result=2; }elseif(count==6){ result=1; } return result; }

✔ 규칙

맞은 개수
등수
6
1등
5
2등
4
3등
3
4등
2
5등
0~1
6등
👉 기본값을 6으로 두고 조건만 덮어씀

🎯 핵심 포인트 정리

✔ 0은 "맞을 수도 있는 숫자" → 최고값 계산에 사용
✔ Map을 사용해 빠르게 당첨번호 비교
✔ 결과는 [최고순위, 최저순위]

🚀 한 줄 정리

👉 확정된 일치 개수 + 미확인(0)을 최대 활용해서 최고/최저 순위를 계산하는 문제
Share article