We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent afb94e9 commit 2e33cc4Copy full SHA for 2e33cc4
cpp/2001-number-of-pairs-of-interchangeable-rectangles.cpp
@@ -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