Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

add solution to longest-increasing-subsequence problem - resolves #1276 #1283

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions algorithms/CPlusPlus/Arrays/longest-increasing-subsequence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Given int array, return length of longest increasing subsequence
// Ex. nums = [10,9,2,5,3,7,101,18] -> 4, [2,3,7,101]
// Time: O(n^2)
// Space: O(n)
#include <bits/stdc++.h>

class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
int n = nums.size();
vector<int> dp(n, 1);

int result = 1;

for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (nums[j] < nums[i]) {
dp[i] = max(dp[i], dp[j] + 1);
}
}
result = max(result, dp[i]);
}

return result;
}
};
1 change: 1 addition & 0 deletions algorithms/CPlusPlus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- [Counting Inversions](Arrays/counting-inversions.cpp)
- [Dutch Flag Algorithm](Arrays/dutch-flag-algo.cpp)
- [Longest Increasing Subsequence](Arrays/longest-increasing-subsequence.cpp)
- [Left Rotation](Arrays/left-rotation.cpp)
- [Max Subarray Sum](Arrays/max-subarray-sum.cpp)
- [Shift Negatives](Arrays/shift-negatives.cpp)
Expand Down