← 返回 tesla 的题目列表First Solar Panel Placement in a Binary Grid
类型:qbank
Given a 2-D binary grid and a solar panel height / width, return the first coordinate where the panel can fit. `0` means empty, `1` means blocked; the round asks for brute force, implementation, tests, and optimization.
Requirements
Input: a 2-D grid containing 0 and 1.
0 means empty; 1 means blocker.
Input: panel height and width.
Return the first coordinate where the rectangular panel can fit without covering blockers.
Implement the code, write tests, and discuss optimization.
Follow-up discussion may ask how to design a system around this placement computation.
Notes
Brute force checks every top-left coordinate and scans the height x width rectangle.
The standard optimization is a 2-D prefix sum over blockers. Then each candidate rectangle can be checked in O(1) by asking whether blocker count is zero.
Use an exclusive-prefix matrix of size (rows + 1) x (cols + 1) to avoid boundary branches in rectangle queries.
Clarify ordering for 'first coordinate' before coding: row-major top-left is the usual assumption.
Edge cases: panel larger than the grid, zero-sized panel, blockers on boundaries, and multiple valid placements.
Preparation
Implement brute force first, then replace the inner rectangle scan with a 2-D prefix-sum query and compare both outputs on random small grids.
Memorize the rectangle formula sum = ps[r2][c2] - ps[r1][c2] - ps[r2][c1] + ps[r1][c1] using exclusive bottom/right indices.
Add tests for 1x1 panels, full-grid panels, panel larger than grid, blockers on corners, and row-major tie-breaking.