Eine Folge {X1 X2 .. Xn} ist eine alternierende Folge, wenn ihre Elemente eine der folgenden Beziehungen erfüllen:
X1< X2 >X3< X4 >X5< …. xn or
X1 > X2< X3 >X4< X5 >…. xn
Beispiele:
Empfohlene Praxis Längste alternierende Teilsequenz Probieren Sie es aus!Eingang: arr[] = {1 5 4}
Ausgabe: 3
Erläuterung: Das gesamte Array hat die Form x1< x2 >x3Eingang: arr[] = {10 22 9 33 49 50 31 60}
Ausgabe: 6
Erläuterung: Die Teilfolgen {10 22 9 33 31 60} bzw
{10 22 9 49 31 60} oder {10 22 9 50 31 60}
sind die längste Teilfolge der Länge 6
Notiz: Dieses Problem ist eine Erweiterung des am längsten zunehmendes Teilsequenzproblem erfordert jedoch mehr Überlegung, um hier die optimale Unterstruktureigenschaft zu finden
Längste alternierende Teilsequenz mit dynamische Programmierung :
Um das Problem zu lösen, befolgen Sie die folgende Idee:
Wir werden dieses Problem durch die Methode der dynamischen Programmierung lösen, da sie über eine optimale Unterstruktur und überlappende Teilprobleme verfügt
F-String-Python
Befolgen Sie die folgenden Schritte, um das Problem zu lösen:
- Es sei A ein Array der Länge N gegeben
- Wir definieren ein 2D-Array las[n][2], sodass las[i][0] die längste alternierende Teilsequenz enthält, die am Index i endet, und das letzte Element größer als sein vorheriges Element ist
- las[i][1] enthält die längste alternierende Teilsequenz, die am Index i endet, und das letzte Element ist kleiner als sein vorheriges Element, dann haben wir die folgende Wiederholungsbeziehung zwischen ihnen
las[i][0] = Länge der längsten alternierenden Teilsequenz
endet bei Index i und das letzte Element ist größer
als sein vorheriges Elementdas[i][1] = Länge der längsten alternierenden Teilsequenz
endet bei Index i und das letzte Element ist kleiner
als sein vorheriges ElementRekursive Formulierung:
slf4j vs. log4jlas[i][0] = max (las[i][0] las[j][1] + 1);
für alle j< i and A[j] < A[i]las[i][1] = max (las[i][1] las[j][0] + 1);
für alle j< i and A[j] >A[i]
- Die erste Wiederholungsbeziehung basiert auf der Tatsache: Wenn wir uns an der Position i befinden und dieses Element größer als sein vorheriges Element sein muss, werden wir versuchen, ein Element j auszuwählen, damit diese Sequenz (bis zu i) größer ist (< i) such that A[j] < A[i] i.e. A[j] can become A[i]’s previous element and las[j][1] + 1 is bigger than las[i][0] then we will update las[i][0].
- Denken Sie daran, dass wir las[j][1] + 1 und nicht las[j][0] + 1 gewählt haben, um die alternative Eigenschaft zu erfüllen, da in las[j][0] das letzte Element größer als sein vorheriges ist und A[i] größer als A[j] ist, wodurch die alternierende Eigenschaft bei einer Aktualisierung unterbrochen wird. Die obige Tatsache leitet also die erste Wiederholungsbeziehung ab. Ein ähnliches Argument kann auch für die zweite Wiederholungsbeziehung angeführt werden.
Nachfolgend finden Sie die Umsetzung des oben genannten Ansatzes:
C++// C++ program to find longest alternating // subsequence in an array #include using namespace std; // Function to return max of two numbers int max(int a int b) { return (a > b) ? a : b; } // Function to return longest alternating // subsequence length int zzis(int arr[] int n) { /*las[i][0] = Length of the longest alternating subsequence ending at index i and last element is greater than its previous element las[i][1] = Length of the longest alternating subsequence ending at index i and last element is smaller than its previous element */ int las[n][2]; // Initialize all values from 1 for (int i = 0; i < n; i++) las[i][0] = las[i][1] = 1; // Initialize result int res = 1; // Compute values in bottom up manner for (int i = 1; i < n; i++) { // Consider all elements as // previous of arr[i] for (int j = 0; j < i; j++) { // If arr[i] is greater then // check with las[j][1] if (arr[j] < arr[i] && las[i][0] < las[j][1] + 1) las[i][0] = las[j][1] + 1; // If arr[i] is smaller then // check with las[j][0] if (arr[j] > arr[i] && las[i][1] < las[j][0] + 1) las[i][1] = las[j][0] + 1; } // Pick maximum of both values at index i if (res < max(las[i][0] las[i][1])) res = max(las[i][0] las[i][1]); } return res; } // Driver code int main() { int arr[] = { 10 22 9 33 49 50 31 60 }; int n = sizeof(arr) / sizeof(arr[0]); cout << 'Length of Longest alternating ' << 'subsequence is ' << zzis(arr n); return 0; } // This code is contributed by shivanisinghss2110
C // C program to find longest alternating subsequence in // an array #include #include // function to return max of two numbers int max(int a int b) { return (a > b) ? a : b; } // Function to return longest alternating subsequence length int zzis(int arr[] int n) { /*las[i][0] = Length of the longest alternating subsequence ending at index i and last element is greater than its previous element las[i][1] = Length of the longest alternating subsequence ending at index i and last element is smaller than its previous element */ int las[n][2]; /* Initialize all values from 1 */ for (int i = 0; i < n; i++) las[i][0] = las[i][1] = 1; int res = 1; // Initialize result /* Compute values in bottom up manner */ for (int i = 1; i < n; i++) { // Consider all elements as previous of arr[i] for (int j = 0; j < i; j++) { // If arr[i] is greater then check with // las[j][1] if (arr[j] < arr[i] && las[i][0] < las[j][1] + 1) las[i][0] = las[j][1] + 1; // If arr[i] is smaller then check with // las[j][0] if (arr[j] > arr[i] && las[i][1] < las[j][0] + 1) las[i][1] = las[j][0] + 1; } /* Pick maximum of both values at index i */ if (res < max(las[i][0] las[i][1])) res = max(las[i][0] las[i][1]); } return res; } /* Driver code */ int main() { int arr[] = { 10 22 9 33 49 50 31 60 }; int n = sizeof(arr) / sizeof(arr[0]); printf( 'Length of Longest alternating subsequence is %dn' zzis(arr n)); return 0; }
Java // Java program to find longest // alternating subsequence in an array import java.io.*; class GFG { // Function to return longest // alternating subsequence length static int zzis(int arr[] int n) { /*las[i][0] = Length of the longest alternating subsequence ending at index i and last element is greater than its previous element las[i][1] = Length of the longest alternating subsequence ending at index i and last element is smaller than its previous element */ int las[][] = new int[n][2]; /* Initialize all values from 1 */ for (int i = 0; i < n; i++) las[i][0] = las[i][1] = 1; int res = 1; // Initialize result /* Compute values in bottom up manner */ for (int i = 1; i < n; i++) { // Consider all elements as // previous of arr[i] for (int j = 0; j < i; j++) { // If arr[i] is greater then // check with las[j][1] if (arr[j] < arr[i] && las[i][0] < las[j][1] + 1) las[i][0] = las[j][1] + 1; // If arr[i] is smaller then // check with las[j][0] if (arr[j] > arr[i] && las[i][1] < las[j][0] + 1) las[i][1] = las[j][0] + 1; } /* Pick maximum of both values at index i */ if (res < Math.max(las[i][0] las[i][1])) res = Math.max(las[i][0] las[i][1]); } return res; } /* Driver code*/ public static void main(String[] args) { int arr[] = { 10 22 9 33 49 50 31 60 }; int n = arr.length; System.out.println('Length of Longest ' + 'alternating subsequence is ' + zzis(arr n)); } } // This code is contributed by Prerna Saini
Python3 # Python3 program to find longest # alternating subsequence in an array # Function to return max of two numbers def Max(a b): if a > b: return a else: return b # Function to return longest alternating # subsequence length def zzis(arr n): '''las[i][0] = Length of the longest alternating subsequence ending at index i and last element is greater than its previous element las[i][1] = Length of the longest alternating subsequence ending at index i and last element is smaller than its previous element''' las = [[0 for i in range(2)] for j in range(n)] # Initialize all values from 1 for i in range(n): las[i][0] las[i][1] = 1 1 # Initialize result res = 1 # Compute values in bottom up manner for i in range(1 n): # Consider all elements as # previous of arr[i] for j in range(0 i): # If arr[i] is greater then # check with las[j][1] if (arr[j] < arr[i] and las[i][0] < las[j][1] + 1): las[i][0] = las[j][1] + 1 # If arr[i] is smaller then # check with las[j][0] if(arr[j] > arr[i] and las[i][1] < las[j][0] + 1): las[i][1] = las[j][0] + 1 # Pick maximum of both values at index i if (res < max(las[i][0] las[i][1])): res = max(las[i][0] las[i][1]) return res # Driver Code arr = [10 22 9 33 49 50 31 60] n = len(arr) print('Length of Longest alternating subsequence is' zzis(arr n)) # This code is contributed by divyesh072019
C# // C# program to find longest // alternating subsequence // in an array using System; class GFG { // Function to return longest // alternating subsequence length static int zzis(int[] arr int n) { /*las[i][0] = Length of the longest alternating subsequence ending at index i and last element is greater than its previous element las[i][1] = Length of the longest alternating subsequence ending at index i and last element is smaller than its previous element */ int[ ] las = new int[n 2]; /* Initialize all values from 1 */ for (int i = 0; i < n; i++) las[i 0] = las[i 1] = 1; // Initialize result int res = 1; /* Compute values in bottom up manner */ for (int i = 1; i < n; i++) { // Consider all elements as // previous of arr[i] for (int j = 0; j < i; j++) { // If arr[i] is greater then // check with las[j][1] if (arr[j] < arr[i] && las[i 0] < las[j 1] + 1) las[i 0] = las[j 1] + 1; // If arr[i] is smaller then // check with las[j][0] if (arr[j] > arr[i] && las[i 1] < las[j 0] + 1) las[i 1] = las[j 0] + 1; } /* Pick maximum of both values at index i */ if (res < Math.Max(las[i 0] las[i 1])) res = Math.Max(las[i 0] las[i 1]); } return res; } // Driver Code public static void Main() { int[] arr = { 10 22 9 33 49 50 31 60 }; int n = arr.Length; Console.WriteLine('Length of Longest ' + 'alternating subsequence is ' + zzis(arr n)); } } // This code is contributed by anuj_67.
PHP // PHP program to find longest // alternating subsequence in // an array // Function to return longest // alternating subsequence length function zzis($arr $n) { /*las[i][0] = Length of the longest alternating subsequence ending at index i and last element is greater than its previous element las[i][1] = Length of the longest alternating subsequence ending at index i and last element is smaller than its previous element */ $las = array(array()); /* Initialize all values from 1 */ for ( $i = 0; $i < $n; $i++) $las[$i][0] = $las[$i][1] = 1; $res = 1; // Initialize result /* Compute values in bottom up manner */ for ( $i = 1; $i < $n; $i++) { // Consider all elements // as previous of arr[i] for ($j = 0; $j < $i; $j++) { // If arr[i] is greater then // check with las[j][1] if ($arr[$j] < $arr[$i] and $las[$i][0] < $las[$j][1] + 1) $las[$i][0] = $las[$j][1] + 1; // If arr[i] is smaller then // check with las[j][0] if($arr[$j] > $arr[$i] and $las[$i][1] < $las[$j][0] + 1) $las[$i][1] = $las[$j][0] + 1; } /* Pick maximum of both values at index i */ if ($res < max($las[$i][0] $las[$i][1])) $res = max($las[$i][0] $las[$i][1]); } return $res; } // Driver Code $arr = array(10 22 9 33 49 50 31 60 ); $n = count($arr); echo 'Length of Longest alternating ' . 'subsequence is ' zzis($arr $n) ; // This code is contributed by anuj_67. ?> JavaScript <script> // Javascript program to find longest // alternating subsequence in an array // Function to return longest // alternating subsequence length function zzis(arr n) { /*las[i][0] = Length of the longest alternating subsequence ending at index i and last element is greater than its previous element las[i][1] = Length of the longest alternating subsequence ending at index i and last element is smaller than its previous element */ let las = new Array(n); for (let i = 0; i < n; i++) { las[i] = new Array(2); for (let j = 0; j < 2; j++) { las[i][j] = 0; } } /* Initialize all values from 1 */ for (let i = 0; i < n; i++) las[i][0] = las[i][1] = 1; let res = 1; // Initialize result /* Compute values in bottom up manner */ for (let i = 1; i < n; i++) { // Consider all elements as // previous of arr[i] for (let j = 0; j < i; j++) { // If arr[i] is greater then // check with las[j][1] if (arr[j] < arr[i] && las[i][0] < las[j][1] + 1) las[i][0] = las[j][1] + 1; // If arr[i] is smaller then // check with las[j][0] if( arr[j] > arr[i] && las[i][1] < las[j][0] + 1) las[i][1] = las[j][0] + 1; } /* Pick maximum of both values at index i */ if (res < Math.max(las[i][0] las[i][1])) res = Math.max(las[i][0] las[i][1]); } return res; } let arr = [ 10 22 9 33 49 50 31 60 ]; let n = arr.length; document.write('Length of Longest '+ 'alternating subsequence is ' + zzis(arr n)); // This code is contributed by rameshtravel07. </script>
Ausgabe
Length of Longest alternating subsequence is 6
Zeitkomplexität: AN2)
Hilfsraum: O(N), da N zusätzlicher Platz belegt wurde
Effizienter Ansatz: Um das Problem zu lösen, befolgen Sie die folgende Idee:
Im obigen Ansatz verfolgen wir zu jedem Zeitpunkt zwei Werte (die Länge der längsten alternierenden Teilsequenz, die am Index i endet und das letzte Element kleiner oder größer als das vorherige Element ist) für jedes Element im Array. Um den Platz zu optimieren, müssen wir nur zwei Variablen für das Element an jedem Index i speichern
inc = Länge der bislang längsten alternativen Teilsequenz, wobei der aktuelle Wert größer als der vorherige Wert ist.
dec = Länge der bislang längsten alternativen Teilsequenz, wobei der aktuelle Wert kleiner als der vorherige Wert ist.
Der schwierige Teil dieses Ansatzes besteht darin, diese beiden Werte zu aktualisieren.„inc“ sollte genau dann erhöht werden, wenn das letzte Element in der Alternativsequenz kleiner war als das vorherige Element.
„dec“ sollte genau dann erhöht werden, wenn das letzte Element in der alternativen Sequenz größer als das vorherige Element war.
Befolgen Sie die folgenden Schritte, um das Problem zu lösen:
TCP vs. UDP
- Deklarieren Sie zwei Ganzzahlen inc und dec gleich eins
- Führen Sie eine Schleife für i [1 N-1] aus
- Wenn arr[i] größer als das vorherige Element ist, setzen Sie inc gleich dec + 1
- Andernfalls, wenn arr[i] kleiner als das vorherige Element ist, wird dec auf inc + 1 gesetzt
- Gibt das Maximum von Inc. und Dec. zurück
Nachfolgend finden Sie die Umsetzung des oben genannten Ansatzes:
C++// C++ program for above approach #include using namespace std; // Function for finding // longest alternating // subsequence int LAS(int arr[] int n) { // 'inc' and 'dec' initialized as 1 // as single element is still LAS int inc = 1; int dec = 1; // Iterate from second element for (int i = 1; i < n; i++) { if (arr[i] > arr[i - 1]) { // 'inc' changes if 'dec' // changes inc = dec + 1; } else if (arr[i] < arr[i - 1]) { // 'dec' changes if 'inc' // changes dec = inc + 1; } } // Return the maximum length return max(inc dec); } // Driver Code int main() { int arr[] = { 10 22 9 33 49 50 31 60 }; int n = sizeof(arr) / sizeof(arr[0]); // Function Call cout << LAS(arr n) << endl; return 0; }
Java // Java Program for above approach public class GFG { // Function for finding // longest alternating // subsequence static int LAS(int[] arr int n) { // 'inc' and 'dec' initialized as 1 // as single element is still LAS int inc = 1; int dec = 1; // Iterate from second element for (int i = 1; i < n; i++) { if (arr[i] > arr[i - 1]) { // 'inc' changes if 'dec' // changes inc = dec + 1; } else if (arr[i] < arr[i - 1]) { // 'dec' changes if 'inc' // changes dec = inc + 1; } } // Return the maximum length return Math.max(inc dec); } // Driver Code public static void main(String[] args) { int[] arr = { 10 22 9 33 49 50 31 60 }; int n = arr.length; // Function Call System.out.println(LAS(arr n)); } }
Python3 # Python3 program for above approach def LAS(arr n): # 'inc' and 'dec' initialized as 1 # as single element is still LAS inc = 1 dec = 1 # Iterate from second element for i in range(1 n): if (arr[i] > arr[i-1]): # 'inc' changes if 'dec' # changes inc = dec + 1 elif (arr[i] < arr[i-1]): # 'dec' changes if 'inc' # changes dec = inc + 1 # Return the maximum length return max(inc dec) # Driver Code if __name__ == '__main__': arr = [10 22 9 33 49 50 31 60] n = len(arr) # Function Call print(LAS(arr n))
C# // C# program for above approach using System; class GFG { // Function for finding // longest alternating // subsequence static int LAS(int[] arr int n) { // 'inc' and 'dec' initialized as 1 // as single element is still LAS int inc = 1; int dec = 1; // Iterate from second element for (int i = 1; i < n; i++) { if (arr[i] > arr[i - 1]) { // 'inc' changes if 'dec' // changes inc = dec + 1; } else if (arr[i] < arr[i - 1]) { // 'dec' changes if 'inc' // changes dec = inc + 1; } } // Return the maximum length return Math.Max(inc dec); } // Driver code static void Main() { int[] arr = { 10 22 9 33 49 50 31 60 }; int n = arr.Length; // Function Call Console.WriteLine(LAS(arr n)); } } // This code is contributed by divyeshrabadiya07
JavaScript <script> // Javascript program for above approach // Function for finding // longest alternating // subsequence function LAS(arr n) { // 'inc' and 'dec' initialized as 1 // as single element is still LAS let inc = 1; let dec = 1; // Iterate from second element for (let i = 1; i < n; i++) { if (arr[i] > arr[i - 1]) { // 'inc' changes if 'dec' // changes inc = dec + 1; } else if (arr[i] < arr[i - 1]) { // 'dec' changes if 'inc' // changes dec = inc + 1; } } // Return the maximum length return Math.max(inc dec); } let arr = [ 10 22 9 33 49 50 31 60 ]; let n = arr.length; // Function Call document.write(LAS(arr n)); // This code is contributed by mukesh07. </script>
Ausgabe:
6
Zeitkomplexität: AN)
Hilfsraum: O(1)
Quiz erstellen