Number of Pairs of Interchangeable Rectangles LeetCode solution in Java
1class Solution {23public long interchangeableRectangles(int[][] rectangles) {4Map<Double, Long> hash = new HashMap<>();56for (int i = 0; i < rectangles.length; i++) {7Double tmp = (double) (rectangles[i][0] / (double) rectangles[i][1]);89hash.put(tmp, hash.getOrDefault(tmp, 0L) + 1);10}1112long ans = 0;13for (Map.Entry<Double, Long> entry : hash.entrySet()) {14if (entry.getValue() > 1) {15Long n = entry.getValue();16ans += (n * (n - 1)) / 2;17}18}1920return ans;21}22}