Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2e33cc4

Browse files
committed
Create 2001-number-of-pairs-of-interchangeable.cpp
1 parentafb94e9 commit2e33cc4

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
classSolution {
12+
public:
13+
longlonginterchangeableRectangles(vector<vector<int>>& rectangles) {
14+
15+
map<longdouble,int> hash;
16+
longdouble ratio;
17+
18+
longlong answer=0;
19+
20+
for(int i=0;i<rectangles.size();i++){
21+
ratio = (longdouble)(rectangles[i][0])/
22+
(longdouble)(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+= (longlong)(it.second)*(longlong)(it.second-1)/2;
34+
}
35+
36+
return answer;
37+
}
38+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp