Score After Flipping Matrix
Real-world version: Maximum Relay Cabinet TotalThe statement here is our own: the same inputs, outputs and technique.
A control cabinet holds a stack of relay racks. Every rack carries the same run of relays side by side, and each relay is either closed, written 1, or open, written 0. racks[i][j] is the state of the relay at position j on rack i.
Each rack reports a reading: read its relays from left to right as the digits of a binary number, so the leftmost relay is the most significant digit. The cabinet total is the sum of all the rack readings.
A maintenance panel offers two kinds of switch, and you may throw as many of them as you like, in any order, including none at all:
- a rack switch picks one rack and flips every relay on it, each closed relay becoming open and each open relay becoming closed;
- a column switch picks one position and flips the relay at that position on every rack at once.
Return the largest cabinet total you can reach.
Examples
Example 1
- Input
- racks = [[0, 1, 1, 0], [1, 0, 0, 1], [1, 1, 1, 0]]
- Output
- 38
Throw the rack switch on rack 0, then the column switches at positions 1 and 2. The racks then read 1111, 1111 and 1000, which are 15, 15 and 8, for a cabinet total of 38.
Example 2
- Input
- racks = [[1, 0, 1], [1, 1, 0]]
- Output
- 11
Left alone the two racks read 101 and 110, which are 5 and 6, for a total of 11.
Example 3
- Input
- racks = [[1, 0, 0, 1, 0]]
- Output
- 31
With a single rack in the cabinet, throwing the column switches at positions 1, 2 and 4 leaves it reading 11111, which is 31.
Constraints
- 1 <= racks.length <= 20
- 1 <= racks[i].length <= 20
- 0 <= racks[i][j] <= 1
- Every rack carries the same number of relays.
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 best_cabinet_total(racks: list[list[int]]) -> int:public int bestCabinetTotal(int[][] racks)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 17 test cases: 17 passed
- Passed: 3 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
- Cinema Seat AllocationReal-world version: Study Pods in the Lecture HallMedium
- Maximum ORReal-world version: Boosting the Channel PanelMedium
- Largest Submatrix With RearrangementsReal-world version: Widest Block After Reordering ColumnsMedium
- Minimum Swaps to Arrange a Binary GridReal-world version: Row Shuffles to Clear the DiagonalMedium