-
Notifications
You must be signed in to change notification settings - Fork 36
/
Bender-Episode-3.kt
108 lines (92 loc) · 2.5 KB
/
Bender-Episode-3.kt
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// https://www.codingame.com/training/hard/bender-episode-3
import java.util.Scanner
// complexity names and functions
val complexities = arrayListOf(
Constant(),
Logarithmic(),
Linear(),
LogLinear(),
Quadratic(),
LogQuadratic(),
Cubic(),
Exponential()
)
fun main(args : Array<String>) {
// read standard input
val input = Scanner(System.`in`)
val nbPoints = input.nextInt()
val points = ArrayList<Pair<Int, Int>>(nbPoints)
for (i in 0 until nbPoints) {
val nbItems = input.nextInt()
val time = input.nextInt()
points.add(Pair(nbItems, time))
}
// compute the most probable computational complexity
var bestFit = ""
var minNormVariance = Double.POSITIVE_INFINITY
for (complexity in complexities) {
val ratios = points.map { it.second / complexity.function(it.first.toDouble()) }
// calculate the normalized variance
val meanRatios = ratios.sum() / ratios.size
val variances = ratios.map { Math.pow(it - meanRatios, 2.0) }
val normVariance = variances.sum() / Math.pow(meanRatios, 2.0)
if (normVariance < minNormVariance) {
minNormVariance = normVariance
bestFit = complexity.name()
}
}
println("O($bestFit)")
}
interface Complexity {
fun name(): String
fun function(n: Double): Double
}
class Constant : Complexity {
override fun name(): String { return "1" }
override fun function(n: Double): Double {
return 1.0
}
}
class Logarithmic : Complexity {
override fun name(): String { return "log n" }
override fun function(n: Double): Double {
return Math.log(n)
}
}
class Linear : Complexity {
override fun name(): String { return "n" }
override fun function(n: Double): Double {
return n
}
}
class LogLinear : Complexity {
override fun name(): String { return "n log n" }
override fun function(n: Double): Double {
return n * Math.log(n)
}
}
class Quadratic : Complexity {
override fun name(): String { return "n^2" }
override fun function(n: Double): Double {
return n * n
}
}
class LogQuadratic : Complexity {
override fun name(): String { return "n^2 log n" }
override fun function(n: Double): Double {
return n * n * Math.log(n)
}
}
// for validator test #7
class Cubic : Complexity {
override fun name(): String { return "n^3" }
override fun function(n: Double): Double {
return Math.pow(n, 2.1)
}
}
class Exponential : Complexity {
override fun name(): String { return "2^n" }
override fun function(n: Double): Double {
return Math.pow(2.0, n)
}
}