Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1992 from Maunish-dave/2001-number-of-…
Browse files Browse the repository at this point in the history
…pairs-of-interchangeable-rectangles.cpp

Create 2001-number-of-pairs-of-interchangeable.cpp
  • Loading branch information
MHamiid authored Jan 11, 2023
2 parents 77696ee + 2e33cc4 commit f40aa78
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 f40aa78

Please sign in to comment.