Skip to content

Add solution for finding the length of the longest increasing subsequ… #2

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

Merged
merged 1 commit into from
Aug 21, 2024
Merged
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
5 changes: 5 additions & 0 deletions problems/general/longest_increasing_subsequence.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
This problem was asked by **Microsoft**.

Given an array of numbers, find the length of the longest increasing subsequence in the array. The subsequence does not necessarily have to be contiguous.

For example, given the array `[0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15]`, the longest increasing subsequence has length `6`: it is `0, 2, 6, 9, 11, 15`.
25 changes: 25 additions & 0 deletions solutions/general/longest_increasing_subsequence/Approach.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
public class Approach {

private static int[] dp;

public static int longestIncreasingSubsequence(int[] arr) {

int max = 0;
for (int i = 0; i < arr.length; i++) {
dp[i] = 1;
for (int j = 0; j < i; j++) {
if (arr[i] > arr[j]) {
dp[i] = dp[i] > dp[j] + 1 ? dp[i] : dp[j] + 1;
}
}
max = dp[i] > max ? dp[i] : max;
}
return max;
}

public static void main(String[] args) {
int[] arr = { 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15 };
dp = new int[arr.length];
System.err.println(longestIncreasingSubsequence(arr));
}
}
23 changes: 23 additions & 0 deletions solutions/general/longest_increasing_subsequence/approach.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function longestIncreasingSubsequence(nums) {
n = nums.length;
dp = Array(nums.length);
max = 0;

// Build the DP array
for (let i = 0; i < n; i++) {
dp[i] = 1;
for (let j = 0; j < i; j++) {
if (nums[i] > nums[j]) {
dp[i] = dp[i] > dp[j] + 1 ? dp[i] : dp[j] + 1;
}
}
max = max > dp[i] ? max : dp[i];
}

// The length of the longest increasing subsequence
return max;
}

// Example usage:
let nums = [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15];
console.log(longestIncreasingSubsequence(nums));
19 changes: 19 additions & 0 deletions solutions/general/longest_increasing_subsequence/approach.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
def length_of_lis(nums):
if not nums:
return 0

n = len(nums)
dp = [1] * n # Initialize DP array with 1

# Build the DP array
for i in range(1, n):
for j in range(i):
if nums[i] > nums[j]:
dp[i] = max(dp[i], dp[j] + 1)

# The length of the longest increasing subsequence
return max(dp)

# Example usage:
nums = [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15]
print(length_of_lis(nums)) # Output: 6
Loading