|
1 | 1 | package com.thealgorithms.dynamicprogramming;
|
2 | 2 |
|
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. |
13 | 17 | */
|
14 | 18 | public final class LongestAlternatingSubsequence {
|
15 | 19 | private LongestAlternatingSubsequence() {
|
16 | 20 | }
|
17 | 21 |
|
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 | + */ |
19 | 42 | 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 |
| - */ |
34 | 43 | int[][] las = new int[n][2]; // las = LongestAlternatingSubsequence
|
35 | 44 |
|
| 45 | + // Initialize the dp array |
36 | 46 | for (int i = 0; i < n; i++) {
|
37 | 47 | las[i][0] = 1;
|
38 | 48 | las[i][1] = 1;
|
39 | 49 | }
|
40 | 50 |
|
41 | 51 | int result = 1; // Initialize result
|
42 | 52 |
|
43 |
| - /* Compute values in bottom up manner */ |
| 53 | + // Compute values in a bottom-up manner |
44 | 54 | for (int i = 1; i < n; i++) {
|
45 |
| - /* Consider all elements as previous of arr[i]*/ |
46 | 55 | 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] |
48 | 57 | if (arr[j] < arr[i] && las[i][0] < las[j][1] + 1) {
|
49 | 58 | las[i][0] = las[j][1] + 1;
|
50 | 59 | }
|
51 | 60 |
|
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] |
53 | 62 | if (arr[j] > arr[i] && las[i][1] < las[j][0] + 1) {
|
54 | 63 | las[i][1] = las[j][0] + 1;
|
55 | 64 | }
|
56 | 65 | }
|
57 | 66 |
|
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])); |
62 | 69 | }
|
63 | 70 |
|
64 | 71 | return result;
|
65 | 72 | }
|
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 |
| - } |
73 | 73 | }
|
0 commit comments