Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Longest Increasing Subsequence code in Go #2493

Merged
merged 4 commits into from Mar 20, 2020
Merged
Changes from 3 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
65 changes: 65 additions & 0 deletions Longest_Increasing_Subsequence/LIS.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* 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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please split this comment across multiple lines. 80 characters per line is standard.

*/
package main
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a brief problem description at the start of file as multi line comment. Rest of the code looks good..


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
}

// 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

// 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))
}

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)
}

/* 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
*/