Number of Pairs of Interchangeable Rectangles LeetCode solution in Java

1
class Solution {
2

3
public long interchangeableRectangles(int[][] rectangles) {
4
Map<Double, Long> hash = new HashMap<>();
5

6
for (int i = 0; i < rectangles.length; i++) {
7
Double tmp = (double) (rectangles[i][0] / (double) rectangles[i][1]);
8

9
hash.put(tmp, hash.getOrDefault(tmp, 0L) + 1);
10
}
11

12
long ans = 0;
13
for (Map.Entry<Double, Long> entry : hash.entrySet()) {
14
if (entry.getValue() > 1) {
15
Long n = entry.getValue();
16
ans += (n * (n - 1)) / 2;
17
}
18
}
19

20
return ans;
21
}
22
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0