File tree 2 files changed +40
-1
lines changed
2 files changed +40
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,679 LeetCode solutions in JavaScript
1
+ # 1,680 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
1535
1535
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|
1536
1536
1998|[ GCD Sort of an Array] ( ./solutions/1998-gcd-sort-of-an-array.js ) |Hard|
1537
1537
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|
1538
1539
2011|[ Final Value of Variable After Performing Operations] ( ./solutions/2011-final-value-of-variable-after-performing-operations.js ) |Easy|
1539
1540
2016|[ Maximum Difference Between Increasing Elements] ( ./solutions/2016-maximum-difference-between-increasing-elements.js ) |Easy|
1540
1541
2017|[ Grid Game] ( ./solutions/2017-grid-game.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments