Skip to content

Commit 078a2ca

Browse files
add Java solution for 2001
1 parent 324c6bd commit 078a2ca

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11-
|2001|[Number of Pairs of Interchangeable Rectangles](https://leetcode.com/problems/number-of-pairs-of-interchangeable-rectangles/)|[Python3](../master/python3/2001.py) ||Medium||
11+
|2001|[Number of Pairs of Interchangeable Rectangles](https://leetcode.com/problems/number-of-pairs-of-interchangeable-rectangles/)|[Python3](../master/python3/2001.py), [Java](../master/src/main/java/com/fishercoder/solutions/_2001.java) ||Medium||
1212
|2000|[Reverse Prefix of Word](https://leetcode.com/problems/reverse-prefix-of-word/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_2000.java) ||Easy||
1313
|1996|[The Number of Weak Characters in the Game](https://leetcode.com/problems/the-number-of-weak-characters-in-the-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1996.java) ||Medium||
1414
|1995|[Count Special Quadruplets](https://leetcode.com/problems/count-special-quadruplets/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1995.java) ||Easy||
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class _2001 {
7+
public static class Solution1 {
8+
/**
9+
* credit: https://github.com/fishercoder1534/Leetcode/blob/master/python3/2001.py
10+
*/
11+
public long interchangeableRectangles(int[][] rectangles) {
12+
Map<Double, Integer> map = new HashMap<>();
13+
for (int[] rec : rectangles) {
14+
double ratio = (double) rec[0] / rec[1];
15+
map.put(ratio, map.getOrDefault(ratio, 0) + 1);
16+
}
17+
long answer = 0;
18+
for (double ratio : map.keySet()) {
19+
int count = map.get(ratio);
20+
answer += (long) count * (count - 1) / 2;
21+
}
22+
return answer;
23+
}
24+
}
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions._2001;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class _2001Test {
11+
private static _2001.Solution1 solution1;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _2001.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
assertEquals(6, solution1.interchangeableRectangles(CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[4,8],[3,6],[10,20],[15,30]")));
21+
}
22+
}

0 commit comments

Comments
 (0)
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