Skip to content

Commit a6b25ac

Browse files
committed
Freestyle | Longest Increasing Subsequence (LIS) | Java
1 parent 6639db3 commit a6b25ac

3 files changed

Lines changed: 120 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Longest Increasing Subsequence with k Replacements
2+
3+
## Problem Statement
4+
5+
Given an array of integers `nums` and an integer `k`, return the length of the longest strictly increasing subsequence that can be formed by **replacing up to `k` elements** of the array with any values of your choice (not necessarily from the array).
6+
7+
You may perform at most `k` replacements. Your goal is to **maximize** the length of the resulting strictly increasing subsequence.
8+
9+
## Input
10+
11+
- An integer array `nums` of length `n` where `1 <= n <= 1000`
12+
- An integer `k` where `0 <= k <= n`
13+
14+
## Output
15+
16+
- An integer representing the length of the longest strictly increasing subsequence achievable by replacing up to `k` elements.
17+
18+
## Example
19+
20+
### Input
21+
- `nums = [5, 3, 4, 2, 6]`
22+
- `k = 1`
23+
24+
### Output
25+
26+
- `4`
27+
28+
### Explanation
29+
30+
The standard LIS is `[3, 4, 6]` (length 3).
31+
If we replace the `2` with `5`, we can obtain `[3, 4, 5, 6]` — strictly increasing, length 4.
32+
33+
## Constraints
34+
35+
- `1 <= nums.length <= 1000`
36+
- `-10^9 <= nums[i] <= 10^9`
37+
- `0 <= k <= nums.length`
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.devstromo.dp.lis.practice.with_k_replacement;
2+
3+
import java.util.Arrays;
4+
5+
public class Solution {
6+
public int longestIncreasingSubsequenceWithKReplacements(int[] nums, int k) {
7+
int n = nums.length;
8+
int[][] dp = new int[n + 1][k + 1];
9+
10+
// Initialize to 1 since the minimum LIS at any position is 1
11+
for (int[] row : dp) Arrays.fill(row, 1);
12+
13+
int maxLen = 1;
14+
15+
for (int i = 1; i < n; i++) {
16+
for (int r = 0; r <= k; r++) {
17+
for (int j = 0; j < i; j++) {
18+
for (int prevRepl = 0; prevRepl <= r; prevRepl++) {
19+
if (nums[j] < nums[i]) {
20+
dp[i][r] = Math.max(dp[i][r], dp[j][prevRepl] + 1);
21+
} else if (r > 0) {
22+
// Try replacing nums[i] to be greater than nums[j]
23+
dp[i][r] = Math.max(dp[i][r], dp[j][r - 1] + 1);
24+
}
25+
}
26+
}
27+
maxLen = Math.max(maxLen, dp[i][r]);
28+
}
29+
}
30+
31+
return maxLen;
32+
}
33+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.devstromo.dp.lis.practice.with_k_replacement;
2+
3+
import org.junit.jupiter.api.DisplayName;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.*;
7+
8+
class SolutionUnitTest {
9+
private final Solution solution = new Solution();
10+
11+
@Test
12+
@DisplayName("Test Longest Increasing Subsequence with k Replacements")
13+
void testLongestIncreasingSubsequenceWithKReplacements() {
14+
int[] nums = {5, 3, 4, 2, 6};
15+
int k = 1;
16+
assertEquals(4, solution.longestIncreasingSubsequenceWithKReplacements(nums, k));
17+
}
18+
19+
@Test
20+
@DisplayName("Test No Replacement Needed")
21+
void testNoReplacementNeeded() {
22+
int[] nums = {1, 2, 3, 4, 5};
23+
int k = 0;
24+
assertEquals(5, solution.longestIncreasingSubsequenceWithKReplacements(nums, k));
25+
}
26+
27+
@Test
28+
@DisplayName("Test One Replacement To Start")
29+
void testOneReplacementToStart() {
30+
int[] nums = {5, 1, 2, 3, 4};
31+
int k = 1;
32+
assertEquals(5, solution.longestIncreasingSubsequenceWithKReplacements(nums, k));
33+
}
34+
35+
@Test
36+
@DisplayName("Test Max Replacements")
37+
void testMaxReplacements() {
38+
int[] nums = {9, 8, 7, 6, 5};
39+
int k = 4;
40+
assertEquals(5, solution.longestIncreasingSubsequenceWithKReplacements(nums, k));
41+
}
42+
43+
@Test
44+
@DisplayName("Test Zero LIS")
45+
void testZeroLIS() {
46+
int[] nums = {5, 5, 5, 5};
47+
int k = 0;
48+
assertEquals(1, solution.longestIncreasingSubsequenceWithKReplacements(nums, k));
49+
}
50+
}

0 commit comments

Comments
 (0)