질문
프로그램 제작자
코드 중심 개발자를 고용하십시오. 스택 기반 위치 일치. 프로그래머를 위한 개발자별 프로필에 가입하고 기술 호환성이 좋은 회사와 연결하십시오.
프로그래머.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;
}
}
솔루션
– 정수() : 보유하고 있는 운동복 개수(이니셜, 모두 1로 설정) (도난: 0 / 훔쳤지만 추가: 1 / 대여 가능: 2)
– 부울() 표준 : 운동복 유무(이니셜, 모두 true로 설정)
1시간 고민 끝에 드디어 해결!
!
!
오 정말 자랑스러워
오늘은 문제를 하나도 풀지 못해서 좀 속상하네요…
70줄인데… 포기하지 않고 푸는게 중요하지 않나…!
다른 분들의 코드를 보고 좀 더 효율적인걸 찾아봐야겠네요…

기타 솔루션
프로그램 제작자
코드 중심 개발자를 고용하십시오. 스택 기반 위치 일치. 프로그래머를 위한 개발자별 프로필에 가입하고 기술 호환성이 좋은 회사와 연결하십시오.
프로그래머.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
값을 다음으로 설정
이중 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() 사용 방법
– 부울 배열의 기본값은 false입니다!
(JAVA) Arrays.fill() 사용법(배열 값 일괄 초기화)
안녕하세요, Arrays.fill() 메서드가 생소하실 수도 있는데요, 최근에 알고리즘을 배울 때 처음으로 배운 방법인데요, 이번 글에서는 Arrays.fill() 사용법에 대해 알아보도록 하겠습니다.
crazykim2.