-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChernobylEagle.kt
56 lines (37 loc) · 1.09 KB
/
ChernobylEagle.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
import java.util.Arrays
fun main(args: Array<String>) {
val cache = mutableMapOf<String, Int>();
while(true) {
val values = readLine()!!.split(' ').map({ it.toInt() }).toIntArray();
if (values[0]==0 && values[1]==0)
break;
println(solve(values[1], values[0], cache))
println(cache);
}
}
fun solve(floors:Int, eggs:Int, cache:MutableMap<String,Int>):Int
{
if (eggs == 0) {
return 0;
}
if (eggs == 1 || floors==1) {
return floors;
}
if (floors == 0)
return 1;
val key = floors.toString() + "_" + eggs.toString();
if (cache.containsKey(key)) {
return cache.getOrDefault(key,0);
}
var result = Int.MIN_VALUE;
var prev = Int.MIN_VALUE;
for (i in 1..floors) {
result = Math.max(result, 1+Math.min(solve(i-1, eggs -1, cache),solve(floors-i, eggs, cache)))
if (prev >= result)
break;
prev = result;
}
//println(floors.toString()+" "+eggs.toString()+" "+result );
cache.put(key, result);
return result;
}