Skip to content

Commit cb7edc8

Browse files
committed
Refactor lowerBound method to include size parameter for improved functionality
1 parent 153e3e1 commit cb7edc8

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/main/java/com/thealgorithms/dynamicprogramming/LongestIncreasingSubsequenceNLogN.java

+13-12
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,18 @@
88
*/
99
public class LongestIncreasingSubsequenceNLogN {
1010

11-
/**
12-
* Finds the index of the smallest element in the array that is greater than
13-
* or equal to the target using binary search. If no such element exists,
14-
* returns the index where the target can be inserted to maintain sorted order.
15-
*
16-
* @param arr The array to search in (assumed to be sorted up to a certain point).
17-
* @param target The target value to find the lower bound for.
18-
* @return The index of the lower bound.
19-
*/
20-
private static int lowerBound(int[] arr, int target) {
21-
int l = 0, r = arr.length;
11+
/**
12+
* Finds the index of the smallest element in the array that is greater than
13+
* or equal to the target using binary search. The search is restricted to
14+
* the first `size` elements of the array.
15+
*
16+
* @param arr The array to search in (assumed to be sorted up to `size`).
17+
* @param size The number of valid elements in the array.
18+
* @param target The target value to find the lower bound for.
19+
* @return The index of the lower bound.
20+
*/
21+
private static int lowerBound(int[] arr, int target, int size) {
22+
int l = 0, r = size;
2223

2324
while (l < r) {
2425
int mid = l + (r - l) / 2;
@@ -54,7 +55,7 @@ public static int lengthOfLIS(int[] arr) {
5455

5556
for (int x : arr) {
5657
// Find the position to replace or extend the subsequence
57-
int index = lowerBound(tails, x);
58+
int index = lowerBound(tails, x, size);
5859

5960
// Update the tails array with the current element
6061
tails[index] = x;

0 commit comments

Comments
 (0)