나의 풀이
class Solution {
public static int[] solution(int[] lottos, int[] win_nums) {
int[] answer = new int[2];
// 당첨번호와 맞는 번호의 갯수 확인용 count
int count=0;
// 입력한 번호중 0의 갯수 확인용 countZero
int countZero = 0;
// 입력한 번호를 차례대로 추출
for(int i=0; i<lottos.length; i++) {
int compareNum = lottos[i];
// 비교할 숫자가 0이 아닌경우만
if(compareNum!=0) {
// 당첨번호와 비교
for(int k=0; k<win_nums.length; k++) {
// 당첨번호와 추출한 번호가 같을 시 갯수확인용 count 변수에 1씩 더해줌
if(compareNum == win_nums[k]) {
count++;
}
}
}else{
// 0 일때는 countZero 값에 1을 더해줌
countZero ++;
}
}
// 최종적으로 나온 count 의 값은 입력한 번호와 당첨번호의 겹치는 숫자의 갯수임.
// 0의 갯수만큼 더 맞았을 때가 가장 높은 등수 , 덜 맞았을 때가 낮은 등수
// 즉, 높은등수 = count + countZero; 낮은등수 = count;
// 맞춘갯수가 정답배열의 길이보다 많을 수 없음.
int high = (count+countZero>=win_nums.length) ? win_nums.length : count + countZero;
int lower = count;
int highResult = (high == 6) ? 1 : (high == 5) ? 2 : (high == 4) ? 3 : (high == 3) ? 4 : (high == 2) ? 5 : 6;
int lowerResult = (lower == 6) ? 1 : (lower == 5) ? 2 : (lower == 4) ? 3 : (lower == 3) ? 4 : (lower == 2) ? 5 : 6;
answer[0] = highResult;
answer[1] = lowerResult;
return answer;
}
}
좋아요 높은 풀이
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();
}
}
'Algorithm' 카테고리의 다른 글
[Algorithm] 야근지수 (0) | 2022.10.13 |
---|---|
[Algorithm] 평균구하기 (0) | 2022.10.11 |
[Algorithm] 같은 숫자는 싫어 (0) | 2022.08.30 |
[Algorithm] 가장 큰 수 (0) | 2022.08.29 |
[Algorithm] 알고리즘 공부 시작 방법 및 순서 (0) | 2022.08.26 |