-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path체육복.swift
More file actions
30 lines (27 loc) · 813 Bytes
/
체육복.swift
File metadata and controls
30 lines (27 loc) · 813 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//
// 체육복.swift
//
//
// Created by chihoooon on 2022/01/09.
//
import Foundation
func solution(_ n:Int, _ lost:[Int], _ reserve:[Int]) -> Int {
var reserveSet = Set(reserve)
var lostSet = Set(lost)
var kindPeople = Array(reserveSet.subtracting(lostSet)).sorted()
let lostPeople = Array(lostSet.subtracting(reserveSet)).sorted()
var answer = n - lostPeople.count
lostPeople.forEach {
if kindPeople.contains($0 - 1) {
let idx = kindPeople.firstIndex(of: $0 - 1)!
kindPeople.remove(at: idx)
answer += 1
}
else if kindPeople.contains($0 + 1) {
let idx = kindPeople.firstIndex(of: $0 + 1)!
kindPeople.remove(at: idx)
answer += 1
}
}
return answer
}