Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add all #404

Closed
wants to merge 2 commits into from
Closed
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
33 changes: 33 additions & 0 deletions C/Longest_Increasing_Subsequence.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <stdio.h>

// Function to find the length of the longest increasing subsequence
int lengthOfLIS(int arr[], int n) {
int dp[n]; // dp[i] will store the length of LIS ending at arr[i]
int maxLength = 1; // Minimum LIS length is 1 (each element itself)

// Initialize each dp[i] to 1 (each element is an increasing subsequence)
for (int i = 0; i < n; i++) {
dp[i] = 1;
}

// Compute LIS for each element
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (arr[i] > arr[j]) { // Check if arr[i] can extend LIS ending at arr[j]
dp[i] = (dp[i] > dp[j] + 1) ? dp[i] : (dp[j] + 1);
}
}
maxLength = (maxLength > dp[i]) ? maxLength : dp[i];
}

return maxLength;
}

int main() {
int arr[] = {10, 9, 2, 5, 3, 7, 101, 18};
int n = sizeof(arr) / sizeof(arr[0]);

printf("Length of Longest Increasing Subsequence: %d\n", lengthOfLIS(arr, n));

return 0;
}