<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 在線性時間內找到大小為 3 的排序子序列 > 原文: [https://www.geeksforgeeks.org/find-a-sorted-subsequence-of-size-3-in-linear-time/](https://www.geeksforgeeks.org/find-a-sorted-subsequence-of-size-3-in-linear-time/) 給定 n 個整數的數組,請找到 3 個元素,以便在`0(n)`時間內`a[i] < a[j] < a[k]`和`i < j < k`。 如果有多個這樣的三胞胎,請打印其中的任何一個。 **示例**: ``` Input: arr[] = {12, 11, 10, 5, 6, 2, 30} Output: 5, 6, 30 Explanation: As 5 < 6 < 30, and they appear in the same sequence in the array Input: arr[] = {1, 2, 3, 4} Output: 1, 2, 3 OR 1, 2, 4 OR 2, 3, 4 Explanation: As the array is sorted, for every i, j, k, where i < j < k, arr[i] < arr[j] < arr[k] Input: arr[] = {4, 3, 2, 1} Output: No such triplet exists. ``` **提示**:使用輔助空間。 **解決方案**:因此,主要動機是找到一個元素,該元素的元素在數組的左側小于其自身,而元素的大小大于在數組的右側,如果有的話 這樣的元素存在一個滿足標準的三元組。 **方法**:這可以通過非常簡單的方式解決。 要查找數組左側的元素小于其自身的元素,請在從起始索引即(0)開始遍歷數組時檢查該元素是否為最小元素,并檢查是否存在大于 本身在數組的右側,從該數組的末尾遍歷(`n-1`)時,檢查該元素是否為最大元素。 如果該元素不是從 0 到該索引的最小元素,則它的左側小于其自身的元素;類似地,如果該元素不是從該索引到最后一個索引的最大元素,則其上的元素更大 它的右側。 **算法** 1. 創建一個較小的輔助數組`[0..n-1]`。 `small[i]`存儲小于`arr[i]`且在左側的數字的索引。 如果沒有這樣的元素,則數組包含 -1。 2. 創建另一個更大`[0..n-1]`的輔助數組。 `Greater[i]`存儲大于`arr[i]`且位于`arr[i]`右側的數字的索引。 如果沒有這樣的元素,則數組包含 -1。 3. 最后遍歷`smaller[]`和`biger[]`,并找到索引[i],其中`smaller[i]`和`Greater[i]`都不等于 -1。 ## C++ ```cpp // C++ program to find a sorted // sub-sequence of size 3 #include <bits/stdc++.h> using namespace std; // A function to fund a sorted // sub-sequence of size 3 void find3Numbers(int arr[], int n) { ????// Index of maximum element ????// from right side ????int max = n - 1; ????// Index of minimum element ????// from left side ????int min = 0; ????int i; ????// Create an array that will store ????// index of a smaller element on left side. ????// If there is no smaller element on left ????// side, then smaller[i] will be -1\. ????int* smaller = new int[n]; ????// first entry will always be -1 ????smaller[0] = -1; ????for (i = 1; i < n; i++) { ????????if (arr[i] <= arr[min]) { ????????????min = i; ????????????smaller[i] = -1; ????????} ????????else ????????????smaller[i] = min; ????} ????// Create another array that will ????// store index of a greater element ????// on right side. If there is no greater ????// element on right side, then ????// greater[i] will be -1\. ????int* greater = new int[n]; ????// last entry will always be -1 ????greater[n - 1] = -1; ????for (i = n - 2; i >= 0; i--) { ????????if (arr[i] >= arr[max]) { ????????????max = i; ????????????greater[i] = -1; ????????} ????????else ????????????greater[i] = max; ????} ????// Now find a number which has both ????// a greater number on right side and ????// smaller number on left side ????for (i = 0; i < n; i++) { ????????if (smaller[i] != -1 && greater[i] != -1) { ????????????cout << arr[smaller[i]] ?????????????????<< " " << arr[i] << " " ?????????????????<< arr[greater[i]]; ????????????return; ????????} ????} ????// If we reach number, then there are ????// no such 3 numbers ????cout << "No such triplet found"; ????// Free the dynamically allocated memory ????// to avoid memory leak ????delete[] smaller; ????delete[] greater; ????return; } // Driver code int main() { ????int arr[] = { 12, 11, 10, 5, 6, 2, 30 }; ????int n = sizeof(arr) / sizeof(arr[0]); ????find3Numbers(arr, n); ????return 0; ????a greater number on } // This is code is contributed by rathbhupendra ``` ## C ``` // C program to find a sorted // sub-sequence of size 3 #include <stdio.h> // A function to fund a sorted // sub-sequence of size 3 void find3Numbers(int arr[], int n) { ????// Index of maximum element ????// from right side ????int max = n - 1; ????// Index of minimum element ????// from left side ????int min = 0; ????int i; ????// Create an array that will store ????// index of a smaller element on left side. ????// If there is no smaller element on left side, ????// then smaller[i] will be -1\. ????int* smaller = new int[n]; ????// first entry will always be -1 ????smaller[0] = -1; ????for (i = 1; i < n; i++) { ????????if (arr[i] <= arr[min]) { ????????????min = i; ????????????smaller[i] = -1; ????????} ????????else ????????????smaller[i] = min; ????} ????// Create another array that will ????// store index of a greater element ????// on right side. If there is no greater ????// element on right side, then ????// greater[i] will be -1\. ????int* greater = new int[n]; ????// last entry will always be -1 ????greater[n - 1] = -1; ????for (i = n - 2; i >= 0; i--) { ????????if (arr[i] >= arr[max]) { ????????????max = i; ????????????greater[i] = -1; ????????} ????????else ????????????greater[i] = max; ????} ????// Now find a number which has ????// both a greater number on right ????// side and smaller number on left side ????for (i = 0; i < n; i++) { ????????if (smaller[i] != -1 && greater[i] != -1) { ????????????printf("%d %d %d", arr[smaller[i]], ???????????????????arr[i], arr[greater[i]]); ????????????return; ????????} ????} ????// If we reach number, then ????// there are no such 3 numbers ????printf("No such triplet found"); ????// Free the dynamically allocated memory ????// to avoid memory leak ????delete[] smaller; ????delete[] greater; ????return; } // Driver program to test above function int main() { ????int arr[] = { 12, 11, 10, 5, 6, 2, 30 }; ????int n = sizeof(arr) / sizeof(arr[0]); ????find3Numbers(arr, n); ????return 0; } ``` ## Java ```java // Java program to find a sorted // sub-sequence of size 3 import java.io.*; class SortedSubsequence { ????// A function to find a sorted ????// sub-sequence of size 3 ????static void find3Numbers(int arr[]) ????{ ????????int n = arr.length; ????????// Index of maximum element ????????// from right side ????????int max = n - 1; ????????// Index of minimum element ????????// from left side ????????int min = 0; ????????int i; ????????// Create an array that will store ????????// index of a smaller element on left side. ????????// If there is no smaller element on left ????????// side, then smaller[i] will be -1\. ????????int[] smaller = new int[n]; ????????// first entry will always be -1 ????????smaller[0] = -1; ????????for (i = 1; i < n; i++) { ????????????if (arr[i] <= arr[min]) { ????????????????min = i; ????????????????smaller[i] = -1; ????????????} ????????????else ????????????????smaller[i] = min; ????????} ????????// Create another array that will ????????// store index of a greater element ????????// on right side. If there is no greater ????????// element on right side, then greater[i] ????????// will be -1\. ????????int[] greater = new int[n]; ????????// last entry will always be -1 ????????greater[n - 1] = -1; ????????for (i = n - 2; i >= 0; i--) { ????????????if (arr[i] >= arr[max]) { ????????????????max = i; ????????????????greater[i] = -1; ????????????} ????????????else ????????????????greater[i] = max; ????????} ????????// Now find a number which has ????????// both greater number on right ????????// side and smaller number on left side ????????for (i = 0; i < n; i++) { ????????????if ( ????????????????smaller[i] != -1 && greater[i] != -1) { ????????????????System.out.print( ????????????????????arr[smaller[i]] + " " + arr[i] ????????????????????+ " " + arr[greater[i]]); ????????????????return; ????????????} ????????} ????????// If we reach number, then there ????????// are no such 3 numbers ????????System.out.println("No such triplet found"); ????????return; ????} ????public static void main(String[] args) ????{ ????????int arr[] = { 12, 11, 10, 5, 6, 2, 30 }; ????????find3Numbers(arr); ????} } /* This code is contributed by Devesh Agrawal*/ ``` ## Python ``` # Python program to fund a sorted # sub-sequence of size 3 def find3numbers(arr): ????n = len(arr) # Index of maximum element from right side ????max = n-1 # Index of minimum element from left side? ????min = 0 ????# Create an array that will store? ????# index of a smaller element on left side.? ????# If there is no smaller element on left side, # then smaller[i] will be -1\. ????smaller = [0]*10000 ????smaller[0] = -1 ????for i in range(1, n): ????????if (arr[i] <= arr[min]): ????????????min = i ????????????smaller[i] = -1 ????????else: ????????????smaller[i] = min ????# Create another array that will? ????# store index of a greater element? ????# on right side. If there is no greater? # element on right side, then greater[i]? # will be -1\. ????greater = [0]*10000 ????greater[n-1] = -1 ????for i in range(n-2, -1, -1): ????????if (arr[i] >= arr[max]): ????????????max = i ????????????greater[i] = -1 ????????else: ????????????greater[i] = max ????# Now find a number which has? ????# both a greater number on right? # side and smaller number on left side ????for i in range(0, n): ????????if smaller[i] != -1 and greater[i] != -1: ????????????print arr[smaller[i]], arr[i], arr[greater[i]] ????????????return ????# If we reach here, then there are no such 3 numbers ????print "No triplet found" ????return # Driver function to test above function arr = [12, 11, 10, 5, 6, 2, 30] find3numbers(arr) # This code is contributed by Devesh Agrawal ``` ## C# ```cs // C# program to find a sorted // subsequence of size 3 using System; class SortedSubsequence { ????// A function to find a sorted ????// subsequence of size 3 ????static void find3Numbers(int[] arr) ????{ ????????int n = arr.Length; ????????// Index of maximum element from right side ????????int max = n - 1; ????????// Index of minimum element from left side ????????int min = 0; ????????int i; ????????// Create an array that will store index ????????// of a smaller element on left side. ????????// If there is no smaller element ????????// on left side, then smaller[i] will be -1\. ????????int[] smaller = new int[n]; ????????// first entry will always be -1 ????????smaller[0] = -1; ????????for (i = 1; i < n; i++) { ????????????if (arr[i] <= arr[min]) { ????????????????min = i; ????????????????smaller[i] = -1; ????????????} ????????????else ????????????????smaller[i] = min; ????????} ????????// Create another array that will store ????????// index of a greater element on right side. ????????// If there is no greater element on ????????// right side, then greater[i] will be -1\. ????????int[] greater = new int[n]; ????????// last entry will always be -1 ????????greater[n - 1] = -1; ????????for (i = n - 2; i >= 0; i--) { ????????????if (arr[i] >= arr[max]) { ????????????????max = i; ????????????????greater[i] = -1; ????????????} ????????????else ????????????????greater[i] = max; ????????} ????????// Now find a number which has ????????// both a greater number on right side ????????// and smaller number on left side ????????for (i = 0; i < n; i++) { ????????????if (smaller[i] != -1 && greater[i] != -1) { ????????????????Console.Write( ????????????????????arr[smaller[i]] + " " + arr[i] ????????????????????+ " " + arr[greater[i]]); ????????????????return; ????????????} ????????} ????????// If we reach number, then there ????????// are no such 3 numbers ????????Console.Write("No such triplet found"); ????????return; ????} ????// Driver code ????public static void Main() ????{ ????????int[] arr = { 12, 11, 10, 5, 6, 2, 30 }; ????????find3Numbers(arr); ????} } /* This code is contributed by vt_m*/ ``` ## PHP ```php <?php? // PHP program to find a sorted? // subsequence of size 3 // A function to fund a sorted // subsequence of size 3 function find3Numbers(&$arr, $n) { ????// Index of maximum element from right side ????$max = $n - 1; ????// Index of minimum element from left side? ????$min = 0;? ????// Create an array that will store? ????// index of a smaller element on ????// left side. If there is no smaller? ????// element on left side, then? ????// smaller[i] will be -1\. ????$smaller = array_fill(0, $n, NULL); ????$smaller[0] = -1; // first entry will ??????????????????????// always be -1 ????for ($i = 1; $i < $n; $i++) ????{ ????????if ($arr[$i] <= $arr[$min]) ????????{ ????????????$min = $i; ????????????$smaller[$i] = -1; ????????} ????????else ????????????$smaller[$i] = $min; ????} ????// Create another array that will? ????// store index of a greater element? ????// on right side. If there is no? ????// greater element on right side,? ????// then greater[i] will be -1\. ????$greater = array_fill(0, $n, NULL); ????// last entry will always be -1 ????$greater[$n - 1] = -1;? ????for ($i = $n - 2; $i >= 0; $i--) ????{ ????????if ($arr[$i] >= $arr[$max]) ????????{ ????????????$max = $i; ????????????$greater[$i] = -1; ????????} ????????else ????????????$greater[$i] = $max; ????} ????// Now find a number which has both? ????// a greater number on right side? ????// and smaller number on left side ????for ($i = 0; $i < $n; $i++) ????{ ????????if ($smaller[$i] != -1 &&? ????????????$greater[$i] != -1) ????????{ ????????????echo $arr[$smaller[$i]]." ". ??????????????????????$arr[$i] . " " .? ??????????????????????$arr[$greater[$i]]; ????????????return; ????????} ????} ????// If we reach number, then there ????// are no such 3 numbers ????printf("No such triplet found"); ????return; } // Driver Code $arr = array(12, 11, 10, 5, 6, 2, 30); $n = sizeof($arr); find3Numbers($arr, $n); // This code is contributed? // by ChitraNayal ?> ``` **輸出**: ``` 5 6 30 ``` **復雜度分析** * **時間復雜度**:`O(n)`。 由于數組僅傳播一次,并且沒有嵌套循環,因此時間復雜度將約為`n`。 * **輔助空間**:`O(n)`。 由于需要兩個額外的數組來存儲前一個較小元素和下一個較大元素的索引,因此所需空間也將為`n` **參考**: [如何在線性時間中按數組的遞增順序和索引找到 3 個數字](http://stackoverflow.com/questions/10008118/how-to-find-3-numbers-in-increasing-order-and-increasing-indices-in-an-array-in) **練習**: 1. 找到大小為 3 的子序列,使得`arr[i] arr[k]`。 2. 在線性時間內找到大小為 4 的排序子序列
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看