Skip to content

Commit b598c1e

Browse files
committed
Add 220418 체육복 1개의 풀이 추가
1 parent 22af6f2 commit b598c1e

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

level-1/체육복.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,29 @@ function solution(n, lost, reserve) {
6060
}
6161
// 체육복 1개 이상을 가진 학생들의 수 반환
6262
return students.filter((v => v >= 1)).length;
63-
}
63+
}
64+
65+
//정답 3 - jaewon1676
66+
function solution(n, lost, reserve) {
67+
let answer = Array(n).fill(1) // n만큼의 배열을 만들어서 1을 만들어줍니다
68+
let cnt = 0;
69+
for(let i = 0; i < reserve.length; i++){ // reserve 를 순회하며 체육복
70+
answer[reserve[i]-1] += 1
71+
}
72+
for(let i = 0; i < lost.length; i++){ // lost 를 순회하며 체육복 수를 -1
73+
answer[lost[i]-1] -= 1
74+
}
75+
for(let i = 0; i < n; i++){ // n을 순회하며 앞사람과 뒷사람의 체육복 수를 비교한다.
76+
if (answer[i] == 2 && answer[i+1] == 0 || answer[i+1] == 2 && answer[i] == 0){
77+
answer[i] = 1
78+
answer[i+1] = 1
79+
}
80+
}
81+
for(let i = 0; i < answer.length; i++){
82+
(answer[i] >= 1 ? cnt += 1 : null)
83+
}
84+
return cnt
85+
}
86+
//그리디
87+
// lost 배열과 reserve 배열을 순회하여 체육복을 추가, 제거 해줍니다.
88+
// 그 후에 최종적으로 i부터 n까지 for문을 순회하며 i번쨰 학생과 i+1번째의 학생이 가진 체육복 수를 비교하여 빌려 줄 수 있는지, 빌려줄 수 없는지 확인 합니다.

0 commit comments

Comments
 (0)