Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

Блог пользователя ashwanikumarsharma

Автор ashwanikumarsharma, история, 5 часов назад, По-английски

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.


  • Проголосовать: нравится
  • +11
  • Проголосовать: не нравится

»
4 часа назад, # |
Rev. 4   Проголосовать: нравится 0 Проголосовать: не нравится

nvm my bad, I could only come up with a k*n^6 DP which I can prove. There maybe some greedy technique as well

  • »
    »
    3 часа назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    It would be nice if you will explain this.

    • »
      »
      »
      2 часа назад, # ^ |
      Rev. 2   Проголосовать: нравится -8 Проголосовать: не нравится

      Sorry my DP is wrong. The dp states are not independent. I can't solve this at all at this point.

      Wrong DP:
»
2 часа назад, # |
Rev. 2   Проголосовать: нравится -12 Проголосовать: не нравится
Wrong solution (see reply)
  • »
    »
    2 часа назад, # ^ |
      Проголосовать: нравится +5 Проголосовать: не нравится

    I thought the same, but it's wrong as far as I understand. Consider a 3x3 grid and 1-based cells (1, 1) (2, 2) and (2, 1) are black. minimum edge cover gives an answer of two, while we could obviously pick cell (2, 1) which would cover all the three black cells at once.

»
2 часа назад, # |
Rev. 2   Проголосовать: нравится +14 Проголосовать: не нравится

Notice that different book types are independent, so we can solve for each book type separately. Build a graph where the nodes are rows and columns, and each book of that type is an edge between its row and its column. This is a bipartite graph, and in addition, every bipartite graph corresponds to a book configuration.

When removing a book, you also remove all the books in the same row and in the same column, which correspond to its adjacent edges in the graph. Therefore, solving this problem solves the minimum edge dominating set on bipartite graphs, which is NP-complete.

In conclusion, either this problem requires some state-of-the-art randomized/approximation algorithm, or the author made a wrong problem (very likely).

  • »
    »
    118 минут назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    nvm the dp is wrong lol cuz the states are dependent.

  • »
    »
    113 минут назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    It's not equivalent to edge dominating set. Consider this case (from sample 1):

    1 1
    1 0
    

    The bipartite graph will have $$$4$$$ nodes, but it only needs $$$1$$$ operation.

    I think it's more like some kind of set cover problem. Nevertheless I believe it's NP-complete.

»
90 минут назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Considering the constraints are very low, please recommend the best method we can use to solve by brute forcing.

»
87 минут назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by ashwanikumarsharma (previous revision, new revision, compare).