Skip to content

Commit fd4de04

Browse files
authored
Merge branch 'master' into test/EditDistance
2 parents 933b6e4 + cdb6412 commit fd4de04

File tree

6 files changed

+249
-199
lines changed

6 files changed

+249
-199
lines changed
Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,73 @@
11
package com.thealgorithms.dynamicprogramming;
22

3-
/*
4-
5-
* Problem Statement: -
6-
* Find Longest Alternating Subsequence
7-
8-
* A sequence {x1, x2, .. xn} is alternating sequence if its elements satisfy one of the following
9-
relations :
10-
11-
x1 < x2 > x3 < x4 > x5 < …. xn or
12-
x1 > x2 < x3 > x4 < x5 > …. xn
3+
/**
4+
* Class for finding the length of the longest alternating subsequence in an array.
5+
*
6+
* <p>An alternating sequence is a sequence of numbers where the elements alternate
7+
* between increasing and decreasing. Specifically, a sequence is alternating if its elements
8+
* satisfy one of the following relations:
9+
*
10+
* <ul>
11+
* <li>{@code x1 < x2 > x3 < x4 > x5 < ... < xn}</li>
12+
* <li>{@code x1 > x2 < x3 > x4 < x5 > ... > xn}</li>
13+
* </ul>
14+
*
15+
* <p>This class provides a method to compute the length of the longest such subsequence
16+
* from a given array of integers.
1317
*/
1418
public final class LongestAlternatingSubsequence {
1519
private LongestAlternatingSubsequence() {
1620
}
1721

18-
/* Function to return longest alternating subsequence length*/
22+
/**
23+
* Finds the length of the longest alternating subsequence in the given array.
24+
*
25+
* @param arr an array of integers where the longest alternating subsequence is to be found
26+
* @param n the length of the array {@code arr}
27+
* @return the length of the longest alternating subsequence
28+
*
29+
* <p>The method uses dynamic programming to solve the problem. It maintains a 2D array
30+
* {@code las} where:
31+
* <ul>
32+
* <li>{@code las[i][0]} represents the length of the longest alternating subsequence
33+
* ending at index {@code i} with the last element being greater than the previous element.</li>
34+
* <li>{@code las[i][1]} represents the length of the longest alternating subsequence
35+
* ending at index {@code i} with the last element being smaller than the previous element.</li>
36+
* </ul>
37+
*
38+
* <p>The method iterates through the array and updates the {@code las} array based on
39+
* whether the current element is greater or smaller than the previous elements.
40+
* The result is the maximum value found in the {@code las} array.
41+
*/
1942
static int alternatingLength(int[] arr, int n) {
20-
/*
21-
22-
las[i][0] = Length of the longest
23-
alternating subsequence ending at
24-
index i and last element is
25-
greater than its previous element
26-
27-
las[i][1] = Length of the longest
28-
alternating subsequence ending at
29-
index i and last element is
30-
smaller than its previous
31-
element
32-
33-
*/
3443
int[][] las = new int[n][2]; // las = LongestAlternatingSubsequence
3544

45+
// Initialize the dp array
3646
for (int i = 0; i < n; i++) {
3747
las[i][0] = 1;
3848
las[i][1] = 1;
3949
}
4050

4151
int result = 1; // Initialize result
4252

43-
/* Compute values in bottom up manner */
53+
// Compute values in a bottom-up manner
4454
for (int i = 1; i < n; i++) {
45-
/* Consider all elements as previous of arr[i]*/
4655
for (int j = 0; j < i; j++) {
47-
/* If arr[i] is greater, then check with las[j][1] */
56+
// If arr[i] is greater than arr[j], update las[i][0]
4857
if (arr[j] < arr[i] && las[i][0] < las[j][1] + 1) {
4958
las[i][0] = las[j][1] + 1;
5059
}
5160

52-
/* If arr[i] is smaller, then check with las[j][0]*/
61+
// If arr[i] is smaller than arr[j], update las[i][1]
5362
if (arr[j] > arr[i] && las[i][1] < las[j][0] + 1) {
5463
las[i][1] = las[j][0] + 1;
5564
}
5665
}
5766

58-
/* Pick maximum of both values at index i */
59-
if (result < Math.max(las[i][0], las[i][1])) {
60-
result = Math.max(las[i][0], las[i][1]);
61-
}
67+
// Pick the maximum of both values at index i
68+
result = Math.max(result, Math.max(las[i][0], las[i][1]));
6269
}
6370

6471
return result;
6572
}
66-
67-
public static void main(String[] args) {
68-
int[] arr = {10, 22, 9, 33, 49, 50, 31, 60};
69-
int n = arr.length;
70-
System.out.println("Length of Longest "
71-
+ "alternating subsequence is " + alternatingLength(arr, n));
72-
}
7373
}
Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,62 @@
11
package com.thealgorithms.others;
2+
23
import java.util.Arrays;
34
import java.util.Comparator;
45

5-
/* Line Sweep algorithm can be used to solve range problems by first sorting the list of ranges
6-
* by the start value of the range in non-decreasing order and doing a "sweep" through the number
7-
* line(x-axis) by incrementing the start point by 1 and decrementing the end point+1 by 1 on the
8-
* number line.
9-
* An overlapping range is defined as (StartA <= EndB) AND (EndA >= StartB)
10-
* References
11-
* https://en.wikipedia.org/wiki/Sweep_line_algorithm
12-
* https://en.wikipedia.org/wiki/De_Morgan%27s_laws>
6+
/**
7+
* The Line Sweep algorithm is used to solve range problems efficiently. It works by:
8+
* 1. Sorting a list of ranges by their start values in non-decreasing order.
9+
* 2. Sweeping through the number line (x-axis) while updating a count for each point based on the ranges.
10+
*
11+
* An overlapping range is defined as:
12+
* - (StartA <= EndB) AND (EndA >= StartB)
13+
*
14+
* References:
15+
* - https://en.wikipedia.org/wiki/Sweep_line_algorithm
16+
* - https://en.wikipedia.org/wiki/De_Morgan%27s_laws
1317
*/
1418
public final class LineSweep {
1519
private LineSweep() {
1620
}
1721

1822
/**
19-
* Find Maximum end point
20-
* param = ranges : Array of range[start,end]
21-
* return Maximum Endpoint
23+
* Finds the maximum endpoint from a list of ranges.
24+
*
25+
* @param ranges a 2D array where each element is a range represented by [start, end]
26+
* @return the maximum endpoint among all ranges
2227
*/
2328
public static int findMaximumEndPoint(int[][] ranges) {
24-
Arrays.sort(ranges, Comparator.comparingInt(a -> a[1]));
29+
Arrays.sort(ranges, Comparator.comparingInt(range -> range[1]));
2530
return ranges[ranges.length - 1][1];
2631
}
2732

2833
/**
29-
* Find if any ranges overlap
30-
* param = ranges : Array of range[start,end]
31-
* return true if overlap exists false otherwise.
34+
* Determines if any of the given ranges overlap.
35+
*
36+
* @param ranges a 2D array where each element is a range represented by [start, end]
37+
* @return true if any ranges overlap, false otherwise
3238
*/
3339
public static boolean isOverlap(int[][] ranges) {
40+
if (ranges == null || ranges.length == 0) {
41+
return false;
42+
}
3443

3544
int maximumEndPoint = findMaximumEndPoint(ranges);
36-
Arrays.sort(ranges, Comparator.comparingInt(a -> a[0]));
3745
int[] numberLine = new int[maximumEndPoint + 2];
3846
for (int[] range : ranges) {
39-
4047
int start = range[0];
4148
int end = range[1];
4249

4350
numberLine[start] += 1;
4451
numberLine[end + 1] -= 1;
4552
}
4653

47-
int current = 0;
48-
int overlaps = 0;
49-
for (int num : numberLine) {
50-
current += num;
51-
overlaps = Math.max(overlaps, current);
54+
int currentCount = 0;
55+
int maxOverlaps = 0;
56+
for (int count : numberLine) {
57+
currentCount += count;
58+
maxOverlaps = Math.max(maxOverlaps, currentCount);
5259
}
53-
return overlaps > 1;
60+
return maxOverlaps > 1;
5461
}
5562
}

0 commit comments

Comments
 (0)