forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2001-number-of-pairs-of-interchangeable-rectangles.js
47 lines (42 loc) · 1.22 KB
/
2001-number-of-pairs-of-interchangeable-rectangles.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* Brute Force
* Time O(N^2) | Space O(1)
* https://leetcode.com/problems/number-of-pairs-of-interchangeable-rectangles
* @param {number[][]} rectangles
* @return {number}
*/
var interchangeableRectangles = (rectangles) => {
let totalPair = 0;
for (let i = 0; i < rectangles.length; i++) {
for (let j = i + 1; j < rectangles.length; j++) {
if (rectangles[i][1] / rectangles[i][0] === rectangles[j][1] / rectangles[j][0]) {
totalPair++;
}
}
}
return totalPair;
};
/**
* Linear
* Time O(N) | Space O(n)
* @param {number[][]} rectangles
* @return {number}
*/
var interchangeableRectangles = (rectangles) => {
const ratioFrequency = {};
for (let i = 0; i < rectangles.length; i++) {
const ratio = rectangles[i][1] / rectangles[i][0];
if (ratioFrequency[ratio.toString()]) {
ratioFrequency[ratio.toString()] += 1;
} else {
ratioFrequency[ratio.toString()] = 1;
}
}
let totalPair = 0;
for (const key in ratioFrequency) {
if (ratioFrequency[key] !== 1) {
totalPair += (ratioFrequency[key] * (ratioFrequency[key] - 1)) / 2;
}
}
return totalPair;
};