Skip to content

Create 2001-number-of-pairs-of-interchangeable.cpp #1992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
};
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