<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 刪除小于下一個或變得更小的數組元素 > 原文: [https://www.geeksforgeeks.org/delete-array-elements-which-are-smaller-than-next-or-become-smaller/](https://www.geeksforgeeks.org/delete-array-elements-which-are-smaller-than-next-or-become-smaller/) 給定一個數組`arr[]`和一個數字`k`。 任務是刪除小于下一個元素的`k`個元素(即,如果`arr[i] < arr[i + 1]`則刪除`arr[i]`)或由于下一個元素被刪除而變得小于下一個元素。 例子: ``` Input : arr[] = { 3, 100, 1 } k = 1 Output : 100, 1 Explanation : arr[0] < arr[1] means 3 is less than 100, so delete 3 Input : arr[] = {20, 10, 25, 30, 40} k = 2 Output : 25 30 40 Explanation : First we delete 10 because it follows arr[i] < arr[i+1]. Then we delete 20 because 25 is moved next to it and it also starts following the condition. Input : arr[] = { 23, 45, 11, 77, 18} k = 3 Output : 77, 18 Explanation : We delete 23, 45 and 11 as they follow the condition arr[i] < arr[i+1] ``` **方法**: [堆棧](https://www.geeksforgeeks.org/stack-data-structure-introduction-program/)用于解決此問題。 首先,我們將`arr[0]`推入堆棧`S`,然后將`count`初始化為 0,然后從 1 遍歷到`n`,然后檢查`s.top() < arr[i]`是否為`true`,然后彈出堆棧中的元素,如果`count == k`,則增加計數,然后我們停止循環,然后將堆棧的值存儲在另一個數組中,然后打印該數組。 ## C++ ```cpp // C++ program to delete elements from array. #include <bits/stdc++.h> using namespace std; // Function for deleting k elements void deleteElements(int arr[], int n, int k) {? ????// Create a stack and push arr[0] ????stack<int> s; ????s.push(arr[0]); ????int count = 0; ????// traversing a loop from i = 1 to n ????for (int i=1; i<n; i++) { ????????// condition for deleting an element ????????while (!s.empty() && s.top() < arr[i]? ????????????????????????????&& count < k) {????????????????????????????????????? ????????????s.pop(); ????????????count++; ????????} ????????s.push(arr[i]); ????} ????// Putting elements of stack in a vector ????// from end to begin. ????int m = s.size(); ????vector<int> v(m); // Size of vector is m ????while (!s.empty()) { ????????// push element from stack to vector v ????????v[--m] = s.top(); ????????s.pop(); ????} ????// printing result ????for (auto x : v) ????????cout << x << " "; ????cout << endl; } // Driver code int main() { ????int n = 5, k = 2; ????int arr[] = {20, 10, 25, 30, 40};? ????deleteElements(arr, n, k); ????return 0; } ``` ## Java ```java import java.util.*; //Java program to delete elements from array. class GFG { // Function for deleting k elements ????static void deleteElements(int arr[], int n, int k) { ????????// Create a stack and push arr[0] ????????Stack<Integer> s = new Stack<>(); ????????s.push(arr[0]); ????????int count = 0; ????????// traversing a loop from i = 1 to n ????????for (int i = 1; i < n; i++) { ????????????// condition for deleting an element ????????????while (!s.empty() && s.peek() < arr[i] ????????????????????&& count < k) { ????????????????s.pop(); ????????????????count++; ????????????} ????????????s.push(arr[i]); ????????} ????????// Putting elements of stack in a vector ????????// from end to begin. ????????int m = s.size(); ????????Integer[] v = new Integer[m]; // Size of vector is m ????????while (!s.empty()) { ????????????// push element from stack to vector v ????????????v[--m] = s.peek(); ????????????s.pop(); ????????} ????????// printing result ????????for (Integer x : v) { ????????????System.out.print(x + " "); ????????}; ????????System.out.println(""); ????} // Driver code ????public static void main(String[] args) { ????????int n = 5, k = 2; ????????int arr[] = {20, 10, 25, 30, 40}; ????????deleteElements(arr, n, k); ????} } // This code is contributed by PrinciRaj1992 ``` ## Python3 ```py # Function to delete elements def deleteElements(arr, n, k): ????# create an empty stack st ????st = [] ????st.append(arr[0]) ????# index to mantain the top? ????# of the stack ????top = 0 ????count = 0 ????for i in range(1, n): ????????# pop till the present element? ????????# is greater than stack's top ????????# element ????????while(len(st) != 0 and count < k ???????????????????and st[top] < arr[i]): ????????????st.pop() ????????????count += 1 ????????????top -= 1 ????????st.append(arr[i]) ????????top += 1 ????# print the remaining elements ????for i in range(0, len(st)): ????????print(st[i], " ", end="") # Driver code k = 2 arr = [20, 10, 25, 30, 40]? deleteElements(arr, len(arr), k) # This code is contributed by himan085\. ``` ## C# ```cs // C# program to delete elements from array. using System; using System.Collections.Generic; class GFG { ????// Function for deleting k elements ????static void deleteElements(int []arr, int n, int k)? ????{ ????????// Create a stack and push arr[0] ????????Stack<int> s = new Stack<int>(); ????????s.Push(arr[0]); ????????int count = 0; ????????// traversing a loop from i = 1 to n ????????for (int i = 1; i < n; i++) ????????{ ????????????// condition for deleting an element ????????????while (s.Count != 0 && s.Peek() < arr[i] ????????????????????&& count < k)? ????????????{ ????????????????s.Pop(); ????????????????count++; ????????????} ????????????s.Push(arr[i]); ????????} ????????// Putting elements of stack in a vector ????????// from end to begin. ????????int m = s.Count; ????????int[] v = new int[m]; // Size of vector is m ????????while (s.Count != 0)? ????????{ ????????????// push element from stack to vector v ????????????v[--m] = s.Peek(); ????????????s.Pop(); ????????} ????????// printing result ????????foreach (int x in v)? ????????{ ????????????Console.Write(x + " "); ????????}; ????????Console.Write(""); ????} ????// Driver code ????public static void Main()? ????{ ????????int n = 5, k = 2; ????????int []arr = {20, 10, 25, 30, 40}; ????????deleteElements(arr, n, k); ????} } // This code is contributed by 29AjayKumar ``` **輸出**: ``` 25 30 40 ``` * * * * * *
                  <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>

                              哎呀哎呀视频在线观看