Skip to content

Commit e3e2475

Browse files
add 1128
1 parent f7fe2e3 commit e3e2475

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ _If you like this project, please leave me a star._ ★
2626
|1150|[Check If a Number Is Majority Element in a Sorted Array](https://leetcode.com/problems/check-if-a-number-is-majority-element-in-a-sorted-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1150.java) | | | |Easy||
2727
|1137|[N-th Tribonacci Number](https://leetcode.com/problems/n-th-tribonacci-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1137.java) | O(n) | O(n) | |Easy||
2828
|1134|[Armstrong Number](https://leetcode.com/problems/armstrong-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1134.java) | | | [:tv:](https://www.youtube.com/watch?v=HTL7fd4HPf4)|Easy||
29+
|1128|[Number of Equivalent Domino Pairs](https://leetcode.com/problems/number-of-equivalent-domino-pairs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1128.java) | | | |Easy||
2930
|1122|[Relative Sort Array](https://leetcode.com/problems/relative-sort-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1122.java) | O(n) | O(n) | |Easy||
3031
|1170|[Compare Strings by Frequency of the Smallest Character](https://leetcode.com/problems/compare-strings-by-frequency-of-the-smallest-character/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1170.java) | | | |Easy||
3132
|1119|[Remove Vowels from a String](https://leetcode.com/problems/remove-vowels-from-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1119.java) | | | [:tv:](https://www.youtube.com/watch?v=6KCBrIWEauw)|Easy||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* 1128. Number of Equivalent Domino Pairs
8+
*
9+
* Given a list of dominoes, dominoes[i] = [a, b] is equivalent to dominoes[j] = [c, d] if and only if either (a==c and b==d), or (a==d and b==c) - that is, one domino can be rotated to be equal to another domino.
10+
* Return the number of pairs (i, j) for which 0 <= i < j < dominoes.length, and dominoes[i] is equivalent to dominoes[j].
11+
*
12+
* Example 1:
13+
*
14+
* Input: dominoes = [[1,2],[2,1],[3,4],[5,6]]
15+
* Output: 1
16+
*
17+
* Constraints:
18+
* 1 <= dominoes.length <= 40000
19+
* 1 <= dominoes[i][j] <= 9
20+
* */
21+
public class _1128 {
22+
public static class Solution1 {
23+
public int numEquivDominoPairs(int[][] dominoes) {
24+
int m = dominoes.length;
25+
Map<Integer, Integer> map = new HashMap<>();
26+
int count = 0;
27+
for (int i = 0; i < m; i++) {
28+
int smaller = Math.min(dominoes[i][0], dominoes[i][1]);
29+
int bigger = Math.max(dominoes[i][0], dominoes[i][1]);
30+
int key = smaller * 10 + bigger;
31+
count += map.getOrDefault(key, 0);
32+
map.put(key, map.getOrDefault(key, 0) + 1);
33+
}
34+
return count;
35+
}
36+
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1128;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _1128Test {
10+
private static _1128.Solution1 solution1;
11+
private static int[][] dominoes;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1128.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
dominoes = new int[][]{
21+
{1, 2},
22+
{2, 1},
23+
{3, 4},
24+
{5, 6}
25+
};
26+
assertEquals(1, solution1.numEquivDominoPairs(dominoes));
27+
}
28+
}

0 commit comments

Comments
 (0)