Class Solution
- java.lang.Object
-
- g2001_2100.s2001_number_of_pairs_of_interchangeable_rectangles.Solution
-
public class Solution extends Object
2001 - Number of Pairs of Interchangeable Rectangles.Medium
You are given
nrectangles represented by a 0-indexed 2D integer arrayrectangles, whererectangles[i] = [widthi, heighti]denotes the width and height of theithrectangle.Two rectangles
iandj(i < j) are considered interchangeable if they have the same width-to-height ratio. More formally, two rectangles are interchangeable ifwidthi/heighti == widthj/heightj(using decimal division, not integer division).Return the number of pairs of interchangeable rectangles in
rectangles.Example 1:
Input: rectangles = [[4,8],[3,6],[10,20],[15,30]]
Output: 6
Explanation: The following are the interchangeable pairs of rectangles by index (0-indexed):
-
Rectangle 0 with rectangle 1: 4/8 == 3/6.
-
Rectangle 0 with rectangle 2: 4/8 == 10/20.
-
Rectangle 0 with rectangle 3: 4/8 == 15/30.
-
Rectangle 1 with rectangle 2: 3/6 == 10/20.
-
Rectangle 1 with rectangle 3: 3/6 == 15/30.
-
Rectangle 2 with rectangle 3: 10/20 == 15/30.
Example 2:
Input: rectangles = [[4,5],[7,8]]
Output: 0
Explanation: There are no interchangeable pairs of rectangles.
Constraints:
n == rectangles.length1 <= n <= 105rectangles[i].length == 21 <= widthi, heighti <= 105
-
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description longinterchangeableRectangles(int[][] rec)
-