Skip to content

feat: implement Longest Increasing Subsequence algorithm in R#168

Closed
Sachinn-64 wants to merge 5 commits into
TheAlgorithms:masterfrom
Sachinn-64:feat-LIC
Closed

feat: implement Longest Increasing Subsequence algorithm in R#168
Sachinn-64 wants to merge 5 commits into
TheAlgorithms:masterfrom
Sachinn-64:feat-LIC

Conversation

@Sachinn-64
Copy link
Copy Markdown
Contributor

  • 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.

@Sachinn-64 Sachinn-64 requested a review from siriak as a code owner October 8, 2025 21:13
Copilot AI review requested due to automatic review settings October 8, 2025 21:13
@Sachinn-64 Sachinn-64 requested a review from acylam as a code owner October 8, 2025 21:13
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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
Copy link

Copilot AI Oct 8, 2025

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.
Comment thread dynamic_programming/lonest_increasing_subsequence.r Outdated
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings October 8, 2025 21:15
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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)
Copy link

Copilot AI Oct 8, 2025

Choose a reason for hiding this comment

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

The filename contains a typo: 'lonest' should be 'longest'.

Copilot uses AI. Check for mistakes.
Comment on lines +156 to +159
# 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))
Copy link

Copilot AI Oct 8, 2025

Choose a reason for hiding this comment

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

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.

Suggested change
# 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))

Copilot uses AI. Check for mistakes.
@Sachinn-64
Copy link
Copy Markdown
Contributor Author

@si

@Sachinn-64 Sachinn-64 closed this Oct 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants