프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
문제풀이
1. 0의 개수를 카운팅하고 , lotoos[] 와 win_nums[] 의 원소중 중복숫자를 카운팅한다.
2. 중복개수 + 0의 개수는 최고 등수 , 중복개수는 최저 등수
3. 중복을 활용하는 문제로 HashSet을 사용해도 무방 ( 사용하지 않아도 됨 )
나의풀이 ( 코드 )
일반 풀이
class Solution {
public int[] solution(int[] lottos, int[] win_nums) {
int[] answer = new int[2];
int[] grade = {6, 6, 5, 4, 3, 2, 1};
int count = 0;
int zeroCnt = 0;
for (int i = 0; i < lottos.length; i++) {
if (lottos[i] == 0) {
zeroCnt++;
continue;
}
for (int j = 0; j < win_nums.length; j++) {
if (lottos[i] == win_nums[j]) {
count++;
break;
}
}
}
answer[0] = grade[count + zeroCnt];
answer[1] = grade[count];
return answer;
}
}
HashSet을 사용한 풀이
import java.util.*;
class Solution {
public int[] solution(int[] lottos, int[] win_nums) {
int[] answer = new int[2];
int[] grade = {6, 6, 5, 4, 3, 2, 1};
HashSet<Integer> hs = new HashSet<>();
int zeroCnt = 0;
for (int i = 0; i < lottos.length; i++) {
if (lottos[i] == 0) {
zeroCnt++;
hs.add(win_nums[i]);
continue;
}
hs.add(lottos[i]);
hs.add(win_nums[i]);
}
answer[0] = grade[12 - hs.size()];
answer[1] = grade[12 - hs.size() - zeroCnt];
return answer;
}
}'알고리즘 > 프로그래머스' 카테고리의 다른 글
| [프로그래머스/JAVA 자바] 체육복 (2) | 2024.04.17 |
|---|---|
| [프로그래머스/JAVA 자바] 완주하지 못한 선수 (0) | 2024.04.17 |
| [프로그래머스/JAVA 자바] [1차] 다트 게임 (0) | 2024.04.16 |
| [프로그래머스/JAVA 자바] 옹알이 (2) (0) | 2024.04.15 |
| [프로그래머스/JAVA 자바] 옹알이 (1) (0) | 2024.04.15 |