-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaxSum2.kt
43 lines (30 loc) · 885 Bytes
/
MaxSum2.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
fun main(args: Array<String>) {
val cnt = readLine()!!.toInt();
val matrix: Array<IntArray> = Array(cnt,{
readLine()!!.split(' ').map({ it.toInt() }).toIntArray();
});
println(solve(matrix).toString());
}
fun solve(matrix:Array<IntArray>):Int
{
var result:Int = Int.MIN_VALUE;
for (i in 0..matrix.size-1) {
val temp = IntArray(matrix.size, {0});
for (j in i..matrix.size-1) {
for (k in 0..matrix.size-1)
temp[k] += matrix[k][j];
result = Math.max(result, kadane( temp ));
}
}
return result;
}
fun kadane(list:IntArray):Int
{
var globalMax = Int.MIN_VALUE;
var localMax = 0;
for (i in 0..list.size-1) {
localMax = Math.max(list[i], localMax+list[i]);
globalMax = Math.max(localMax, globalMax);
}
return globalMax;
}