-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeet401.swift
More file actions
26 lines (22 loc) · 938 Bytes
/
Leet401.swift
File metadata and controls
26 lines (22 loc) · 938 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
class Leet401 {
typealias Time = (num: Int, type: String)
let target: [Time] = [(1,"H"),(2,"H"),(4,"H"),(8,"H"),(1,"M"),(2,"M"),(4,"M"),(8,"M"),(16,"M"),(32,"M")]
var answer: [String] = []
func readBinaryWatch(_ turnedOn: Int) -> [String] {
combination(target, turnedOn, 0, [])
return answer
}
func combination(_ target: [Time], _ tartgetNum: Int, _ index: Int, _ result: [Time]) {
if result.count == tartgetNum {
let hour: Int = result.filter { $0.type == "H" }.map { $0.num }.reduce(0,+)
let minute: Int = result.filter { $0.type == "M" }.map { $0.num }.reduce(0,+)
if hour < 12 && minute <= 59 {
answer.append(String(format: "%d:%02d", hour, minute))
}
return
}
for i in index..<target.count {
combination(target, tartgetNum, i+1, result + [target[i]])
}
}
}