import java.util.HashMap;
import java.util.Map;
class Solution {
public int[] solution(int[] lottos, int[] win_nums) {
int[] answer = new int[2];
//0. init rankMap && isWinLottoMap
Map<Integer, Integer> rankMap = initRankMap();
int countOfZero = getCountOfZero(lottos); //아래 두 코드를 합치려면 어떻게 해야 할까?
Map<Integer, Boolean> isWinLottoMap = initIsWinLottoMap(lottos);
//2. count number of Win
int numOfWin = 0;
for (int win_num : win_nums) {
if (isWinLottoMap.containsKey(win_num)) {
numOfWin++;
}
}
answer[0] = rankMap.get(numOfWin + countOfZero);
answer[1] = rankMap.get(numOfWin);
return answer;
}
private HashMap<Integer, Integer> initRankMap() {
HashMap<Integer, Integer> rankMap = new HashMap<>();
int maxWinCountNum = 6;
rankMap.put(0, maxWinCountNum);
for (int countOfWin = 1; countOfWin <= 6; countOfWin++) {
rankMap.put(countOfWin, maxWinCountNum - countOfWin + 1);
}
return rankMap;
}
private int getCountOfZero(int[] lottos) {
int countOfZero = 0;
for (int lotto : lottos) {
if(lotto == 0) {
countOfZero++;
}
}
return countOfZero;
}
private HashMap<Integer, Boolean> initIsWinLottoMap(int[] lottos) {
HashMap<Integer, Boolean> isWinLottoMap = new HashMap<>();
for (int lotto : lottos) {
if(lotto == 0) {
continue;
}
isWinLottoMap.put(lotto, false);
}
return isWinLottoMap;
}
}
반응형
'ComputerScience > Algorithm' 카테고리의 다른 글
[프로그래머스 Lv.1] 신고 결과 받기 (0) | 2022.03.13 |
---|---|
[프로그래머스 Lv.1] 없는 숫자 더하기 (0) | 2022.03.12 |
[백준] 14500-테트로미노 (0) | 2021.03.13 |
[백준] 3190 - 뱀 (0) | 2021.03.04 |
[백준] 13460 - 구슬탈출2 (0) | 2021.02.25 |