-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.swift
46 lines (41 loc) · 938 Bytes
/
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
43
44
45
46
//
// main.swift
// SwiftAlgorithm
//
// Created by Lee Myeonghwan on 2023/01/24.
//
import Foundation
func solution(_ people:[Int], _ limit:Int) -> Int {
var answer: Int = 0
var people = people.sorted(by: >)
while people.count > 1 {
if people[0] + people[people.endIndex - 1] <= limit {
people.removeFirst()
people.removeLast()
} else {
people.removeFirst()
}
answer += 1
}
if !people.isEmpty {
answer += 1
}
return answer
}
/* python
from collections import deque
def solution(people, limit):
answer = 0
people.sort(reverse=True)
people = deque(people)
while len(people) > 1:
if people[0] + people[-1] <= limit:
people.popleft()
people.pop()
else:
people.popleft()
answer += 1
if people:
answer += 1
return answer
*/