-
Notifications
You must be signed in to change notification settings - Fork 3
/
마인크래프트.swift
36 lines (31 loc) · 1007 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
import Foundation
let NMB = readLine()!.split(separator: " ").map{ Int(String($0))! }
var matrix: [[Int]] = []
(1...NMB[0]).forEach { _ in
let arr = readLine()!.split(separator: " ").map{ Int(String($0))! }
matrix.append(arr)
}
func time(matrix: [[Int]], B: Int, height target: Int) -> Int? {
var minus: Int = 0
var plus: Int = 0
matrix.forEach { arr in
arr.forEach {
let difference: Int = $0 - target
if difference <= 0 {
minus -= difference
} else {
plus += difference
}
}
}
if plus - minus + B < 0 { return nil }
else { return plus * 2 + minus }
}
func solution(matrix: [[Int]], B: Int) -> [Int] {
let results: [Int?] = (0...256).map{ time(matrix: matrix, B: B, height: $0)}
let minValue = results.compactMap{ $0 }.min()!
return [minValue, Int(results.lastIndex(of: minValue)!)]
}
solution(matrix: matrix, B: NMB[2]).forEach{
print($0, terminator: " ")
}