Skip to content

Commit 2e294a5

Browse files
add 1733
1 parent 66de1d7 commit 2e294a5

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

README.md

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

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1733|[Minimum Number of People to Teach](https://leetcode.com/problems/minimum-number-of-people-to-teach/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1733.java) ||Medium|Array, Greedy|
1112
|1732|[Find the Highest Altitude](https://leetcode.com/problems/find-the-highest-altitude/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1732.java) ||Easy|Array|
1213
|1727|[Largest Submatrix With Rearrangements](https://leetcode.com/problems/largest-submatrix-with-rearrangements/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1727.java) ||Medium|Greedy, Sort|
1314
|1726|[Tuple with Same Product](https://leetcode.com/problems/tuple-with-same-product/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1726.java) ||Medium|Array|
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.*;
4+
5+
public class _1733 {
6+
public static class Solution1 {
7+
public int minimumTeachings(int n, int[][] languages, int[][] friendships) {
8+
Map<Integer, Set<Integer>> langMap = new HashMap<>();
9+
for (int i = 0; i < languages.length; i++) {
10+
Set<Integer> set = new HashSet<>();
11+
langMap.put(i + 1, set);
12+
for (int lang : languages[i]) {
13+
set.add(lang);
14+
}
15+
}
16+
boolean[] canCommunicate = new boolean[friendships.length];
17+
for (int i = 1; i <= n; i++) {
18+
for (int j = 0; j < friendships.length; j++) {
19+
int friend1 = friendships[j][0];
20+
int friend2 = friendships[j][1];
21+
if (langMap.get(friend1).contains(i) && langMap.get(friend2).contains(i)) {
22+
canCommunicate[j] = true;
23+
}
24+
}
25+
}
26+
int minTeach = friendships.length;
27+
for (int i = 1; i <= n; i++) {
28+
Set<Integer> teach = new HashSet<>();
29+
for (int j = 0; j < friendships.length; j++) {
30+
if (!canCommunicate[j]) {
31+
int friend1 = friendships[j][0];
32+
int friend2 = friendships[j][1];
33+
if (!langMap.get(friend1).contains(i)) {
34+
teach.add(friend1);
35+
}
36+
if (!langMap.get(friend2).contains(i)) {
37+
teach.add(friend2);
38+
}
39+
}
40+
}
41+
minTeach = Math.min(minTeach, teach.size());
42+
}
43+
return minTeach;
44+
}
45+
}
46+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1733;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1733Test {
10+
private static _1733.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1733.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(1, solution1.minimumTeachings(2, new int[][]{
20+
{1}, {2}, {1, 2}
21+
}, new int[][]{
22+
{1, 2}, {1, 3}, {2, 3}
23+
}));
24+
}
25+
26+
@Test
27+
public void test2() {
28+
assertEquals(2, solution1.minimumTeachings(3, new int[][]{
29+
{2}, {1, 3}, {1, 2}, {3}
30+
}, new int[][]{
31+
{1, 4}, {1, 2}, {3, 4}, {2, 3}
32+
}));
33+
}
34+
35+
}

0 commit comments

Comments
 (0)