Skip to content

Commit

Permalink
Create 2001-number-of-pairs-of-interchangeable.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Maunish-dave committed Jan 11, 2023
1 parent afb94e9 commit 2e33cc4
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions cpp/2001-number-of-pairs-of-interchangeable-rectangles.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Approach:
Keep the track of the ratios in a hash map
Time complexity : O(n)
Space complexity: O(n)
n is number of rectangles
*/

class Solution {
public:
long long interchangeableRectangles(vector<vector<int>>& rectangles) {

map<long double,int> hash;
long double ratio;

long long answer=0;

for(int i=0;i<rectangles.size();i++){
ratio = (long double)(rectangles[i][0])/
(long double)(rectangles[i][1]);

if(hash.find(ratio)!=hash.end()){
hash[ratio]++;
}
else{
hash[ratio] = 1;
}
}

for(auto it:hash){
answer+= (long long)(it.second)*(long long)(it.second-1)/2;
}

return answer;
}
};

0 comments on commit 2e33cc4

Please sign in to comment.