Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2c23301

Browse files
committed
Add solution #2001
1 parent a5aeb0c commit 2c23301

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,679 LeetCode solutions in JavaScript
1+
# 1,680 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1535,6 +1535,7 @@
15351535
1997|[First Day Where You Have Been in All the Rooms](./solutions/1997-first-day-where-you-have-been-in-all-the-rooms.js)|Medium|
15361536
1998|[GCD Sort of an Array](./solutions/1998-gcd-sort-of-an-array.js)|Hard|
15371537
2000|[Reverse Prefix of Word](./solutions/2000-reverse-prefix-of-word.js)|Easy|
1538+
2001|[Number of Pairs of Interchangeable Rectangles](./solutions/2001-number-of-pairs-of-interchangeable-rectangles.js)|Medium|
15381539
2011|[Final Value of Variable After Performing Operations](./solutions/2011-final-value-of-variable-after-performing-operations.js)|Easy|
15391540
2016|[Maximum Difference Between Increasing Elements](./solutions/2016-maximum-difference-between-increasing-elements.js)|Easy|
15401541
2017|[Grid Game](./solutions/2017-grid-game.js)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* 2001. Number of Pairs of Interchangeable Rectangles
3+
* https://leetcode.com/problems/number-of-pairs-of-interchangeable-rectangles/
4+
* Difficulty: Medium
5+
*
6+
* You are given n rectangles represented by a 0-indexed 2D integer array rectangles,
7+
* where rectangles[i] = [widthi, heighti] denotes the width and height of the ith rectangle.
8+
*
9+
* Two rectangles i and j (i < j) are considered interchangeable if they have the same
10+
* width-to-height ratio. More formally, two rectangles are interchangeable if
11+
* widthi/heighti == widthj/heightj (using decimal division, not integer division).
12+
*
13+
* Return the number of pairs of interchangeable rectangles in rectangles.
14+
*/
15+
16+
/**
17+
* @param {number[][]} rectangles
18+
* @return {number}
19+
*/
20+
var interchangeableRectangles = function(rectangles) {
21+
const map = new Map();
22+
let result = 0;
23+
24+
for (const [width, height] of rectangles) {
25+
const gcd = (a, b) => b === 0 ? a : gcd(b, a % b);
26+
const divisor = gcd(width, height);
27+
const ratio = `${width / divisor}/${height / divisor}`;
28+
29+
if (map.has(ratio)) {
30+
result += map.get(ratio);
31+
map.set(ratio, map.get(ratio) + 1);
32+
} else {
33+
map.set(ratio, 1);
34+
}
35+
}
36+
37+
return result;
38+
};

0 commit comments

Comments
 (0)