All problems
0890MediumArrayHash TableMathCountingNumber Theory

Panes Cut to the Same Shape

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2001Number of Pairs of Interchangeable Rectangles

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def interchangeable_rectangles(rectangles: list[list[int]]) -> int:
Java
public long interchangeableRectangles(int[][] rectangles)
September 7
Apply