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

+1-1
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||
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+
}
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)