학생 배열에 학생의 번호 순서대로 체육복을 잃어버린 경우 0, 여분이 있는 경우 2를 입력하고
여분이 있는 친구가 잃어버린 경우 1을 입력해서 배열을 만들어줌
그 후에 배열을 for문을 돌려서 체육복이 없는 경우엔 앞뒤로 체육복이 있는 친구꺼를 빌려서 +1을 해주고 빌려준 친구는 -1를 함
최종 배열에서 체육복이 없어 0인 경우만 빼고 출력해줌
func solution(_ n:Int, _ lost:[Int], _ reserve:[Int]) -> Int {
var students: [Int] = Array(repeating: 1, count: n)
//학생 배열에 여분이 있는 학생들은 2를 입력함
reserve.forEach { students[$0 - 1] = 2 }
//학생 배열에 체육복을 잃어버린 학생은 0을 입력함, 단 2벌을 가지고 있던 학생은 1을 입력
lost.forEach {
if students[$0 - 1] == 2 {
students[$0 - 1] = 1
} else {
students[$0 - 1] = 0
}
}
//학생 배열에서 학생이 가지고 있는 체육복 수가 0인 경우 앞 뒤로 계산해서 여분을 가지고 있는 친구가 있을 경우 +1 해주고, 빌려준 친구는 -1 해줌
for (index, student) in students.enumerated() {
if index == 0 {
if student == 0 {
if students[index + 1] == 2 {
students[index] += 1
students[index + 1] -= 1
}
}
} else if index == n - 1 {
if student == 0 {
if students[index - 1] == 2 {
students[index] += 1
students[index + 1] -= 1
}
}
} else {
if student == 0 {
if students[index - 1] == 2 {
students[index] += 1
students[index - 1] -= 1
} else if students[index + 1] == 2 {
students[index] += 1
students[index + 1] -= 1
}
}
}
}
//학생배열에서 체육복이 0인 경우 빼고 출력
return students.filter { $0 != 0 }.count
}
'알고리즘 > 프로그래머스' 카테고리의 다른 글
프로그래머스 - 기능개발 (0) | 2020.12.30 |
---|---|
프로그래머스 - 조이스틱 (0) | 2020.12.30 |
프로그래머스 = 베스트앨범 (0) | 2020.12.27 |
프로그래머스 - 위장 (0) | 2020.12.26 |
프로그래머스 - 카펫 (0) | 2020.12.24 |