feat: implement Longest Increasing Subsequence algorithm in R#168
feat: implement Longest Increasing Subsequence algorithm in R#168Sachinn-64 wants to merge 5 commits into
Conversation
Sachinn-64
commented
Oct 8, 2025
- Implemented the Longest Increasing Subsequence algorithm in R.
- Finds the length and sequence of the longest strictly increasing subsequence.
- Efficient O(n log n) binary search implementation with O(n²) DP fallback.
- Handles edge cases including empty arrays, duplicates, and single elements.
- Prints DP table, actual LIS sequence, and performance comparisons.
- Time Complexity: O(n log n) optimized, O(n²) basic DP.
- Space Complexity: O(n) for DP array, O(1) for optimized version.
There was a problem hiding this comment.
Pull Request Overview
This PR implements the Longest Increasing Subsequence (LIS) algorithm in R with both O(n²) dynamic programming and O(n log n) optimized binary search approaches. The implementation includes comprehensive testing and real-world examples.
- Adds complete LIS algorithm with both basic DP and optimized binary search implementations
- Includes comprehensive test cases covering edge cases, performance comparisons, and real-world stock price analysis
- Provides helper functions for visualization and analysis of the DP table and results
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| dynamic_programming/lonest_increasing_subsequence.r | Complete implementation of LIS algorithm with both O(n²) and O(n log n) solutions, comprehensive testing suite, and real-world examples |
| dynamic_programming/1_knapsack_problem.r | Implementation of 0/1 Knapsack problem for comparison and context within the dynamic programming collection |
Comments suppressed due to low confidence (1)
dynamic_programming/lonest_increasing_subsequence.r:1
- Corrected spelling of 'lonest' to 'longest' in filename.
# Longest Increasing Subsequence (Dynamic Programming)
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| max_length = max_length, | ||
| dp_array = dp, | ||
| lis_sequence = lis_sequence, | ||
| dp_table = dp |
There was a problem hiding this comment.
The dp_table field is redundant as it duplicates the dp_array field with identical content. Consider removing this duplication to simplify the return structure.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| @@ -0,0 +1,249 @@ | |||
| # Longest Increasing Subsequence (Dynamic Programming) | |||
There was a problem hiding this comment.
The filename contains a typo: 'lonest' should be 'longest'.
| # For simplicity, return just one LIS (same as the main function) | ||
| # Finding all possible LIS is complex and not essential for the algorithm demonstration | ||
| result <- longest_increasing_subsequence(nums) | ||
| return(list(result$lis_sequence)) |
There was a problem hiding this comment.
The function recalculates the entire DP solution unnecessarily. Since the DP array was already computed on lines 145-152, the LIS sequence should be extracted from that existing computation instead of calling the main function again.
| # For simplicity, return just one LIS (same as the main function) | |
| # Finding all possible LIS is complex and not essential for the algorithm demonstration | |
| result <- longest_increasing_subsequence(nums) | |
| return(list(result$lis_sequence)) | |
| # For simplicity, extract just one LIS using the computed DP array | |
| # Finding all possible LIS is complex and not essential for the algorithm demonstration | |
| # Backtrack to find one LIS sequence | |
| lis_sequence <- c() | |
| current_length <- max_length | |
| for (i in seq(n, 1)) { | |
| if (dp[i] == current_length) { | |
| lis_sequence <- c(nums[i], lis_sequence) | |
| current_length <- current_length - 1 | |
| } | |
| if (current_length == 0) break | |
| } | |
| return(list(lis_sequence)) |