Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions dana/programs/lis.dana
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
(*
Finds the length of the Longest Increasing Subsequence.
Dynamic Programming solution in O(N^2)
*)

def main

def lis is int: n as int, arr as int[]

var dp is int [n]
var i j maxLen is int

if n <= 0:
return: 0

(* Initialize dp array *)
i := 0
loop:
if i >= n : break
dp[i] := 1
i := i + 1

(* Begin computing values *)
i := 1
loop:
if i >= n: break
j := 0
loop:
if j >= i: break
if arr[j] < arr[i] and dp[j] + 1 > dp[i]:
dp[i] := dp[j] + 1
j := j + 1
i := i + 1

maxLen := dp[0]
i := 1
loop:
if i >= n: break
if dp[i] > maxLen:
maxLen := dp[i]
i := i + 1

return: maxLen

var n is int
n := readInteger()

var arr is int [n]

(* Populate array *)
var i is int
i := 0
loop:
if i >= n: break
arr[i] := readInteger()
i := i + 1

var len is int
len := lis: n, arr

writeString: "longest inreasing subsequence has length "
writeInteger: len
writeString: "\n"
2 changes: 2 additions & 0 deletions dana/programs/lis.input
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
8
10 9 2 5 3 7 101 18
1 change: 1 addition & 0 deletions dana/programs/lis.output
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
longest inreasing subsequence has length 4