From 03a3edc98ddbb9db069d4b8a3ac706ed10da4466 Mon Sep 17 00:00:00 2001 From: Ayushi Arora Date: Mon, 16 Mar 2020 03:40:34 +0530 Subject: [PATCH 1/4] Longest Increasing Subsequence code in Go --- Longest_Increasing_Subsequence/LIS.go | 51 +++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Longest_Increasing_Subsequence/LIS.go diff --git a/Longest_Increasing_Subsequence/LIS.go b/Longest_Increasing_Subsequence/LIS.go new file mode 100644 index 0000000000..c8f1b18de2 --- /dev/null +++ b/Longest_Increasing_Subsequence/LIS.go @@ -0,0 +1,51 @@ +package main + +import "fmt" + +func Max(lis []int) (max int) { + max = lis[0] + for i := 1; i < len(lis); i++ { + if lis[i] > max { + max = lis[i] + } + } + return +} + +func LIS(arr []int) { + length := len(arr) + + if length == 0 { + return + } + + lis_arr := make([]int, length) + lis_arr[0] = 1 + + for i := 1; i < length; i++ { + lis_arr[i] = 1 + + for j := 0; j < i; j++ { + if ( arr[i] > arr[j] && lis_arr[i] < lis_arr[j] + 1) { + lis_arr[i] = lis_arr[j] + 1 + } + } + } + + fmt.Printf("Length of the Longest Increasing Subsequence : %d\n", Max(lis_arr)) +} + +func main() { + var n int + fmt.Println("Enter the size of the array : ") + fmt.Scan(&n) + fmt.Println("Enter the array elements : ") + arr := make([]int, n) + + for i := 0; i < n; i++ { + fmt.Scan(&arr[i]) + } + + LIS(arr) +} + From 74248ad04e30d38aa48fdae03f08f3b90b5f781c Mon Sep 17 00:00:00 2001 From: Ayushi Arora Date: Mon, 16 Mar 2020 03:56:52 +0530 Subject: [PATCH 2/4] Longest Increasing Subsequence code in Go --- Longest_Increasing_Subsequence/LIS.go | 81 +++++++++++++++------------ 1 file changed, 46 insertions(+), 35 deletions(-) diff --git a/Longest_Increasing_Subsequence/LIS.go b/Longest_Increasing_Subsequence/LIS.go index c8f1b18de2..d13702887d 100644 --- a/Longest_Increasing_Subsequence/LIS.go +++ b/Longest_Increasing_Subsequence/LIS.go @@ -3,49 +3,60 @@ package main import "fmt" func Max(lis []int) (max int) { - max = lis[0] - for i := 1; i < len(lis); i++ { - if lis[i] > max { - max = lis[i] - } - } - return + max = lis[0] + for i := 1; i < len(lis); i++ { + if lis[i] > max { + max = lis[i] + } + } + return } func LIS(arr []int) { - length := len(arr) - - if length == 0 { - return - } - - lis_arr := make([]int, length) - lis_arr[0] = 1 + length := len(arr) + + if length == 0 { + return + } + + // Slice to store the length of the LIS til some index + lis_arr := make([]int, length) + lis_arr[0] = 1 + + for i := 1; i < length; i++ { + lis_arr[i] = 1 - for i := 1; i < length; i++ { - lis_arr[i] = 1 - - for j := 0; j < i; j++ { - if ( arr[i] > arr[j] && lis_arr[i] < lis_arr[j] + 1) { - lis_arr[i] = lis_arr[j] + 1 - } - } - } + // Computing LIS value in DP Bottom-up + for j := 0; j < i; j++ { + if ( arr[i] > arr[j] && lis_arr[i] < lis_arr[j] + 1) { + lis_arr[i] = lis_arr[j] + 1 + } + } + } - fmt.Printf("Length of the Longest Increasing Subsequence : %d\n", Max(lis_arr)) + fmt.Printf("Length of the Longest Increasing Subsequence : %d\n", Max(lis_arr)) } func main() { - var n int - fmt.Println("Enter the size of the array : ") - fmt.Scan(&n) - fmt.Println("Enter the array elements : ") - arr := make([]int, n) - - for i := 0; i < n; i++ { - fmt.Scan(&arr[i]) - } + var n int + fmt.Println("Enter the size of the array : ") + fmt.Scan(&n) + fmt.Println("Enter the array elements : ") + arr := make([]int, n) + + for i := 0; i < n; i++ { + fmt.Scan(&arr[i]) + } - LIS(arr) + LIS(arr) } +/* Sample Input : +Enter the size of the array : +8 +Enter the array elements : +15 26 13 38 26 52 43 62 + +Sample Output : +Length of the Longest Increasing Subsequence : 5 +*/ From cde1ba96206eb1d4b982d161fdfea77420513d0c Mon Sep 17 00:00:00 2001 From: Ayushi Arora Date: Tue, 17 Mar 2020 18:49:12 +0530 Subject: [PATCH 3/4] Updated problem description --- Longest_Increasing_Subsequence/LIS.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Longest_Increasing_Subsequence/LIS.go b/Longest_Increasing_Subsequence/LIS.go index d13702887d..52157d6b2f 100644 --- a/Longest_Increasing_Subsequence/LIS.go +++ b/Longest_Increasing_Subsequence/LIS.go @@ -1,3 +1,6 @@ +/* Problem Statement : +To find the length of the Longest Increasing Subsequence of a given sequence, that is all the elements of the subsequence are in increasing sorted order. +*/ package main import "fmt" From ae38546bd6248549fbfbd6e5f7df3361e469bfab Mon Sep 17 00:00:00 2001 From: Ayushi Arora Date: Wed, 18 Mar 2020 20:47:09 +0530 Subject: [PATCH 4/4] No more than 80 chars per line --- Longest_Increasing_Subsequence/LIS.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Longest_Increasing_Subsequence/LIS.go b/Longest_Increasing_Subsequence/LIS.go index 52157d6b2f..ee84d02469 100644 --- a/Longest_Increasing_Subsequence/LIS.go +++ b/Longest_Increasing_Subsequence/LIS.go @@ -1,5 +1,7 @@ /* Problem Statement : -To find the length of the Longest Increasing Subsequence of a given sequence, that is all the elements of the subsequence are in increasing sorted order. +To find the length of the Longest Increasing +Subsequence of a given sequence, that is all the +elements of the subsequence are in increasing sorted order. */ package main