Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions 200203/ksundong/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* 프로그래머스 - 체육복(https://programmers.co.kr/learn/courses/30/lessons/42862)
* 1. 일단 Test Case를 만들었다.
* 2. 일단 배열의 length값을 빼고 더해보았다. (test 1, 3 실패)
* 3. 만들어 둔 Test Case를 모두 만족하는 코드를 작성하였다.
* 4. 예외 상황이 있어서(조건을 제대로 안읽어서) 코드를 수정했다. (먼저 같은 경우를 비교)
*/
public class Solution {
// 전체 학생의 수 n, 도난당한 학생의 배열 lost, 여벌의 체육복을 가진 학생의 배열 reserve
public int solution(int n, int[] lost, int[] reserve) {
n -= lost.length;
for (int i = 0; i < lost.length; i++) {
for (int j = 0; j < reserve.length; j++) {
if (lost[i] == reserve[j]) {
n++;
lost[i] = 32;
reserve[j] = -1;
break;
}
}
}
for (int i = 0; i < lost.length; i++) {
for (int j = 0; j < reserve.length; j++) {
if (lost[i] == reserve[j] + 1 || lost[i] == reserve[j] - 1) {
n++;
lost[i] = 32;
reserve[j] = -1;
break;
}
}
}
return n;
}
}
63 changes: 63 additions & 0 deletions 200203/ksundong/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class SolutionTest {
Solution solution;

@BeforeEach
void setUp() {
solution = new Solution();
}

@Test
void testSolution1() {
int n = 5;
int[] lost = {2, 4};
int[] reserve = {1, 3, 5};
int expected = 5;
int actual = solution.solution(n, lost, reserve);
assertEquals(expected, actual, "예상 출력과 다릅니다.");
}

@Test
void testSolution2() {
int n = 5;
int[] lost = {2, 4};
int[] reserve = {3};
int expected = 4;
int actual = solution.solution(n, lost, reserve);
assertEquals(expected, actual, "예상 출력과 다릅니다.");
}

@Test
void testSolution3() {
int n = 3;
int[] lost = {3};
int[] reserve = {1};
int expected = 2;
int actual = solution.solution(n, lost, reserve);
assertEquals(expected, actual, "예상 출력과 다릅니다.");
}

@Test
void testSolution4() {
int n = 5;
int[] lost = {4, 5};
int[] reserve = {4};
int expected = 4;
int actual = solution.solution(n, lost, reserve);
assertEquals(expected, actual, "예상 출력과 다릅니다.");
}

@Test
void testSolution5() {
int n = 10;
int[] lost = {3, 9, 10};
int[] reserve = {3, 8, 9};
int expected = 9;
int actual = solution.solution(n, lost, reserve);
assertEquals(expected, actual, "예상 출력과 다릅니다.");
}
}