250x250
Notice
Recent Posts
Recent Comments
Link
반응형
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- Properties
- Union-find
- union_find
- sql
- html
- Calendar
- 스프링부트
- date
- alter
- 힙덤프
- Java
- GC로그수집
- 리소스모니터링
- 스택
- map
- priority_queue
- dfs
- string
- 큐
- deque
- set
- javascript
- scanner
- CSS
- JPA
- BFS
- math
- NIO
- List
- spring boot
Archives
- Today
- Total
매일 조금씩
프로그래머스 level1 : 로또의 최고 순위와 최저 순위 - JAVA 본문
728x90
반응형
코딩테스트 연습 - 로또의 최고 순위와 최저 순위 | 프로그래머스 (programmers.co.kr)
코딩테스트 연습 - 로또의 최고 순위와 최저 순위
로또 6/45(이하 '로또'로 표기)는 1부터 45까지의 숫자 중 6개를 찍어서 맞히는 대표적인 복권입니다. 아래는 로또의 순위를 정하는 방식입니다. 1 순위 당첨 내용 1 6개 번호가 모두 일치 2 5개 번호
programmers.co.kr
1. IntStream
import java.util.stream.IntStream;
class Solution {
public static int rank(int num) {
if (num >= 2)
return 7 - num;
return 6;
}
public int[] solution(int[] lottos, int[] win_nums) {
int zeroCount = 0;
int correctCount = 0;
for (int lotto : lottos) {
if (lotto == 0) {
zeroCount += 1;
} else {
if (IntStream.of(win_nums).anyMatch(x -> x == lotto))
correctCount += 1;
}
}
int[] answer = {rank(zeroCount + correctCount), rank(correctCount)};
return answer;
}
}
2. LongStream, 람다 표현식
import java.util.Arrays;
import java.util.stream.LongStream;
class Solution {
public int[] solution(int[] lottos, int[] winNums) {
return LongStream.of(
(lottos.length + 1) - Arrays.stream(lottos).filter(l -> Arrays.stream(winNums).anyMatch(w -> w == l) || l == 0).count(),
(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();
}
}
728x90
반응형
'알고리즘' 카테고리의 다른 글
프로그래머스 level1 : 달리기 경주 - Java (0) | 2023.06.02 |
---|---|
프로그래머스 level1 : 대충 만든 자판 - JAVA (0) | 2023.06.01 |
[C++] 백준 2798번 : 블랙잭 (0) | 2021.02.04 |
백준 2630번: 색종이 만들기 (0) | 2020.04.17 |
백준 1655번: 가운데를 말해요 (0) | 2020.04.10 |