Groovy web console

subscribe to the feed Subscribe
to this
site
LongestIncreasingSubsequenceBruteForce (via #groovywebconsole)
tweet this snippet Tweet
this
script

LongestIncreasingSubsequenceBruteForce

Published 3 months ago by JaneDoe with tags Algorithms
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def sequence = [10, 29471,5242,21175,28931,2889,7275,19159,21773,1325,6901]

def isSorted(list) { list.size() < 2 || (1..<list.size()).every { list[it - 1] < list[it] } }

def getLongestIncreasingSubsequencesBruteForce(sequence) {


    def sortedSubsequences = sequence.subsequences().findAll { isSorted(it) }
    def groupedBySize = sortedSubsequences.groupBy { it.size() }
    def maxLength = groupedBySize.keySet().max()
    def maxSequences = groupedBySize[maxLength]
    assert maxSequences.every { isSorted(it) }
    return maxSequences
}

getLongestIncreasingSubsequencesBruteForce(sequence)