🔺 문제
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
🔺 코드
import java.util.*;
class Solution {
// 도난 여벌
public int solution(int n, int[] lost, int[] reserve) {
int answer = 0;
int[] arr = new int[n];
Arrays.fill(arr, 1); // 배열 1로 채우기
boolean[] std = new boolean[n];
Arrays.fill(std, true); // 배열 true로 채우기
// lost, reserve 반영하기
for(int i=0 ; i < lost.length ; i++) {
arr[lost[i] - 1] = 0;
std[lost[i] - 1] = false;
}
for(int i=0; i < reserve.length ; i++) {
arr[reserve[i] - 1] = 2;
std[reserve[i] - 1] = true;
}
// 여벌도 있지만 도난 당한 경우
for(int i=0 ; i < lost.length ; i++) {
for(int j=0 ; j < reserve.length ; j++) {
if(lost[i] == reserve[j]) {
arr[reserve[j] - 1] = 1;
}
}
}
for(int i=0 ; i < std.length ; i++) {
// 체육복이 없을 때
if(!std[i]) {
// 가운데 수의 학생일 때
if(i != 0 && i != (n-1)) {
if(arr[i-1] == 2) {
arr[i-1]--;
std[i] = true;
} else if(arr[i+1] == 2) {
arr[i+1]--;
std[i] = true;
}
}
// 첫 번째 학생일 때
else if(i==0) {
if(arr[i+1] == 2) {
arr[i+1]--;
std[i] = true;
}
}
// 마지막 번호 학생일 때
else if(i==(n-1)) {
if(arr[i-1] == 2) {
arr[i-1]--;
std[i] = true;
}
}
}
}
// 체육복 있는 (=True인) 학생 수 계산
for(boolean b : std) {
if(b) answer++;
}
return answer;
}
}
✅ 해결 아이디어
- int[] arr : 갖고 있는 체육복 개수 (초기, 다 1로 설정) (도난 당한 사람 : 0 / 도난 당했지만 여벌 있음 : 1 / 빌려줄 수 있음 : 2)
- boolean[] std : 체육복 갖고 있는지 여부 (초기, 다 true로 설정)
와 1시간 고민해서 결국 풀긴 풀었다!!! 아 뿌듯해
오늘 제대로 푼 문제 하나도 없어서 좀 속상했는데....ㅠㅠ
비록 70줄이지만.... 그래도 포기하지 않고 풀었다는 것이 중요한 것 아니겠나..!
다른 분 코드 보고 더 효율적인 코드 찾아야지...ㅎㅎ
🔺 다른 풀이들
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이1)
class Solution {
public int solution(int n, int[] lost, int[] reserve) {
int[] people = new int[n];
int answer = n;
for (int l : lost)
people[l-1]--;
for (int r : reserve)
people[r-1]++;
for (int i = 0; i < people.length; i++) {
if(people[i] == -1) {
if(i-1>=0 && people[i-1] == 1) {
people[i]++;
people[i-1]--;
}else if(i+1< people.length && people[i+1] == 1) {
people[i]++;
people[i+1]--;
}else
answer--;
}
}
return answer;
}
}
배열을 하나만 만들어서
도난 당한 사람 : -1 / 도난 당했지만 여벌 있음 : 0 / 빌려줄 수 있음 : 1
의 값을 갖도록 세팅해
2중 for문을 삭제했다.
풀이2)
import java.util.HashSet;
class Solution {
public int solution(int n, int[] lost, int[] reserve) {
int answer = n - lost.length;
HashSet<Integer> ko = new HashSet<Integer>();
for(int k : reserve) {
ko.add(k);
}
for(int i =0;i<lost.length;i++) {
// 여분이 있지만 잃어버린 경우
if(ko.contains(lost[i])) {
answer++;
ko.remove(lost[i]);
lost[i]=-1;
}
}
for(int i =0;i<lost.length;i++) {
if(ko.contains(lost[i]-1)) {
answer++;
ko.remove(lost[i]-1);
} else if(ko.contains(lost[i]+1)) {
answer++;
ko.remove(lost[i]+1);
}
}
return answer;
}
}
중복이니까 HashSet을 이용했다고 한다.
(참고)
✔ Arrays.fill() 사용법
- boolean 배열은 default 값으로 false를 갖는다!
[JAVA] Arrays.fill() 사용 방법(배열의 값 일괄초기화)
안녕하세요 Arrays.fill() 이란 메서드는 익숙하지 않을 메서드입니다 최근에 알고리즘을 공부하면서 저도 처음 알게된 메서드입니다 이번 포스팅에서는 Arrays.fill()의 사용 방법에 대해서 알아보겠
crazykim2.tistory.com
'코테 > 프로그래머스' 카테고리의 다른 글
[프로그래머스/Lv. 1] [1차] 비밀지도 (0) | 2023.03.24 |
---|---|
[프로그래머스/Lv. 1] [1차] 다트 게임 (0) | 2023.03.20 |
[프로그래머스/Lv. 1] 가장 가까운 같은 글자 (0) | 2023.03.15 |
[프로그래머스/Lv. 1] 푸드 파이트 대회 (0) | 2023.03.15 |
[프로그래머스/Lv. 1] 콜라 문제 (0) | 2023.03.14 |