Skip to content

Commit 5ca5cd9

Browse files
authored
Added tasks 2368, 2369, 2370.
1 parent 4a5bb94 commit 5ca5cd9

File tree

9 files changed

+312
-0
lines changed

9 files changed

+312
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package g2301_2400.s2368_reachable_nodes_with_restrictions;
2+
3+
// #Medium #Array #Tree #Graph #Hash_Table #Depth_First_Search #Breadth_First_Search
4+
// #2022_08_15_Time_88_ms_(42.86%)_Space_201.1_MB_(14.29%)
5+
6+
import java.util.LinkedList;
7+
8+
public class Solution {
9+
public int reachableNodes(int n, int[][] edges, int[] restricted) {
10+
LinkedList<Integer>[] adj = new LinkedList[n];
11+
for (int i = 0; i < n - 1; i++) {
12+
if (adj[edges[i][0]] == null) {
13+
LinkedList<Integer> source = new LinkedList<>();
14+
source.add(edges[i][1]);
15+
adj[edges[i][0]] = source;
16+
} else {
17+
adj[edges[i][0]].add(edges[i][1]);
18+
}
19+
if (adj[edges[i][1]] == null) {
20+
LinkedList<Integer> dest = new LinkedList<>();
21+
dest.add(edges[i][0]);
22+
adj[edges[i][1]] = dest;
23+
} else {
24+
adj[edges[i][1]].add(edges[i][0]);
25+
}
26+
}
27+
boolean[] visited = new boolean[n];
28+
for (int res : restricted) {
29+
visited[res] = true;
30+
}
31+
int count = 1;
32+
visited[0] = true;
33+
return countReachableNodes(0, adj, visited, count);
34+
}
35+
36+
private int countReachableNodes(
37+
int source, LinkedList<Integer>[] adj, boolean[] visited, int count) {
38+
for (int neighbour : adj[source]) {
39+
if (!visited[neighbour]) {
40+
visited[neighbour] = true;
41+
count = countReachableNodes(neighbour, adj, visited, ++count);
42+
}
43+
}
44+
return count;
45+
}
46+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2368\. Reachable Nodes With Restrictions
2+
3+
Medium
4+
5+
There is an undirected tree with `n` nodes labeled from `0` to `n - 1` and `n - 1` edges.
6+
7+
You are given a 2D integer array `edges` of length `n - 1` where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given an integer array `restricted` which represents **restricted** nodes.
8+
9+
Return _the **maximum** number of nodes you can reach from node_ `0` _without visiting a restricted node._
10+
11+
Note that node `0` will **not** be a restricted node.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2022/06/15/ex1drawio.png)
16+
17+
**Input:** n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5]
18+
19+
**Output:** 4
20+
21+
**Explanation:** The diagram above shows the tree. We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
22+
23+
**Example 2:**
24+
25+
![](https://assets.leetcode.com/uploads/2022/06/15/ex2drawio.png)
26+
27+
**Input:** n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1]
28+
29+
**Output:** 3
30+
31+
**Explanation:** The diagram above shows the tree. We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
32+
33+
**Constraints:**
34+
35+
* <code>2 <= n <= 10<sup>5</sup></code>
36+
* `edges.length == n - 1`
37+
* `edges[i].length == 2`
38+
* <code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code>
39+
* <code>a<sub>i</sub> != b<sub>i</sub></code>
40+
* `edges` represents a valid tree.
41+
* `1 <= restricted.length < n`
42+
* `1 <= restricted[i] < n`
43+
* All the values of `restricted` are **unique**.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package g2301_2400.s2369_check_if_there_is_a_valid_partition_for_the_array;
2+
3+
// #Medium #Array #Dynamic_Programming #2022_08_15_Time_7_ms_(81.82%)_Space_84.8_MB_(27.27%)
4+
5+
public class Solution {
6+
public boolean validPartition(int[] nums) {
7+
boolean[] canPartition = new boolean[nums.length + 1];
8+
canPartition[0] = true;
9+
int diff = nums[1] - nums[0];
10+
boolean equal = diff == 0;
11+
boolean incOne = diff == 1;
12+
canPartition[2] = equal;
13+
int count = canPartition[2] ? 0 : 2;
14+
for (int i = 3; i < canPartition.length; i++) {
15+
diff = nums[i - 1] - nums[i - 2];
16+
if (diff == 0) {
17+
canPartition[i] = canPartition[i - 2] || (equal && canPartition[i - 3]);
18+
equal = true;
19+
incOne = false;
20+
} else if (diff == 1) {
21+
canPartition[i] = incOne && canPartition[i - 3];
22+
equal = false;
23+
incOne = true;
24+
} else if (canPartition[i - 1]) {
25+
equal = false;
26+
incOne = false;
27+
} else {
28+
return false;
29+
}
30+
31+
if (canPartition[i]) {
32+
count = 0;
33+
} else if (count == 2) {
34+
return false;
35+
} else {
36+
count++;
37+
}
38+
}
39+
return canPartition[nums.length];
40+
}
41+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2369\. Check if There is a Valid Partition For The Array
2+
3+
Medium
4+
5+
You are given a **0-indexed** integer array `nums`. You have to partition the array into one or more **contiguous** subarrays.
6+
7+
We call a partition of the array **valid** if each of the obtained subarrays satisfies **one** of the following conditions:
8+
9+
1. The subarray consists of **exactly** `2` equal elements. For example, the subarray `[2,2]` is good.
10+
2. The subarray consists of **exactly** `3` equal elements. For example, the subarray `[4,4,4]` is good.
11+
3. The subarray consists of **exactly** `3` consecutive increasing elements, that is, the difference between adjacent elements is `1`. For example, the subarray `[3,4,5]` is good, but the subarray `[1,3,5]` is not.
12+
13+
Return `true` _if the array has **at least** one valid partition_. Otherwise, return `false`.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [4,4,4,5,6]
18+
19+
**Output:** true
20+
21+
**Explanation:** The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true.
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [1,1,1,2]
26+
27+
**Output:** false
28+
29+
**Explanation:** There is no valid partition for this array.
30+
31+
**Constraints:**
32+
33+
* <code>2 <= nums.length <= 10<sup>5</sup></code>
34+
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package g2301_2400.s2370_longest_ideal_subsequence;
2+
3+
// #Medium #String #Hash_Table #Dynamic_Programming
4+
// #2022_08_15_Time_28_ms_(85.71%)_Space_48.6_MB_(85.71%)
5+
6+
@SuppressWarnings("java:S135")
7+
public class Solution {
8+
public int longestIdealString(String s, int k) {
9+
int ans = 1;
10+
int[] array = new int[26];
11+
for (int i = 0; i < s.length(); i++) {
12+
int curr = s.charAt(i) - 'a';
13+
int currans = 1;
14+
int temp = k;
15+
array[curr] += 1;
16+
int j = curr - 1;
17+
while (temp > 0) {
18+
if (j == -1) {
19+
break;
20+
}
21+
currans = Math.max(currans, array[j] + 1);
22+
temp--;
23+
if (j == 0) {
24+
break;
25+
}
26+
j--;
27+
}
28+
temp = k;
29+
j = curr + 1;
30+
while (temp > 0) {
31+
if (j == 26) {
32+
break;
33+
}
34+
currans = Math.max(currans, array[j] + 1);
35+
temp--;
36+
if (j == 25) {
37+
break;
38+
}
39+
j++;
40+
}
41+
array[curr] = Math.max(currans, array[curr]);
42+
ans = Math.max(ans, array[curr]);
43+
}
44+
return ans;
45+
}
46+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2370\. Longest Ideal Subsequence
2+
3+
Medium
4+
5+
You are given a string `s` consisting of lowercase letters and an integer `k`. We call a string `t` **ideal** if the following conditions are satisfied:
6+
7+
* `t` is a **subsequence** of the string `s`.
8+
* The absolute difference in the alphabet order of every two **adjacent** letters in `t` is less than or equal to `k`.
9+
10+
Return _the length of the **longest** ideal string_.
11+
12+
A **subsequence** is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.
13+
14+
**Note** that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of `'a'` and `'z'` is `25`, not `1`.
15+
16+
**Example 1:**
17+
18+
**Input:** s = "acfgbd", k = 2
19+
20+
**Output:** 4
21+
22+
**Explanation:** The longest ideal string is "acbd". The length of this string is 4, so 4 is returned. Note that "acfgbd" is not ideal because 'c' and 'f' have a difference of 3 in alphabet order.
23+
24+
**Example 2:**
25+
26+
**Input:** s = "abcd", k = 3
27+
28+
**Output:** 4
29+
30+
**Explanation:** The longest ideal string is "abcd". The length of this string is 4, so 4 is returned.
31+
32+
**Constraints:**
33+
34+
* <code>1 <= s.length <= 10<sup>5</sup></code>
35+
* `0 <= k <= 25`
36+
* `s` consists of lowercase English letters.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g2301_2400.s2368_reachable_nodes_with_restrictions;
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 reachableNodes() {
11+
assertThat(
12+
new Solution()
13+
.reachableNodes(
14+
7,
15+
new int[][] {{0, 1}, {1, 2}, {3, 1}, {4, 0}, {0, 5}, {5, 6}},
16+
new int[] {4, 5}),
17+
equalTo(4));
18+
}
19+
20+
@Test
21+
void reachableNodes2() {
22+
assertThat(
23+
new Solution()
24+
.reachableNodes(
25+
7,
26+
new int[][] {{0, 1}, {0, 2}, {0, 5}, {0, 4}, {3, 2}, {6, 5}},
27+
new int[] {4, 2, 1}),
28+
equalTo(3));
29+
}
30+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2301_2400.s2369_check_if_there_is_a_valid_partition_for_the_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 validPartition() {
11+
assertThat(new Solution().validPartition(new int[] {4, 4, 4, 5, 6}), equalTo(true));
12+
}
13+
14+
@Test
15+
void validPartition2() {
16+
assertThat(new Solution().validPartition(new int[] {1, 1, 1, 2}), equalTo(false));
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2301_2400.s2370_longest_ideal_subsequence;
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 longestIdealString() {
11+
assertThat(new Solution().longestIdealString("acfgbd", 2), equalTo(4));
12+
}
13+
14+
@Test
15+
void longestIdealString2() {
16+
assertThat(new Solution().longestIdealString("abcd", 3), equalTo(4));
17+
}
18+
}

0 commit comments

Comments
 (0)