Number of Pairs of Interchangeable Rectangles
Real-world version: Panes Cut to the Same ShapeThe statement here is our own: the same inputs, outputs and technique.
A glazier's stock is given as rectangles, where rectangles[i] is [width, height].
Two panes are the same shape when their width divided by their height comes to the same value.
Return how many pairs of panes in the stock are the same shape.
Examples
Example 1
- Input
- rectangles = [[24, 36], [14, 21], [90000, 60000], [15, 10], [7, 11]]
- Output
- 2
The first two both reduce to 2 by 3, and the next two both reduce to 3 by 2, giving one pair from each group. The last pane is on its own.
Example 2
- Input
- rectangles = [[1, 7], [2, 14], [3, 21], [4, 28], [5, 35], [6, 42]]
- Output
- 15
All six panes reduce to 1 by 7, so every one of the fifteen pairs matches.
Example 3
- Input
- rectangles = [[2, 3], [3, 2]]
- Output
- 0
Turning a pane on its side gives a different proportion, so these two are not the same shape.
Constraints
- 1 <= rectangles.length <= 10^5
- rectangles[i].length == 2
- 1 <= rectangles[i][0] <= 10^5
- 1 <= rectangles[i][1] <= 10^5
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 interchangeable_rectangles(rectangles: list[list[int]]) -> int:public long interchangeableRectangles(int[][] rectangles)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 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
- Sorted GCD Pair QueriesReal-world version: Gear Mesh QueriesHard
- Number of Good PairsReal-world version: Matched Spoke PairsEasy
- Minimum Jumps to Reach End via Prime TeleportationReal-world version: Relay Hops Along the Valve LineMedium
- Count Number of Bad PairsReal-world version: Out of Step Seat PairsMedium