forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request neetcode-gh#1992 from Maunish-dave/2001-number-of-…
…pairs-of-interchangeable-rectangles.cpp Create 2001-number-of-pairs-of-interchangeable.cpp
- Loading branch information
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
cpp/2001-number-of-pairs-of-interchangeable-rectangles.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |