Sep 24, 2023, 17:09 pm
Hello everyone,
I've been trying to implement the Longest Increasing Subsequence in C++ algorithm after reading about Longest Increasing Subsequence C++.
I'm having some trouble with my code, and I was wondering if someone could help me out. Here's the C++ code I've written so far:
My code is close to the correct solution, but I'm not getting the expected output. Can someone please review my code and point out where I might be going wrong? Any help or suggestions would be greatly appreciated.
I've been trying to implement the Longest Increasing Subsequence in C++ algorithm after reading about Longest Increasing Subsequence C++.
I'm having some trouble with my code, and I was wondering if someone could help me out. Here's the C++ code I've written so far:
PHP Code:
#include <iostream>
#include <vector>
using namespace std;
int longestIncreasingSubsequence(vector<int>& nums) {
int n = nums.size();
vector<int> dp(n, 1);
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (nums[i] > nums[j]) {
dp[i] = max(dp[i], dp[j] + 1);
}
}
}
int maxLen = 0;
for (int i = 0; i < n; i++) {
maxLen = max(maxLen, dp[i]);
}
return maxLen;
}
int main() {
vector<int> nums = {10, 22, 9, 33, 21, 50, 41, 60, 80};
int result = longestIncreasingSubsequence(nums);
cout << "The length of the longest increasing subsequence is: " << result << endl;
return 0;
}