Skip to content

Commit

Permalink
Solution to today's problem
Browse files Browse the repository at this point in the history
  • Loading branch information
spannm committed Feb 24, 2024
1 parent f00f1f6 commit fab4f58
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
35 changes: 13 additions & 22 deletions src/main/java/io/github/spannm/leetcode/lc2/lc2000/Problem2092.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,39 @@
import io.github.spannm.leetcode.LeetcodeProblem;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
* 2092. Find All People With Secret.
*/
public class Problem2092 extends LeetcodeProblem {

public List<Integer> findAllPeople(int n, int[][] meetings, int firstPerson) {
boolean[] vis = new boolean[n];
public List<Integer> findAllPeople(int _n, int[][] _meetings, int _first) {
boolean[] vis = new boolean[_n];
vis[0] = true;
vis[firstPerson] = true;
int m = meetings.length;
Arrays.sort(meetings, Comparator.comparingInt(a -> a[2]));
vis[_first] = true;
int m = _meetings.length;
Arrays.sort(_meetings, Comparator.comparingInt(a -> a[2]));
for (int i = 0; i < m;) {
int j = i;
while (j + 1 < m && meetings[j + 1][2] == meetings[i][2]) {
while (j + 1 < m && _meetings[j + 1][2] == _meetings[i][2]) {
j++;
}
Map<Integer, List<Integer>> g = new HashMap<>();
Set<Integer> s = new HashSet<>();
for (int k = i; k <= j; k++) {
int x = meetings[k][0];
int y = meetings[k][1];
int x = _meetings[k][0];
int y = _meetings[k][1];
g.computeIfAbsent(x, key -> new ArrayList<>()).add(y);
g.computeIfAbsent(y, key -> new ArrayList<>()).add(x);
s.add(x);
s.add(y);
}
Deque<Integer> q = new ArrayDeque<>();
for (int u : s) {
if (vis[u]) {
q.offer(u);
}
}
Deque<Integer> q = s.stream().filter(u -> vis[u]).collect(Collectors.toCollection(ArrayDeque::new));
while (!q.isEmpty()) {
int u = q.poll();
for (int v : g.getOrDefault(u, Collections.emptyList())) {
for (int v : g.getOrDefault(u, List.of())) {
if (!vis[v]) {
vis[v] = true;
q.offer(v);
Expand All @@ -47,13 +44,7 @@ public List<Integer> findAllPeople(int n, int[][] meetings, int firstPerson) {
}
i = j + 1;
}
List<Integer> ans = new ArrayList<>();
for (int i = 0; i < n; i++) {
if (vis[i]) {
ans.add(i);
}
}
return ans;
return IntStream.range(0, _n).filter(i -> vis[i]).boxed().collect(Collectors.toList());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.github.spannm.leetcode.lc2.lc2000;

import io.github.spannm.leetcode.LeetcodeBaseTest;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.converter.ConvertWith;
import org.junit.jupiter.params.provider.CsvSource;

import java.util.List;

class Problem2092Test extends LeetcodeBaseTest {

@ParameterizedTest(name = "[{index}] {0}; {1}; {2} --> {3}")
@CsvSource(delimiter = ';', value = {
"6; [1,2,5],[2,3,8],[1,5,10]; 1; 0,1,2,3,5",
"4; [3,1,3],[1,2,2],[0,3,3]; 3; 0,1,3",
"5; [3,4,2],[1,2,1],[2,3,1]; 1; 0,1,2,3,4"
})
void test(int _n,
@ConvertWith(CsvToIntMatrix.class) int[][] _meetings,
int _first,
@ConvertWith(CsvToIntegerList.class) List<Integer> _expectedResult) {
assertEquals(_expectedResult, new Problem2092().findAllPeople(_n, _meetings, _first));
}

}

0 comments on commit fab4f58

Please sign in to comment.