-
Notifications
You must be signed in to change notification settings - Fork 9
/
LongestIncreasingSubsequence.kt
52 lines (44 loc) · 1.78 KB
/
LongestIncreasingSubsequence.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
fun longestIncreasingSubsequence(list: List<Int>) : List<Int> {
val longestSubsequenceCountAtIndex = MutableList(list.size, {1})
val subsequenceLink = MutableList(list.size, {-1})
list.forEachIndexed({index, currentElement ->
var maxSubsequenceLength = -1
var predecessor = -1
for (subIndex in 0 until index) {
var subElement = list[subIndex]
if (subElement < currentElement
&& longestSubsequenceCountAtIndex[subIndex] + 1 > maxSubsequenceLength) {
maxSubsequenceLength = longestSubsequenceCountAtIndex[subIndex] + 1
predecessor = subIndex
longestSubsequenceCountAtIndex[index] = maxSubsequenceLength
}
}
if (maxSubsequenceLength != -1) {
subsequenceLink[index] = predecessor
}
})
return buildSequenceFromLink(subsequenceLink, list, getBiggestSubsequenceEndPosition(longestSubsequenceCountAtIndex))
}
fun getBiggestSubsequenceEndPosition(longestSubsequenceCountAtIndex: List<Int>) : Int {
var max = Integer.MIN_VALUE
var maxPosition = -1
longestSubsequenceCountAtIndex.forEachIndexed { index, element ->
if (element > max) {
max = element
maxPosition = index
}
}
return maxPosition
}
fun buildSequenceFromLink(link: List<Int>, sequence: List<Int>, start: Int) : List<Int> {
val increasingSequence = mutableListOf<Int>()
var currentPosition = start
while (currentPosition != -1) {
increasingSequence.add(sequence[currentPosition])
currentPosition = link[currentPosition]
}
return increasingSequence
}
fun main(args: Array<String>) {
println(longestIncreasingSubsequence(listOf(0,-5, 7, 200, -2, 1, 0, 3, 2, 5, 0, 7, 9, 10, 300)))
}