반응형
728x90
반응형
문제
https://programmers.co.kr/learn/courses/30/lessons/77484
코딩테스트 연습 - 로또의 최고 순위와 최저 순위
로또 6/45(이하 '로또'로 표기)는 1부터 45까지의 숫자 중 6개를 찍어서 맞히는 대표적인 복권입니다. 아래는 로또의 순위를 정하는 방식입니다. 1 순위 당첨 내용 1 6개 번호가 모두 일치 2 5개 번호
programmers.co.kr
풀이코드
package com.algorithm._00_._Current;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* @Date 2022/03/21
* @URL https://programmers.co.kr/learn/courses/30/lessons/77484
*/
public class P77484_로또의_최고순위_최저순위 {
public static void main(String[] args) {
// write your code here
P77484_로또의_최고순위_최저순위 main = new P77484_로또의_최고순위_최저순위();
System.out.println(Arrays.toString(main.solution(new int[]{44, 1, 0, 0, 31, 25}, new int[]{31, 10, 45, 1, 6, 19})));
}
public int[] solution(int[] lottos, int[] win_nums) {
int[] answer = new int[2];
/* key : 맞는 번호 개수, value : 등수 */
Map<Integer, Integer> map = new HashMap<>();
map.put(6, 1);
map.put(5, 2);
map.put(4, 3);
map.put(3, 4);
map.put(2, 5);
map.put(1, 6);
map.put(0, 6);
/* 지워진 0의 개수에 따라 계산된다. */
long zeroCount = Arrays.stream(lottos)
.filter(i -> i == 0)
.count();
/* 당첨된 번호의 개수를 구한다. */
int sameCount = 0;
for (int lotto : lottos) {
for (int win_num : win_nums) {
if (lotto == win_num) {
sameCount++;
}
}
}
/*
최고등수 : 0이 모두 당첨일때의 경우
-> 0의 개수 + 이미 당첨된 번호의 개수
최저등수 : 0이 모두 미당첨일때의 경우
-> 이미 당첨된 번호의 개수
*/
answer[0] = map.get((int) (zeroCount + sameCount));
answer[1] = map.get(sameCount);
return answer;
}
}
다른사람의 풀이
https://programmers.co.kr/learn/courses/30/lessons/77484/solution_groups?language=java
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
package com.algorithm._00_._Current;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.LongStream;
/**
* @Date 2022/03/21
* @URL https://programmers.co.kr/learn/courses/30/lessons/77484
*/
public class P77484_로또의_최고순위_최저순위 {
public static void main(String[] args) {
// write your code here
P77484_로또의_최고순위_최저순위 main = new P77484_로또의_최고순위_최저순위();
System.out.println(Arrays.toString(main.solution2(new int[]{44, 1, 0, 0, 31, 25}, new int[]{31, 10, 45, 1, 6, 19})));
}
/**
* 다른 사람의 풀이
* @param lottos
* @param winNums
* @return
*/
public int[] solution2(int[] lottos, int[] winNums) {
return LongStream.of(
/* 1) 당첨된 번호 개수 + 0의 개수 */
(lottos.length + 1) - Arrays.stream(lottos)
.filter(l -> Arrays.stream(winNums).anyMatch(w -> w == l) || l == 0).count(),
/* 2) 당첨된 번호 개수 */
(lottos.length + 1) - Arrays.stream(lottos)
.filter(l -> Arrays.stream(winNums).anyMatch(w -> w == l)).count()
)
.mapToInt(op -> (int) (op > 6 ? op - 1 : op))
.toArray();
}
}
내가 풀이한 코드와 식은 동일하다. 다만 위 풀이는 스트림을 사용하여 더욱 간결하게 표현되어있다. 내 풀이 또한 zeroCount(0의 개수)를 구하기 위해서 스트림을 사용했지만, 당첨된 번호의 개수를 구하는 부분에서는 중 반복문을 사용하고 있다. 위 풀이를 참고해서 스트림으로 구하는 방법을 이해하자.
반응형
'Algorithm > Problem Solving' 카테고리의 다른 글
[프로그래머스] Level2 42578번: 위장 (JAVA) (0) | 2022.03.27 |
---|---|
[프로그래머스] Level2 42885번: 구명보트 (JAVA) (0) | 2022.03.25 |
[Baekjoon 1238번] 파티 문제 (with 자바) (0) | 2022.03.15 |
[Baekjoon 1439번] 뒤집기 문제 (with 자바) (0) | 2022.02.05 |
[이것이 코딩테스트다] 실전문제39. 화성탐사 (JAVA) (0) | 2022.01.17 |