Skip to content

Commit 077db33

Browse files
authored
Added tasks 2091, 2092.
1 parent 84998cb commit 077db33

File tree

6 files changed

+319
-0
lines changed

6 files changed

+319
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g2001_2100.s2091_removing_minimum_and_maximum_from_array;
2+
3+
// #Medium #Array #Greedy #2022_05_27_Time_2_ms_(100.00%)_Space_84.4_MB_(40.48%)
4+
5+
public class Solution {
6+
public int minimumDeletions(int[] nums) {
7+
int n = nums.length;
8+
int min = Integer.MAX_VALUE;
9+
int max = Integer.MIN_VALUE;
10+
int minIndex = 0;
11+
int maxIndex = 0;
12+
for (int i = 0; i < nums.length; i++) {
13+
if (nums[i] < min) {
14+
min = nums[i];
15+
minIndex = i;
16+
}
17+
if (nums[i] > max) {
18+
max = nums[i];
19+
maxIndex = i;
20+
}
21+
}
22+
int firstCase = 0;
23+
int secondCase = 0;
24+
int thirdCase = 0;
25+
if (minIndex > maxIndex) {
26+
firstCase = minIndex + 1;
27+
secondCase = n - maxIndex;
28+
thirdCase = maxIndex + 1 + (n - minIndex);
29+
} else {
30+
firstCase = maxIndex + 1;
31+
secondCase = n - minIndex;
32+
thirdCase = minIndex + 1 + (n - maxIndex);
33+
}
34+
return Math.min(firstCase, Math.min(secondCase, thirdCase));
35+
}
36+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2091\. Removing Minimum and Maximum From Array
2+
3+
Medium
4+
5+
You are given a **0-indexed** array of **distinct** integers `nums`.
6+
7+
There is an element in `nums` that has the **lowest** value and an element that has the **highest** value. We call them the **minimum** and **maximum** respectively. Your goal is to remove **both** these elements from the array.
8+
9+
A **deletion** is defined as either removing an element from the **front** of the array or removing an element from the **back** of the array.
10+
11+
Return _the **minimum** number of deletions it would take to remove **both** the minimum and maximum element from the array._
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [2,**10**,7,5,4,**1**,8,6]
16+
17+
**Output:** 5
18+
19+
**Explanation:**
20+
21+
The minimum element in the array is nums[5], which is 1.
22+
23+
The maximum element in the array is nums[1], which is 10.
24+
25+
We can remove both the minimum and maximum by removing 2 elements from the front and 3 elements from the back.
26+
27+
This results in 2 + 3 = 5 deletions, which is the minimum number possible.
28+
29+
**Example 2:**
30+
31+
**Input:** nums = [0,**\-4**,**19**,1,8,-2,-3,5]
32+
33+
**Output:** 3
34+
35+
**Explanation:**
36+
37+
The minimum element in the array is nums[1], which is -4.
38+
39+
The maximum element in the array is nums[2], which is 19.
40+
41+
We can remove both the minimum and maximum by removing 3 elements from the front.
42+
43+
This results in only 3 deletions, which is the minimum number possible.
44+
45+
**Example 3:**
46+
47+
**Input:** nums = [**101**]
48+
49+
**Output:** 1
50+
51+
**Explanation:**
52+
53+
There is only one element in the array, which makes it both the minimum and maximum element.
54+
55+
We can remove it with 1 deletion.
56+
57+
**Constraints:**
58+
59+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
60+
* <code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code>
61+
* The integers in `nums` are **distinct**.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package g2001_2100.s2092_find_all_people_with_secret;
2+
3+
// #Hard #Depth_First_Search #Sorting #Breadth_First_Search #Graph #Union_Find
4+
// #2022_05_27_Time_95_ms_(84.86%)_Space_99.7_MB_(87.33%)
5+
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.HashSet;
9+
import java.util.List;
10+
import java.util.Set;
11+
12+
public class Solution {
13+
public List<Integer> findAllPeople(int n, int[][] meetings, int firstPerson) {
14+
Arrays.sort(meetings, ((a, b) -> a[2] - b[2]));
15+
UF uf = new UF(n);
16+
// base
17+
uf.union(0, firstPerson);
18+
// for every time we have a pool of people that talk to each other
19+
// if someone knows a secret proir to this meeting - all pool will too
20+
// if not - reset unions from this pool
21+
int i = 0;
22+
while (i < meetings.length) {
23+
int curTime = meetings[i][2];
24+
Set<Integer> pool = new HashSet<>();
25+
while (i < meetings.length && curTime == meetings[i][2]) {
26+
int[] currentMeeting = meetings[i];
27+
uf.union(currentMeeting[0], currentMeeting[1]);
28+
pool.add(currentMeeting[0]);
29+
pool.add(currentMeeting[1]);
30+
i++;
31+
}
32+
// meeting that took place now should't affect future
33+
// meetings if people don't know the secret
34+
for (int j : pool) {
35+
if (!uf.connected(0, j)) {
36+
uf.reset(j);
37+
}
38+
}
39+
}
40+
// if the person is conneted to 0 - they know a secret
41+
List<Integer> ans = new ArrayList<>();
42+
for (int j = 0; j < n; j++) {
43+
if (uf.connected(j, 0)) {
44+
ans.add(j);
45+
}
46+
}
47+
return ans;
48+
}
49+
50+
// regular union find
51+
private static class UF {
52+
private int[] parent;
53+
private int[] rank;
54+
55+
public UF(int n) {
56+
parent = new int[n];
57+
rank = new int[n];
58+
for (int i = 0; i < n; i++) {
59+
parent[i] = i;
60+
}
61+
}
62+
63+
public void union(int p, int q) {
64+
int rootP = find(p);
65+
int rootQ = find(q);
66+
if (rootP == rootQ) {
67+
return;
68+
}
69+
if (rank[rootP] < rank[rootQ]) {
70+
parent[rootP] = rootQ;
71+
} else {
72+
parent[rootQ] = rootP;
73+
rank[rootP]++;
74+
}
75+
}
76+
77+
public int find(int p) {
78+
while (parent[p] != p) {
79+
p = parent[parent[p]];
80+
}
81+
return p;
82+
}
83+
84+
public boolean connected(int p, int q) {
85+
return find(p) == find(q);
86+
}
87+
88+
public void reset(int p) {
89+
parent[p] = p;
90+
rank[p] = 0;
91+
}
92+
}
93+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
2092\. Find All People With Secret
2+
3+
Hard
4+
5+
You are given an integer `n` indicating there are `n` people numbered from `0` to `n - 1`. You are also given a **0-indexed** 2D integer array `meetings` where <code>meetings[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> indicates that person <code>x<sub>i</sub></code> and person <code>y<sub>i</sub></code> have a meeting at <code>time<sub>i</sub></code>. A person may attend **multiple meetings** at the same time. Finally, you are given an integer `firstPerson`.
6+
7+
Person `0` has a **secret** and initially shares the secret with a person `firstPerson` at time `0`. This secret is then shared every time a meeting takes place with a person that has the secret. More formally, for every meeting, if a person <code>x<sub>i</sub></code> has the secret at <code>time<sub>i</sub></code>, then they will share the secret with person <code>y<sub>i</sub></code>, and vice versa.
8+
9+
The secrets are shared **instantaneously**. That is, a person may receive the secret and share it with people in other meetings within the same time frame.
10+
11+
Return _a list of all the people that have the secret after all the meetings have taken place._ You may return the answer in **any order**.
12+
13+
**Example 1:**
14+
15+
**Input:** n = 6, meetings = [[1,2,5],[2,3,8],[1,5,10]], firstPerson = 1
16+
17+
**Output:** [0,1,2,3,5]
18+
19+
**Explanation:**
20+
21+
At time 0, person 0 shares the secret with person 1.
22+
23+
At time 5, person 1 shares the secret with person 2.
24+
25+
At time 8, person 2 shares the secret with person 3.
26+
27+
At time 10, person 1 shares the secret with person 5.
28+
29+
Thus, people 0, 1, 2, 3, and 5 know the secret after all the meetings.
30+
31+
**Example 2:**
32+
33+
**Input:** n = 4, meetings = [[3,1,3],[1,2,2],[0,3,3]], firstPerson = 3
34+
35+
**Output:** [0,1,3]
36+
37+
**Explanation:**
38+
39+
At time 0, person 0 shares the secret with person 3.
40+
41+
At time 2, neither person 1 nor person 2 know the secret.
42+
43+
At time 3, person 3 shares the secret with person 0 and person 1.
44+
45+
Thus, people 0, 1, and 3 know the secret after all the meetings.
46+
47+
**Example 3:**
48+
49+
**Input:** n = 5, meetings = [[3,4,2],[1,2,1],[2,3,1]], firstPerson = 1
50+
51+
**Output:** [0,1,2,3,4]
52+
53+
**Explanation:**
54+
55+
At time 0, person 0 shares the secret with person 1.
56+
57+
At time 1, person 1 shares the secret with person 2, and person 2 shares the secret with person 3.
58+
59+
Note that person 2 can share the secret at the same time as receiving it.
60+
61+
At time 2, person 3 shares the secret with person 4.
62+
63+
Thus, people 0, 1, 2, 3, and 4 know the secret after all the meetings.
64+
65+
**Constraints:**
66+
67+
* <code>2 <= n <= 10<sup>5</sup></code>
68+
* <code>1 <= meetings.length <= 10<sup>5</sup></code>
69+
* `meetings[i].length == 3`
70+
* <code>0 <= x<sub>i</sub>, y<sub>i</sub> <= n - 1</code>
71+
* <code>x<sub>i</sub> != y<sub>i</sub></code>
72+
* <code>1 <= time<sub>i</sub> <= 10<sup>5</sup></code>
73+
* `1 <= firstPerson <= n - 1`
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g2001_2100.s2091_removing_minimum_and_maximum_from_array;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void minimumDeletions() {
11+
assertThat(
12+
new Solution().minimumDeletions(new int[] {2, 10, 7, 5, 4, 1, 8, 6}), equalTo(5));
13+
}
14+
15+
@Test
16+
void minimumDeletions2() {
17+
assertThat(
18+
new Solution().minimumDeletions(new int[] {0, -4, 19, 1, 8, -2, -3, 5}),
19+
equalTo(3));
20+
}
21+
22+
@Test
23+
void minimumDeletions3() {
24+
assertThat(new Solution().minimumDeletions(new int[] {101}), equalTo(1));
25+
}
26+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g2001_2100.s2092_find_all_people_with_secret;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import java.util.Arrays;
7+
import org.junit.jupiter.api.Test;
8+
9+
class SolutionTest {
10+
@Test
11+
void findAllPeople() {
12+
assertThat(
13+
new Solution().findAllPeople(6, new int[][] {{1, 2, 5}, {2, 3, 8}, {1, 5, 10}}, 1),
14+
equalTo(Arrays.asList(0, 1, 2, 3, 5)));
15+
}
16+
17+
@Test
18+
void findAllPeople2() {
19+
assertThat(
20+
new Solution().findAllPeople(4, new int[][] {{3, 1, 3}, {1, 2, 2}, {0, 3, 3}}, 3),
21+
equalTo(Arrays.asList(0, 1, 3)));
22+
}
23+
24+
@Test
25+
void findAllPeople3() {
26+
assertThat(
27+
new Solution().findAllPeople(5, new int[][] {{3, 4, 2}, {1, 2, 1}, {2, 3, 1}}, 1),
28+
equalTo(Arrays.asList(0, 1, 2, 3, 4)));
29+
}
30+
}

0 commit comments

Comments
 (0)