Min operations to empty an array by erasing any increasing subsequence
Last Updated :
12 Jul, 2023
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;
}
|
Java
import java.util.*;
public class Main
{
static int findMinOp( int [] A, int N)
{
PriorityQueue<Integer> pq = new PriorityQueue<>();
for ( int i = 0 ; i < N; i++)
{
pq.offer(A[i]);
Iterator<Integer> it = pq.iterator();
int prev = Integer.MIN_VALUE;
while (it.hasNext()) {
int curr = it.next();
if (curr == A[i]) {
break ;
}
prev = curr;
}
if (A[i] == prev) {
continue ;
}
pq.remove(prev);
}
return pq.size();
}
public static void main(String[] args) {
int [] A = { 2 , 1 , 4 , 5 , 3 };
int N = A.length;
System.out.println(findMinOp(A, N));
}
}
|
Python3
import heapq
def findMinOp(A, N):
pq = []
for i in range (N):
heapq.heappush(pq, A[i])
if pq.index(A[i]) = = 0 :
continue
pq.remove(A[i - 1 ])
return len (pq)
if __name__ = = '__main__' :
A = [ 2 , 1 , 4 , 5 , 3 ]
N = len (A)
print (findMinOp(A, N))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int FindMinOp( int [] A, int N)
{
Stack< int > stack = new Stack< int >();
for ( int i = 0; i < N; i++)
{
if (stack.Count == 0 || A[i] > stack.Peek())
{
stack.Push(A[i]);
}
else
{
while (stack.Count > 0 && A[i] < stack.Peek())
{
stack.Pop();
}
stack.Push(A[i]);
}
}
return stack.Count;
}
static void Main()
{
int [] A = { 2, 1, 4, 5, 3 };
int N = A.Length;
Console.WriteLine(FindMinOp(A, N));
}
}
|
Javascript
function findMinOp(A, N) {
let pq = [];
for (let i = 0; i < N; i++) {
pq.push(A[i]);
pq.sort((a, b) => a - b);
if (pq.indexOf(A[i]) === 0) {
continue ;
}
pq.splice(pq.indexOf(A[i - 1]), 1);
}
return pq.length;
}
let A = [2, 1, 4, 5, 3];
let N = A.length;
console.log(findMinOp(A, N));
|
Time Complexity: O(NlogN)
Auxiliary Space: O(N)
Related Articles :
Share your thoughts in the comments
Please Login to comment...