728x90
문제 바로가기: https://school.programmers.co.kr/learn/courses/30/lessons/77484
나의 풀이
import java.util.*;
// GPT의 도움을 받아 품
class Solution {
public int[] solution(int[] lottos, int[] win_nums) {
// Remove zeros from lottos
int zero = 0;
Set<Integer> notZeroSet = new HashSet<>();
for (int num : lottos) {
if (num != 0) {
notZeroSet.add(num);
} else {
zero++;
}
}
// Count the matches
int matches = 0;
for (int num : win_nums) {
if (notZeroSet.contains(num)) {
matches++;
}
}
// Calculate ranks
int bestRank = (matches == 0) ? 6 : 7 - (matches + zero);
int worstRank = (matches == 0) ? 6 : 7 - matches;
if (zero == 6){
bestRank = 1;
}
return new int[]{bestRank, worstRank};
}
}
다른 사람의 풀이
import java.util.HashMap;
import java.util.Map;
class Solution {
public int[] solution(int[] lottos, int[] win_nums) {
Map<Integer, Boolean> map = new HashMap<Integer, Boolean>();
int zeroCount = 0;
for(int lotto : lottos) {
if(lotto == 0) {
zeroCount++;
continue;
}
map.put(lotto, true);
}
int sameCount = 0;
for(int winNum : win_nums) {
if(map.containsKey(winNum)) sameCount++;
}
int maxRank = 7 - (sameCount + zeroCount);
int minRank = 7 - sameCount;
if(maxRank > 6) maxRank = 6;
if(minRank > 6) minRank = 6;
return new int[] {maxRank, minRank};
}
}
소감
너무 어렵다
'verdantjuly > 코딩테스트' 카테고리의 다른 글
프로그래머스 코딩테스트 연습 : 숫자 짝꿍 (20240315, java) (0) | 2024.03.15 |
---|---|
프로그래머스 코딩테스트 연습 : 옹알이(2) (20240315, java) (0) | 2024.03.15 |
프로그래머스 코딩테스트 연습 : 3진법 뒤집기 (20240312, java) (0) | 2024.03.12 |
프로그래머스 코딩테스트 연습 : 최대공약수와 최소공배수 (20240312, java) (0) | 2024.03.12 |
프로그래머스 코딩테스트 연습 : 직사각형 별찍기(20240312, java) (0) | 2024.03.12 |