Most Stones Removed with Same Row or Column
Real-world version: Pegboard Pin RemovalThe statement here is our own: the same inputs, outputs and technique.
A large pegboard has pins pushed into some of its holes. pins[i] = [row_i, col_i] gives the hole holding pin i, and no two pins share a hole.
Pins come out one at a time. A pin may be pulled only if, at that moment, another pin still in the board sits in the same row or in the same column as it.
Return the largest number of pins that can be pulled out.
Examples
Example 1
- Input
- pins = [[0, 0], [0, 2], [2, 2], [2, 0], [1, 1]]
- Output
- 3
Pull [0, 2] (its row mate [0, 0] is still in), then [2, 2] (its column mate [2, 0] is still in), then [2, 0] (its column mate [0, 0] is still in). That leaves [0, 0] and [1, 1], which share neither a row nor a column, so three pins come out.
Example 2
- Input
- pins = [[0, 1], [1, 0]]
- Output
- 0
The two pins sit in different rows and different columns, so neither one is ever eligible to be pulled.
Example 3
- Input
- pins = [[0, 0], [0, 1], [1, 0], [1, 1]]
- Output
- 3
Pull [1, 1], then [1, 0], then [0, 1]; each had a row or column mate on the board at the time, and [0, 0] stays behind.
Example 4
- Input
- pins = [[3, 3]]
- Output
- 0
A lone pin has no row or column mate, so nothing can be pulled.
Constraints
- 1 <= pins.length <= 1000
- 0 <= row_i, col_i <= 10^4
- No two pins occupy the same hole.
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def remove_stones(pins: list[list[int]]) -> int:public int removeStones(int[][] pins)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 8 test cases: 8 passed
- Passed: 4 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Is Graph Bipartite?Real-world version: Two Shift Conflict SplitMedium
- Possible BipartitionReal-world version: Kennel Split Into Two YardsMedium
- Power Grid MaintenanceReal-world version: The Lowest Working Well in the ClusterMedium
- Minimize Malware SpreadReal-world version: Which Faulty Unit to Take OfflineHard