Skip to content

Commit f40aa78

Browse files
authored
Merge pull request neetcode-gh#1992 from Maunish-dave/2001-number-of-pairs-of-interchangeable-rectangles.cpp
Create 2001-number-of-pairs-of-interchangeable.cpp
2 parents 77696ee + 2e33cc4 commit f40aa78

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Approach:
3+
Keep the track of the ratios in a hash map
4+
5+
Time complexity : O(n)
6+
Space complexity: O(n)
7+
8+
n is number of rectangles
9+
*/
10+
11+
class Solution {
12+
public:
13+
long long interchangeableRectangles(vector<vector<int>>& rectangles) {
14+
15+
map<long double,int> hash;
16+
long double ratio;
17+
18+
long long answer=0;
19+
20+
for(int i=0;i<rectangles.size();i++){
21+
ratio = (long double)(rectangles[i][0])/
22+
(long double)(rectangles[i][1]);
23+
24+
if(hash.find(ratio)!=hash.end()){
25+
hash[ratio]++;
26+
}
27+
else{
28+
hash[ratio] = 1;
29+
}
30+
}
31+
32+
for(auto it:hash){
33+
answer+= (long long)(it.second)*(long long)(it.second-1)/2;
34+
}
35+
36+
return answer;
37+
}
38+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy