Dynamic Programming Challenge: Longest Increasing Subsequence [message #1862575] |
Tue, 12 December 2023 11:13 |
bretny relly Messages: 14 Registered: March 2023 |
Junior Member |
|
|
Given an array of integers, find the length of the longest increasing subsequence. A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.
For example:
#include <iostream>
#include <vector>
int longestIncreasingSubsequence(const std::vector<int>& nums) {
// Your dynamic programming solution goes here.
// Return the length of the longest increasing subsequence.
}
int main() {
std::vector<int> sequence = {10, 22, 9, 33, 21, 50, 41, 60, 80};
int result = longestIncreasingSubsequence(sequence);
std::cout << "Length of the longest increasing subsequence: " << result << std::endl;
return 0;
}
I'm specifically interested in implementing this using dynamic programming techniques. How can I approach this problem using dynamic programming, and what would be the C++ code for solving it? Any insights, code snippets, or explanations would be incredibly helpful in mastering dynamic programming for this particular challenge. Thank you for your assistance!
|
|
|
Re: Dynamic Programming Challenge: Longest Increasing Subsequence [message #1862670 is a reply to message #1862575] |
Wed, 20 December 2023 06:50 |
havina bram Messages: 1 Registered: December 2023 |
Junior Member |
|
|
#include <iostream>
#include <vector>
int longestIncreasingSubsequence(const std::vector<int>& nums) {
int n = nums.size();
if (n == 0) return 0; // Empty array has LIS of length 0.
// Initialize the dp array with all values set to 1.
std::vector<int> dp(n, 1);
// Iterate over each element in the array.
for (int i = 1; i < n; ++i) {
// Check all previous elements for increasing subsequence.
for (int j = 0; j < i; ++j) {
if (nums[i] > nums[j]) {
dp[i] = std::max(dp[i], dp[j] + 1);
}
}
}
// Find the maximum value in dp, which represents the length of the LIS.
int maxLIS = *std::max_element(dp.begin(), dp.end());
return maxLIS;
}
int main() {
std::vector<int> sequence = {10, 22, 9, 33, 21, 50, 41, 60, 80};
int result = longestIncreasingSubsequence(sequence);
std::cout << "Length of the longest increasing subsequence: " << result << std::endl;
return 0;
}
Try it
|
|
|
Re: Dynamic Programming Challenge: Longest Increasing Subsequence [message #1863104 is a reply to message #1862670] |
Tue, 16 January 2024 12:09 |
Setua Sutli Messages: 4 Registered: November 2023 |
Junior Member |
|
|
Here's a C++ implementation using dynamic programming to find the length of the longest increasing subsequence:
C++
#include <iostream>
#include <vector>
int longestIncreasingSubsequence(const std::vector<int>& nums) {
int n = nums.size();
std::vector<int> dp(n, 1); // dp[i] stores the length of the longest increasing subsequence ending at nums[i]
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (nums[i] > nums[j] && dp[i] < dp[j] + 1) {
dp[i] = dp[j] + 1;
}
}
}
int longest = 0;
for (int len : dp) {
longest = std::max(longest, len);
}
return longest;
}
Explanation:
Initialize dp array: Create a dp array of size n where each element dp[i] will store the length of the longest increasing subsequence ending at index i. Initialize all elements to 1, as a single element is itself an increasing subsequence of length 1.
Iterate through the array: For each element nums[i], iterate through all the elements before it (nums[j] where j < i).
Check for increasing subsequence: If nums[i] is greater than nums[j], it means we can potentially extend an increasing subsequence ending at nums[j] by including nums[i].
Update dp[i]: If dp[i] (current length of LIS ending at i) is less than dp[j] + 1 (length of LIS ending at j plus 1 for including nums[i]), update dp[i] to dp[j] + 1.
Find the maximum length: After the nested loops, the maximum value in the dp array will represent the length of the longest increasing subsequence in the entire array.
For more Explanation just refer these links:
https://www.geeksforgeeks.org/problems/longest-increasing-subsequence-1587115620/1 https://omegle.club
https://m.youtube.com/watch?v=22s1xxRvy28
[Updated on: Wed, 21 February 2024 10:53] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.02877 seconds