Skip to content

Commit 2e33cc4

Browse files
committed
Create 2001-number-of-pairs-of-interchangeable.cpp
1 parent afb94e9 commit 2e33cc4

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
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)