Empty Shelf
Updated
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 or 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
emptyShelf(bookshelf: int[][], k: int) → intExamples
Example 1
bookshelf = [[1, 1, 1], [1, 2, 2], [1, 2, 1]]k = 2return = 3🐈⬛🐈⬛
Example 2
bookshelf = [[1, 2, 2], [1, 2, 2], [2, 1, 2]]k = 2return = 4For 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.
Constraints
1 <= bookshelf.size() <= 1001 <= bookshelf[0].size() <= 1001 <= k <= 100
More Squarepoint Capital interview questions
- Lazy Filter over a GeneratorPHONE SCREEN · Seen Nov 2026
- Moving Average of a Data StreamPHONE SCREEN · Seen Nov 2026
- Newton Square Root to a Guaranteed TolerancePHONE SCREEN · Seen Nov 2026
- Probability of Consecutive HeadsPHONE SCREEN · Seen Nov 2026
- Three Points in a Semicircle: Monte Carlo EstimatePHONE SCREEN · Seen Nov 2026
- Mutable Record Store with Error ResultsONSITE INTERVIEW · Seen Sep 2026
- Combine Two Index SequencesPHONE SCREEN · Seen Aug 2026
- Parse, Normalize, and Combine PolynomialsPHONE SCREEN · Seen Aug 2026
- Sorted Insertion SnapshotsPHONE SCREEN · Seen Aug 2026
- Strong and Weak Reference LifecyclePHONE SCREEN · Seen Aug 2026
Similar questions at other companies
- Find a Word Path Moving Right or DownIntuit · OAIntuit interview questions
- Count Eight-Neighbor Unlock PatternsGoogle · ONSITE INTERVIEWGoogle interview questions
- Maximum Words in Disjoint Grid PathsIndeed · PHONE SCREENIndeed interview questions
- Enumerate Right-and-Down Matrix PathsOracle · ONSITE INTERVIEWOracle interview questions
- Enumerate Right-and-Down Matrix PathsBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Word Path Moving Only Right or DownWayfair · PHONE SCREENWayfair interview questions
- Cherry PickupInfosys · OAInfosys interview questions
- Safest Shortest Path Through a GridSnowflake · OASnowflake interview questions
- Minimum Cost Path with TeleportationsAmazon · OA
- Largest Two Non-Overlapping Buildable SquaresAmerican Express · OAAmerican Express interview questions