Min operations to empty an array by erasing any increasing subsequence – GeeksforGeeks

Improve Article

Save Article

Like Article

Improve Article

Save Article

Like Article

Given array A[] of size N, the task is to find the number of operations to empty the array by performing the following operation one or more times. In one operation choose any strictly increasing subsequence and delete it from A[].

Examples:

Input: A[] = {2, 1, 4, 5, 3}
Output: 2
Explanation: Following operations are performed to empty the array:

  • Choosing increasing subsequence {2, 4, 5} and removing it from A[], A[] becomes {1, 3}
  • Choosing increasing subsequence {1, 3} and removing it from A[], A[] becomes empty.

Input: A[] = {0, 0, 0, 0}
Output:  4

Approach: The idea is to use a Priority Queue data structure to solve this problem.

Iterate over the array and keep inserting new element to priority queue, if given element is inserted at non-starting position than delete element just previous to that position.

Below are the steps for the above approach:

  • Create priority queue pq[] using a multiset container to store the array elements in sorted order.
  • Iterate from 0 to N – 1.
    • For each iteration, insert the current element in pq[].
    • For each iteration, search the current element in pq[].
  • If the current element is at the initial position in the priority queue, move to the next iteration, else erase the previous element in the priority queue.
  • Return the size of pq[] which will be the answer. 

Below is the code for the above approach:

C++

#include <bits/stdc++.h>

using namespace std;

  

int findMinOp(int A[], int N)

{

  

    

    multiset<int> pq;

  

    

    for (int i = 0; i < N; i++) {

  

        

        

        pq.insert(A[i]);

  

        

        

        auto it = pq.find(A[i]);

  

        

        

        

        if (it == pq.begin())

            continue;

  

        it--;

  

        

        

        pq.erase(it);

    }

  

    

    

    return pq.size();

}

  

int main()

{

    int A[] = { 2, 1, 4, 5, 3 };

    int N = sizeof(A) / sizeof(A[0]);

  

    

    cout << findMinOp(A, N) << endl;

  

    return 0;

}

Time Complexity: O(NlogN)
Auxiliary Space: O(N)

Related Articles :

Like Article

Save Article

 

Stay connected with us on social media platform for instant update click here to join our  Twitter, & Facebook We are now on Telegram. Click here to join our channel (@TechiUpdate) and stay updated with the latest Technology headlines. For all the latest Technology News Click Here 

Read original article here

Denial of responsibility! FineRadar is an automatic aggregator around the global media. All the content are available free on Internet. We have just arranged it in one platform for educational purpose only. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials on our website, please contact us by email – abuse@fineradar.com. The content will be deleted within 24 hours.
arrayEmptyerasingfineradar updateFree Fire Redeem Codesgadget updateGeeksforGeeksIncreasingLatest tech newsMinoperationsSubsequenceTech Headlinestech newsTech News UpdatesTechnologyTechnology News
Comments (0)
Add Comment