-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
*/ | ||
package main | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
*/ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.