Skip to content

Commit 5fcb6ff

Browse files
authored
Added tasks 2321, 2322.
1 parent 7669daf commit 5fcb6ff

File tree

8 files changed

+325
-2
lines changed

8 files changed

+325
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,6 +1485,8 @@ implementation 'com.github.javadev:leetcode-in-java:1.11'
14851485

14861486
| # | Title | Difficulty | Tag | Time, ms | Time, %
14871487
|------|----------------|-------------|-------------|----------|---------
1488+
| 2322 |[Minimum Score After Removals on a Tree](src/main/java/g2301_2400/s2322_minimum_score_after_removals_on_a_tree/Solution.java)| Hard | Array, Bit_Manipulation, Tree, Depth_First_Search | 255 | 70.70
1489+
| 2321 |[Maximum Score Of Spliced Array](src/main/java/g2301_2400/s2321_maximum_score_of_spliced_array/Solution.java)| Hard | Array, Dynamic_Programming | 3 | 99.68
14881490
| 2320 |[Count Number of Ways to Place Houses](src/main/java/g2301_2400/s2320_count_number_of_ways_to_place_houses/Solution.java)| Medium | Dynamic_Programming | 13 | 82.46
14891491
| 2319 |[Check if Matrix Is X-Matrix](src/main/java/g2301_2400/s2319_check_if_matrix_is_x_matrix/Solution.java)| Easy | Array, Matrix | 3 | 53.58
14901492
| 2318 |[Number of Distinct Roll Sequences](src/main/java/g2301_2400/s2318_number_of_distinct_roll_sequences/Solution.java)| Hard | Dynamic_Programming, Memoization | 254 | 91.67
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package g2301_2400.s2321_maximum_score_of_spliced_array;
2+
3+
// #Hard #Array #Dynamic_Programming #2022_06_30_Time_3_ms_(99.68%)_Space_79.3_MB_(84.76%)
4+
5+
public class Solution {
6+
public int maximumsSplicedArray(int[] nums1, int[] nums2) {
7+
int sum1 = 0;
8+
int sum2 = 0;
9+
int n = nums1.length;
10+
for (int num : nums1) {
11+
sum1 += num;
12+
}
13+
for (int num : nums2) {
14+
sum2 += num;
15+
}
16+
if (sum2 > sum1) {
17+
int temp = sum2;
18+
sum2 = sum1;
19+
sum1 = temp;
20+
int[] temparr = nums2;
21+
nums2 = nums1;
22+
nums1 = temparr;
23+
}
24+
// now sum1>=sum2
25+
// maxEndingHere denotes the maximum sum subarray ending at current index(ie. element at
26+
// current index has to be included)
27+
// minEndingHere denotes the minimum sum subarray ending at current index
28+
int maxEndingHere;
29+
int minEndingHere;
30+
int maxSoFar;
31+
int minSoFar;
32+
int currEle;
33+
maxEndingHere = minEndingHere = maxSoFar = minSoFar = nums2[0] - nums1[0];
34+
for (int i = 1; i < n; i++) {
35+
currEle = nums2[i] - nums1[i];
36+
minEndingHere += currEle;
37+
maxEndingHere += currEle;
38+
if (maxEndingHere < currEle) {
39+
maxEndingHere = currEle;
40+
}
41+
if (minEndingHere > currEle) {
42+
minEndingHere = currEle;
43+
}
44+
maxSoFar = Math.max(maxEndingHere, maxSoFar);
45+
minSoFar = Math.min(minEndingHere, minSoFar);
46+
}
47+
// return the maximum of the 2 possibilities dicussed
48+
// also keep care that maxSoFar>=0 and maxSoFar<=0
49+
return Math.max(sum1 + Math.max(maxSoFar, 0), sum2 - Math.min(0, minSoFar));
50+
}
51+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2321\. Maximum Score Of Spliced Array
2+
3+
Hard
4+
5+
You are given two **0-indexed** integer arrays `nums1` and `nums2`, both of length `n`.
6+
7+
You can choose two integers `left` and `right` where `0 <= left <= right < n` and **swap** the subarray `nums1[left...right]` with the subarray `nums2[left...right]`.
8+
9+
* For example, if `nums1 = [1,2,3,4,5]` and `nums2 = [11,12,13,14,15]` and you choose `left = 1` and `right = 2`, `nums1` becomes `[1,**<ins>12,13</ins>**,4,5]` and `nums2` becomes `[11,**<ins>2,3</ins>**,14,15]`.
10+
11+
You may choose to apply the mentioned operation **once** or not do anything.
12+
13+
The **score** of the arrays is the **maximum** of `sum(nums1)` and `sum(nums2)`, where `sum(arr)` is the sum of all the elements in the array `arr`.
14+
15+
Return _the **maximum possible score**_.
16+
17+
A **subarray** is a contiguous sequence of elements within an array. `arr[left...right]` denotes the subarray that contains the elements of `nums` between indices `left` and `right` (**inclusive**).
18+
19+
**Example 1:**
20+
21+
**Input:** nums1 = [60,60,60], nums2 = [10,90,10]
22+
23+
**Output:** 210
24+
25+
**Explanation:** Choosing left = 1 and right = 1, we have nums1 = [60,<ins>**90**</ins>,60] and nums2 = [10,<ins>**60**</ins>,10].
26+
27+
The score is max(sum(nums1), sum(nums2)) = max(210, 80) = 210.
28+
29+
**Example 2:**
30+
31+
**Input:** nums1 = [20,40,20,70,30], nums2 = [50,20,50,40,20]
32+
33+
**Output:** 220
34+
35+
**Explanation:** Choosing left = 3, right = 4, we have nums1 = [20,40,20,<ins>**40,20**</ins>] and nums2 = [50,20,50,<ins>**70,30**</ins>].
36+
37+
The score is max(sum(nums1), sum(nums2)) = max(140, 220) = 220.
38+
39+
**Example 3:**
40+
41+
**Input:** nums1 = [7,11,13], nums2 = [1,1,1]
42+
43+
**Output:** 31
44+
45+
**Explanation:** We choose not to swap any subarray.
46+
47+
The score is max(sum(nums1), sum(nums2)) = max(31, 3) = 31.
48+
49+
**Constraints:**
50+
51+
* `n == nums1.length == nums2.length`
52+
* <code>1 <= n <= 10<sup>5</sup></code>
53+
* <code>1 <= nums1[i], nums2[i] <= 10<sup>4</sup></code>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package g2301_2400.s2322_minimum_score_after_removals_on_a_tree;
2+
3+
// #Hard #Array #Bit_Manipulation #Tree #Depth_First_Search
4+
// #2022_06_30_Time_255_ms_(70.70%)_Space_117.2_MB_(57.14%)
5+
6+
import java.util.ArrayList;
7+
8+
@SuppressWarnings("unchecked")
9+
public class Solution {
10+
private int ans = Integer.MAX_VALUE;
11+
12+
// function to travel 2nd time on the tree and find the second edge to be removed
13+
private int helper(
14+
int src, ArrayList<Integer>[] graph, int[] arr, int par, int block, int xor1, int tot) {
15+
// Setting the value for the current subtree's XOR value
16+
int myXOR = arr[src];
17+
for (int nbr : graph[src]) {
18+
// If the current nbr is niether the parent of this node nor the blocked node , then
19+
// only we'll proceed
20+
if (nbr != par && nbr != block) {
21+
int nbrXOR = helper(nbr, graph, arr, src, block, xor1, tot);
22+
// 'src <----> nbr' is the second edge to be removed
23+
// Getting the XOR value of the current neighbor
24+
int xor2 = nbrXOR;
25+
// The XOR of the remaining component
26+
int xor3 = (tot ^ xor1) ^ xor2;
27+
// Getting the minimum of the three values
28+
int max = Math.max(xor1, Math.max(xor2, xor3));
29+
// Getting the maximum of the three value
30+
int min = Math.min(xor1, Math.min(xor2, xor3));
31+
ans = Math.min(ans, max - min);
32+
// Including the neighbour subtree's XOR value in the XOR value of the subtree
33+
// rooted at src node
34+
myXOR ^= nbrXOR;
35+
}
36+
}
37+
// Returing the XOR value of the current subtree rooted at the src node
38+
return myXOR;
39+
}
40+
41+
// function to travel 1st time on the tree and find the first edge to be removed and
42+
// then block the node at which the edge ends to avoid selecting the same node again
43+
private int dfs(int src, ArrayList<Integer>[] graph, int[] arr, int par, int tot) {
44+
// Setting the value for the current subtree's XOR value
45+
int myXOR = arr[src];
46+
for (int nbr : graph[src]) {
47+
// If the current nbr is not the parent of this node, then only we'll proceed
48+
if (nbr != par) {
49+
// After selecting 'src <----> nbr' as the first edge, we block 'nbr' node and then
50+
// make a call to try all the second edges
51+
int nbrXOR = dfs(nbr, graph, arr, src, tot);
52+
// Calling the helper to find the try all the second edges after blocking the
53+
// current node
54+
helper(0, graph, arr, -1, nbr, nbrXOR, tot);
55+
// Including the neighbour subtree's XOR value in the XOR value of the subtree
56+
// rooted at src node
57+
myXOR ^= nbrXOR;
58+
}
59+
}
60+
// Returing the XOR value of the current subtree rooted at the src node
61+
return myXOR;
62+
}
63+
64+
public int minimumScore(int[] arr, int[][] edges) {
65+
int n = arr.length;
66+
ArrayList<Integer>[] graph = new ArrayList[n];
67+
int tot = 0;
68+
for (int i = 0; i < n; i++) {
69+
// Initializing the graph and finding the total XOR
70+
graph[i] = new ArrayList<>();
71+
tot ^= arr[i];
72+
}
73+
for (int[] edge : edges) {
74+
// adding the edges
75+
int u = edge[0];
76+
int v = edge[1];
77+
graph[u].add(v);
78+
graph[v].add(u);
79+
}
80+
ans = Integer.MAX_VALUE;
81+
dfs(0, graph, arr, -1, tot);
82+
return ans;
83+
}
84+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2322\. Minimum Score After Removals on a Tree
2+
3+
Hard
4+
5+
There is an undirected connected tree with `n` nodes labeled from `0` to `n - 1` and `n - 1` edges.
6+
7+
You are given a **0-indexed** integer array `nums` of length `n` where `nums[i]` represents the value of the <code>i<sup>th</sup></code> node. You are also 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.
8+
9+
Remove two **distinct** edges of the tree to form three connected components. For a pair of removed edges, the following steps are defined:
10+
11+
1. Get the XOR of all the values of the nodes for **each** of the three components respectively.
12+
2. The **difference** between the **largest** XOR value and the **smallest** XOR value is the **score** of the pair.
13+
14+
* For example, say the three components have the node values: `[4,5,7]`, `[1,9]`, and `[3,3,3]`. The three XOR values are `4 ^ 5 ^ 7 = <ins>**6**</ins>`, `1 ^ 9 = <ins>**8**</ins>`, and `3 ^ 3 ^ 3 = <ins>**3**</ins>`. The largest XOR value is `8` and the smallest XOR value is `3`. The score is then `8 - 3 = 5`.
15+
16+
Return _the **minimum** score of any possible pair of edge removals on the given tree_.
17+
18+
**Example 1:**
19+
20+
![](https://assets.leetcode.com/uploads/2022/05/03/ex1drawio.png)
21+
22+
**Input:** nums = [1,5,5,4,11], edges = [[0,1],[1,2],[1,3],[3,4]]
23+
24+
**Output:** 9
25+
26+
**Explanation:** The diagram above shows a way to make a pair of removals.
27+
28+
- The 1<sup>st</sup> component has nodes [1,3,4] with values [5,4,11]. Its XOR value is 5 ^ 4 ^ 11 = 10.
29+
30+
- The 2<sup>nd</sup> component has node [0] with value [1]. Its XOR value is 1 = 1.
31+
32+
- The 3<sup>rd</sup> component has node [2] with value [5]. Its XOR value is 5 = 5.
33+
34+
The score is the difference between the largest and smallest XOR value which is 10 - 1 = 9.
35+
36+
It can be shown that no other pair of removals will obtain a smaller score than 9.
37+
38+
**Example 2:**
39+
40+
![](https://assets.leetcode.com/uploads/2022/05/03/ex2drawio.png)
41+
42+
**Input:** nums = [5,5,2,4,4,2], edges = [[0,1],[1,2],[5,2],[4,3],[1,3]]
43+
44+
**Output:** 0
45+
46+
**Explanation:** The diagram above shows a way to make a pair of removals.
47+
48+
- The 1<sup>st</sup> component has nodes [3,4] with values [4,4]. Its XOR value is 4 ^ 4 = 0.
49+
50+
- The 2<sup>nd</sup> component has nodes [1,0] with values [5,5]. Its XOR value is 5 ^ 5 = 0.
51+
52+
- The 3<sup>rd</sup> component has nodes [2,5] with values [2,2]. Its XOR value is 2 ^ 2 = 0.
53+
54+
The score is the difference between the largest and smallest XOR value which is 0 - 0 = 0.
55+
56+
We cannot obtain a smaller score than 0.
57+
58+
**Constraints:**
59+
60+
* `n == nums.length`
61+
* `3 <= n <= 1000`
62+
* <code>1 <= nums[i] <= 10<sup>8</sup></code>
63+
* `edges.length == n - 1`
64+
* `edges[i].length == 2`
65+
* <code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code>
66+
* <code>a<sub>i</sub> != b<sub>i</sub></code>
67+
* `edges` represents a valid tree.

src/test/java/g1101_1200/s1195_fizz_buzz_multithreaded/FizzBuzzTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void fizzBuzz() throws InterruptedException {
4747
}
4848
})
4949
.start();
50-
TimeUnit.MILLISECONDS.sleep(500);
50+
TimeUnit.MILLISECONDS.sleep(600);
5151
assertThat(fizz[0] > 0, equalTo(true));
5252
}
5353

@@ -90,7 +90,7 @@ void fizzBuzz2() throws InterruptedException {
9090
}
9191
})
9292
.start();
93-
TimeUnit.MILLISECONDS.sleep(500);
93+
TimeUnit.MILLISECONDS.sleep(600);
9494
assertThat(fizz[0] >= 0, equalTo(true));
9595
}
9696
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g2301_2400.s2321_maximum_score_of_spliced_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 maximumsSplicedArray() {
11+
assertThat(
12+
new Solution().maximumsSplicedArray(new int[] {60, 60, 60}, new int[] {10, 90, 10}),
13+
equalTo(210));
14+
}
15+
16+
@Test
17+
void maximumsSplicedArray2() {
18+
assertThat(
19+
new Solution()
20+
.maximumsSplicedArray(
21+
new int[] {20, 40, 20, 70, 30}, new int[] {50, 20, 50, 40, 20}),
22+
equalTo(220));
23+
}
24+
25+
@Test
26+
void maximumsSplicedArray3() {
27+
assertThat(
28+
new Solution().maximumsSplicedArray(new int[] {7, 11, 13}, new int[] {1, 1, 1}),
29+
equalTo(31));
30+
}
31+
32+
@Test
33+
void maximumsSplicedArray4() {
34+
assertThat(
35+
new Solution().maximumsSplicedArray(new int[] {1, 1, 1}, new int[] {7, 11, 13}),
36+
equalTo(31));
37+
}
38+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g2301_2400.s2322_minimum_score_after_removals_on_a_tree;
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 minimumScore() {
11+
assertThat(
12+
new Solution()
13+
.minimumScore(
14+
new int[] {1, 5, 5, 4, 11},
15+
new int[][] {{0, 1}, {1, 2}, {1, 3}, {3, 4}}),
16+
equalTo(9));
17+
}
18+
19+
@Test
20+
void minimumScore2() {
21+
assertThat(
22+
new Solution()
23+
.minimumScore(
24+
new int[] {5, 5, 2, 4, 4, 2},
25+
new int[][] {{0, 1}, {1, 2}, {5, 2}, {4, 3}, {1, 3}}),
26+
equalTo(0));
27+
}
28+
}

0 commit comments

Comments
 (0)