#### 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 roworand 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:↵
↵
```cpp↵
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.↵
↵
---↵
↵
↵
#### 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
↵
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:↵
↵
```cpp↵
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.↵
↵
---↵
↵
↵