https://programmers.co.kr/learn/courses/30/lessons/42862
//테스트코드
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class GymSuitTest {
GymSuit q;
@BeforeEach
void setUp() {
q = new GymSuit();
}
@Test
void test1() {
assertEquals(5, q.solution(5, new int[]{2, 4}, new int[]{1, 3, 5}));
}
@Test
void test2() {
assertEquals(4, q.solution(5, new int[]{2, 4}, new int[]{3}));
}
@Test
void test3() {
assertEquals(2, q.solution(3, new int[]{3}, new int[]{1}));
}
}
//프로덕션코드
import java.util.ArrayList;
import java.util.List;
public class GymSuit {
public int solution(int n, int[] lost, int[] reserve) {
List<Integer> lostList = new ArrayList<>();
List<Integer> reserveList = new ArrayList<>();
int numberOfStudentsWhohaveGymSuit = n;
for (int i = 0; i < lost.length; i++) {
lostList.add(lost[i]);
}
for (int i = 0; i < reserve.length; i++) {
reserveList.add(reserve[i]);
}
for (int i = 0; i < lostList.size(); i++) {
for (int j = 0; j < reserveList.size(); j++) {
if (lostList.get(i) == reserveList.get(j)) {
lostList.remove(i);
reserveList.remove(j);
// 객체로 지우는 것도 있고 인덱스로 지우는 것도 있는데
// 이렇게 Integer 객체일때는...? 숫자 객체라는 것이 없나..?
// 객체 공부 더 해보자.
}
}
}
numberOfStudentsWhohaveGymSuit -= lostList.size();
for (int i = 0; i < reserveList.size(); i++) {
for (int j = 0; j < lostList.size(); j++){
if(reserveList.get(i)-1 == lostList.get(j)){
reserveList.remove(i);
lostList.remove(j);
numberOfStudentsWhohaveGymSuit++;
i--;
break;
}
if(reserveList.get(i)+1 == lostList.get(j)){
reserveList.remove(i);
lostList.remove(j);
numberOfStudentsWhohaveGymSuit++;
i--;
break;
}
}
}
return numberOfStudentsWhohaveGymSuit;
}
}
// 몇개 케이스 실패! 런타임에러도 하나 있고...
// 일단 도난당한 사람이랑 여분 있는 사람이 같을 때 지워주는 과정에서 i-- 추가해야 빠짐없이 확인할 수 있겠네
import java.util.ArrayList;
import java.util.List;
public class GymSuit {
public int solution(int n, int[] lost, int[] reserve) {
List<Integer> lostList = new ArrayList<>();
List<Integer> reserveList = new ArrayList<>();
int numberOfStudentsWhohaveGymSuit = n;
for (int i = 0; i < lost.length; i++) {
lostList.add(lost[i]);
}
for (int i = 0; i < reserve.length; i++) {
reserveList.add(reserve[i]);
}
for (int i = 0; i < lostList.size(); i++) {
for (int j = 0; j < reserveList.size(); j++) {
if (lostList.get(i) == reserveList.get(j)) {
lostList.remove(i);
reserveList.remove(j);
i--;
break;
}
}
}
numberOfStudentsWhohaveGymSuit -= lostList.size();
for (int i = 0; i < reserveList.size(); i++) {
for (int j = 0; j < lostList.size(); j++){
if(reserveList.get(i)-1 == lostList.get(j)){
reserveList.remove(i);
lostList.remove(j);
numberOfStudentsWhohaveGymSuit++;
i--;
break;
}
if(reserveList.get(i)+1 == lostList.get(j)){
reserveList.remove(i);
lostList.remove(j);
numberOfStudentsWhohaveGymSuit++;
i--;
break;
}
}
}
return numberOfStudentsWhohaveGymSuit;
}
}
// 런타임 오류 1개는 해결했는데 아직도 2개 실패!! 혹시 배열이 번호 순서대로 안되어있나..?
// 정렬하고 시작해보자.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class GymSuit {
public int solution(int n, int[] lost, int[] reserve) {
Arrays.sort(lost);
Arrays.sort(reserve);
List<Integer> lostList = new ArrayList<>();
List<Integer> reserveList = new ArrayList<>();
int numberOfStudentsWhohaveGymSuit = n;
for (int i = 0; i < lost.length; i++) {
lostList.add(lost[i]);
}
for (int i = 0; i < reserve.length; i++) {
reserveList.add(reserve[i]);
}
for (int i = 0; i < lostList.size(); i++) {
for (int j = 0; j < reserveList.size(); j++) {
if (lostList.get(i) == reserveList.get(j)) {
lostList.remove(i);
reserveList.remove(j);
i--;
break;
}
}
}
numberOfStudentsWhohaveGymSuit -= lostList.size();
for (int i = 0; i < reserveList.size(); i++) {
for (int j = 0; j < lostList.size(); j++){
if(reserveList.get(i)-1 == lostList.get(j)){
reserveList.remove(i);
lostList.remove(j);
numberOfStudentsWhohaveGymSuit++;
i--;
break;
}
if(reserveList.get(i)+1 == lostList.get(j)){
reserveList.remove(i);
lostList.remove(j);
numberOfStudentsWhohaveGymSuit++;
i--;
break;
}
}
}
return numberOfStudentsWhohaveGymSuit;
}
}
// 성공..! 진짜 너무하다! 테스트 케이스는 다 정렬하고 줘놓고선...
// 지레 짐작한 내 잘못이지.. 앞으로 더 꼼꼼하게 코딩해야겠다
// 리팩토링하고 마무리하자.
Arrays.sort(lost);
Arrays.sort(reserve);
List<Integer> lostList = new ArrayList<>();
List<Integer> reserveList = new ArrayList<>();
int numberOfStudentsWhohaveGymSuit = n;
setLostList(lost, lostList);
setReserveList(reserve, reserveList);
checkIfLostListandReserveListHaveSameStudent(lostList, reserveList);
numberOfStudentsWhohaveGymSuit -= lostList.size();
return getNumberOfStudentsWhohaveGymSuit(lostList, reserveList, numberOfStudentsWhohaveGymSuit);
}
private int getNumberOfStudentsWhohaveGymSuit(List<Integer> lostList, List<Integer> reserveList, int numberOfStudentsWhohaveGymSuit) {
for (int i = 0; i < reserveList.size(); i++) {
for (int j = 0; j < lostList.size(); j++){
if(reserveList.get(i)-1 == lostList.get(j)){
reserveList.remove(i);
lostList.remove(j);
numberOfStudentsWhohaveGymSuit++;
i--;
break;
}
if(reserveList.get(i)+1 == lostList.get(j)){
reserveList.remove(i);
lostList.remove(j);
numberOfStudentsWhohaveGymSuit++;
i--;
break;
}
}
}
return numberOfStudentsWhohaveGymSuit;
}
private void checkIfLostListandReserveListHaveSameStudent(List<Integer> lostList, List<Integer> reserveList) {
for (int i = 0; i < lostList.size(); i++) {
for (int j = 0; j < reserveList.size(); j++) {
if (lostList.get(i) == reserveList.get(j)) {
lostList.remove(i);
reserveList.remove(j);
i--;
break;
}
}
}
}
private void setReserveList(int[] reserve, List<Integer> reserveList) {
for (int i = 0; i < reserve.length; i++) {
reserveList.add(reserve[i]);
}
}
private void setLostList(int[] lost, List<Integer> lostList) {
for (int i = 0; i < lost.length; i++) {
lostList.add(lost[i]);
}
}
}
// 성공!
'코딩테스트연습 > 프로그래머스' 카테고리의 다른 글
[프로그래머스 - JAVA] 3진법 뒤집기 (0) | 2021.12.01 |
---|---|
[프로그래머스 - JAVA] 폰켓몬 (0) | 2021.11.29 |
[프로그래머스 - JAVA] 완주하지 못한 선수 (0) | 2021.11.27 |
[프로그래머스 - JAVA] K번째수 (0) | 2021.11.26 |
[프로그래머스 - JAVA] 로또의 최고 순위와 최저 순위 (0) | 2021.11.24 |