-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.swift
42 lines (35 loc) · 1.08 KB
/
main.swift
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
31
32
33
34
35
36
37
38
39
40
41
42
//
// main.swift
// 1단계/체육복
//
// Created by Yongwoo Marco on 2022/05/08.
//
// https://programmers.co.kr/learn/courses/30/lessons/42862
import Foundation
func solution(_ n:Int, _ lost:[Int], _ reserve:[Int]) -> Int {
var mutableReserve = reserve
let exceptReserveLosts = lost.filter {
if let index = mutableReserve.firstIndex(of: $0) {
mutableReserve.remove(at: index)
return false
}
return true
}.sorted(by: <)
print(exceptReserveLosts)
print(mutableReserve)
let finalLosts = exceptReserveLosts.filter {
if let index = mutableReserve.firstIndex(of: $0 - 1) {
mutableReserve.remove(at: index)
} else if let index = mutableReserve.firstIndex(of: $0 + 1) {
mutableReserve.remove(at: index)
} else {
return true
}
return false
}
return n - finalLosts.count
}
print(solution(5, [2, 4], [1, 3, 5])) // 5
print(solution(5, [2, 3], [2, 5])) // 5
print(solution(5, [2, 4], [3])) // 4
print(solution(3, [3], [1])) // 2