Skip to content

Commit d5c2a32

Browse files
add 1577
1 parent d3b69ba commit d5c2a32

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ _If you like this project, please leave me a star._ ★
8585
|1588|[Sum of All Odd Length Subarrays](https://leetcode.com/problems/sum-of-all-odd-length-subarrays/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1588.java) ||Easy|Array|
8686
|1583|[Count Unhappy Friends](https://leetcode.com/problems/count-unhappy-friends/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1583.java) ||Medium|Array|
8787
|1582|[Special Positions in a Binary Matrix](https://leetcode.com/problems/special-positions-in-a-binary-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1582.java) ||Easy|Array|
88+
|1577|[Number of Ways Where Square of Number Is Equal to Product of Two Numbers](https://leetcode.com/problems/number-of-ways-where-square-of-number-is-equal-to-product-of-two-numbers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1577.java) ||Medium|HashTable, Math|
8889
|1576|[Replace All ?'s to Avoid Consecutive Repeating Characters](https://leetcode.com/problems/replace-all-s-to-avoid-consecutive-repeating-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1576.java) |[:tv:](https://youtu.be/SJBDLYqrIsM)|Easy|String|
8990
|1574|[Shortest Subarray to be Removed to Make Array Sorted](https://leetcode.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1574.java) ||Medium|Array, Binary Search|
9091
|1572|[Matrix Diagonal Sum](https://leetcode.com/problems/matrix-diagonal-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1572.java) ||Easy|Array|
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class _1577 {
7+
public static class Solution1 {
8+
public int numTriplets(int[] nums1, int[] nums2) {
9+
long result = 0;
10+
for (long num : nums1) {
11+
result += twoProduct(num * num, nums2);
12+
}
13+
for (long num : nums2) {
14+
result += twoProduct(num * num, nums1);
15+
}
16+
return (int) result;
17+
}
18+
19+
private long twoProduct(long product, int[] nums) {
20+
Map<Long, Long> map = new HashMap<>();
21+
long count = 0;
22+
for (long num : nums) {
23+
if (product % num == 0) {
24+
count += map.getOrDefault(product / num, 0l);
25+
}
26+
map.put(num, map.getOrDefault(num, 0l) + 1);
27+
}
28+
return count;
29+
}
30+
31+
}
32+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1577;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1577Test {
10+
private static _1577.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1577.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(1, solution1.numTriplets(new int[]{7, 4}, new int[]{5, 2, 8, 9}));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(9, solution1.numTriplets(new int[]{1, 1}, new int[]{1, 1, 1}));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(2, solution1.numTriplets(new int[]{7, 7, 8, 3}, new int[]{1, 2, 9, 7}));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(0, solution1.numTriplets(new int[]{4, 7, 9, 11, 23}, new int[]{3, 5, 1024, 12, 18}));
35+
}
36+
37+
@Test
38+
public void test5() {
39+
assertEquals(4, solution1.numTriplets(new int[]{3, 1, 2, 2}, new int[]{1, 3, 4, 4}));
40+
}
41+
42+
@Test
43+
public void test6() {
44+
assertEquals(5, solution1.numTriplets(new int[]{4, 1, 4, 1, 12}, new int[]{3, 2, 5, 4}));
45+
}
46+
47+
@Test
48+
public void test7() {
49+
assertEquals(234, solution1.numTriplets(new int[]{14, 1, 1, 12, 7, 12, 10, 4, 11, 10, 5, 2, 5, 14, 7, 9, 10, 13, 15, 6, 9, 12, 6, 12, 4, 10, 9, 12, 11}, new int[]{3, 12, 1, 9, 1, 12, 4, 12, 4, 1, 7, 10, 7, 11, 4, 13, 4, 11, 5, 1, 14, 12, 15, 4, 2, 3, 13, 10, 3, 4}));
50+
}
51+
}

0 commit comments

Comments
 (0)