-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproblem292.swift
67 lines (59 loc) · 1.53 KB
/
problem292.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import Dispatch
// recursive
class Solution1 {
func canWinNim(_ n: Int) -> Bool {
if (n>3) {
if (canWinNim(n-1) && canWinNim(n-2) && canWinNim(n-3)) {
return false
} else {
return true
}
} else {
return true
}
}
}
//iteration
class Solution2 {
func canWinNim(_ n: Int) -> Bool {
if (n<4) {
return true
} else {
var history = [true,true,true]
var index_i = 2
for _ in 4...n {
index_i = (index_i+1)%3
history[index_i] = !(history[0] && history[1] && history[2])
}
return history[index_i]
}
}
}
//math
class Solution3 {
func canWinNim(_ n : Int) -> Bool {
return (n%4 != 0)
}
}
//time testing function
protocol sol {
func canWinNim(_ n: Int) -> Bool
}
extension Solution1: sol{}
extension Solution2: sol{}
extension Solution3: sol{}
func time_consuming<T: sol>(_ solution_class: T, _ name: String, _ r: Int) {
let time_start = DispatchTime.now().uptimeNanoseconds
for i in 1...r {
_ = solution_class.canWinNim(i)
}
let time_end = DispatchTime.now().uptimeNanoseconds
print(name,(time_end-time_start)/1000000,"ms","for \(r) times")
}
//test time consuming for different methods
let test1 = Solution1()
let test2 = Solution2()
let test3 = Solution3()
time_consuming(test1,"recursive",40)
time_consuming(test2,"iteration",32768)
time_consuming(test3,"math",32768)