로또의 최고 순위와 최저 순위
문제
https://programmers.co.kr/learn/courses/30/lessons/77484
코딩테스트 연습 - 로또의 최고 순위와 최저 순위
로또 6/45(이하 '로또'로 표기)는 1부터 45까지의 숫자 중 6개를 찍어서 맞히는 대표적인 복권입니다. 아래는 로또의 순위를 정하는 방식입니다. 1 순위 당첨 내용 1 6개 번호가 모두 일치 2 5개 번호
programmers.co.kr
코드
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
class Solution {
public int[] solution(int[] lottos, int[] win_nums) {
int[] result = {};
ArrayList<Integer> list = new ArrayList<Integer>();
int max_count = 0;
int min_count = 0;
for (int i = 0; i < lottos.length; i++) {
for (int j = 0; j < win_nums.length; j++) {
if (lottos[i] == win_nums[j]) {
max_count++;
min_count++;
break;
} else if (lottos[i] == 0) {
max_count++;
break;
}
}
}
// System.out.println(min_count);
// System.out.println(max_count);
list.add(min_count);
list.add(max_count);
result = lottoRank(list);
// System.out.println(Arrays.toString(result));
return result;
}
// 로또 최고,최저순위 배열 반환 메서드
public int[] lottoRank(List<Integer> same_counts) {
// rank 배열 반환변수
int[] rank = new int[2];
// rankMap(로또 일치갯수, 순위)
HashMap<Integer, Integer> rankMap = new HashMap<Integer, Integer>();
rankMap.put(0, 6);
rankMap.put(1, 6);
rankMap.put(2, 5);
rankMap.put(3, 4);
rankMap.put(4, 3);
rankMap.put(5, 2);
rankMap.put(6, 1);
int index = 0;
for (int same_count : same_counts) {
for (int key : rankMap.keySet()) {
if (key == same_count) {
rank[index] = rankMap.get(key);
index++;
}
}
}
Arrays.sort(rank);
return rank;
}
}
'프로그래머스 > LEVEL 1' 카테고리의 다른 글
[프로그래머스/LEVEL1] 문자열 내 마음대로 정렬하기 (0) | 2022.01.17 |
---|---|
[프로그래머스/LEVEL1] 모의고사 (0) | 2022.01.17 |
[프로그래머스/LEVEL1] 두 개 뽑아서 더하기 (0) | 2022.01.17 |
[프로그래머스/LEVEL1] 같은 숫자는 싫어 (0) | 2022.01.17 |
[프로그래머스/LEVEL1] 최소직사각형 (0) | 2022.01.17 |