Please read the new rule regarding the restriction on the use of AI tools. ×

SquarePoint OA Desk Quant Question

Revision en3, by ashwanikumarsharma, 2024-09-29 17:18:24

Please someone solve this, most probably it can be solved by Disjoint Set Union, I tried but can't think of some clear way.

I even posted this on Leetcode(because it is for posting this type of things) but no one solved yet

Problem Statement:

You are given a 2D vector bookshelf, where each element represents a book type numbered from 1 to k. The task is to empty the bookshelf by selecting a book and removing all books of the same type that lie in the same row and column as the selected book. The goal is to determine the minimum number of turns needed to completely clear the bookshelf.

For instance, if you select a book of type 2 located at row 0 and column 3, you would remove all books of type 2 present in row 0 and column 3, including the selected book.


Function Signature:

int emptyshelf(vector<vector<int>>& bookshelf, int k) {
    // Your implementation here
}

Constraints:

  • 1 <= bookshelf.size() <= 100
  • 1 <= bookshelf[0].size() <= 100
  • 1 <= k <= 100

Example 1:

Input:


1 1 1 1 2 2 1 2 1

Output:

3 turns


Example 2:

Input:


1 2 2 1 2 2 2 1 2

Output:
4 turns


Explanation:
For each turn, you select a book and remove all books of the same type either in the same row or column. The objective is to minimize the number of turns it takes to completely empty the bookshelf.


History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en3 English ashwanikumarsharma 2024-09-29 17:18:24 75 Tiny change: '#### Pleas' -> '![ ](https://codeforces.net/c6cc42/codeforces_comment.png)#### Pleas'
en2 English ashwanikumarsharma 2024-09-29 17:09:15 5 Tiny change: '*same row or column** ' -> '*same row and column** '
en1 English ashwanikumarsharma 2024-09-29 13:59:16 1586 Initial revision (published)