-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path기타 레슨.swift
42 lines (35 loc) · 1004 Bytes
/
기타 레슨.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
import Foundation
let NM = readLine()!.split(separator: " ").map{ Int(String($0))! }
let arr = readLine()!.split(separator: " ").map{ Int(String($0))! }
func solution(N: Int, M: Int, arr: [Int]) -> Int {
var low = 0
var high = N * 10000
while low <= high {
let mid = (low + high)/2
if isPossible(size: mid, limitCount: M, arr: arr) {
high = mid - 1
} else {
low = mid + 1
}
}
return low
}
func isPossible(size: Int, limitCount: Int, arr: [Int]) -> Bool {
var index = 0
var sum = 0
var count = 0
while index < arr.count {
defer { index += 1 }
let value = arr[index]
guard value <= size else { return false }
if sum + value <= size {
sum += value
} else {
sum = value
count += 1
}
}
if sum > 0 { count += 1 }
return count <= limitCount ? true : false
}
print(solution(N: NM[0], M: NM[1], arr: arr))