Skip to content

Commit b85ca07

Browse files
committed
Added tasks 3364-3367
1 parent 12b5e8e commit b85ca07

File tree

12 files changed

+423
-0
lines changed

12 files changed

+423
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g3301_3400.s3364_minimum_positive_sum_subarray;
2+
3+
// #Easy #2024_11_24_Time_28_ms_(100.00%)_Space_44.7_MB_(100.00%)
4+
5+
import java.util.List;
6+
7+
public class Solution {
8+
public int minimumSumSubarray(List<Integer> nums, int l, int r) {
9+
int size = nums.size();
10+
int res = -1;
11+
for (int s = l; s <= r; s++) {
12+
for (int i = 0; i <= size - s; i++) {
13+
int sum = 0;
14+
for (int j = i; j < i + s; j++) {
15+
sum += nums.get(j);
16+
}
17+
if (sum > 0) {
18+
if (res == -1 || res > sum) {
19+
res = sum;
20+
}
21+
}
22+
}
23+
}
24+
return res;
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
3364\. Minimum Positive Sum Subarray
2+
3+
Easy
4+
5+
You are given an integer array `nums` and **two** integers `l` and `r`. Your task is to find the **minimum** sum of a **subarray** whose size is between `l` and `r` (inclusive) and whose sum is greater than 0.
6+
7+
Return the **minimum** sum of such a subarray. If no such subarray exists, return -1.
8+
9+
A **subarray** is a contiguous **non-empty** sequence of elements within an array.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [3, -2, 1, 4], l = 2, r = 3
14+
15+
**Output:** 1
16+
17+
**Explanation:**
18+
19+
The subarrays of length between `l = 2` and `r = 3` where the sum is greater than 0 are:
20+
21+
* `[3, -2]` with a sum of 1
22+
* `[1, 4]` with a sum of 5
23+
* `[3, -2, 1]` with a sum of 2
24+
* `[-2, 1, 4]` with a sum of 3
25+
26+
Out of these, the subarray `[3, -2]` has a sum of 1, which is the smallest positive sum. Hence, the answer is 1.
27+
28+
**Example 2:**
29+
30+
**Input:** nums = [-2, 2, -3, 1], l = 2, r = 3
31+
32+
**Output:** \-1
33+
34+
**Explanation:**
35+
36+
There is no subarray of length between `l` and `r` that has a sum greater than 0. So, the answer is -1.
37+
38+
**Example 3:**
39+
40+
**Input:** nums = [1, 2, 3, 4], l = 2, r = 4
41+
42+
**Output:** 3
43+
44+
**Explanation:**
45+
46+
The subarray `[1, 2]` has a length of 2 and the minimum sum greater than 0. So, the answer is 3.
47+
48+
**Constraints:**
49+
50+
* `1 <= nums.length <= 100`
51+
* `1 <= l <= r <= nums.length`
52+
* `-1000 <= nums[i] <= 1000`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g3301_3400.s3365_rearrange_k_substrings_to_form_target_string;
2+
3+
// #Medium #2024_11_24_Time_61_ms_(100.00%)_Space_49.3_MB_(100.00%)
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public class Solution {
9+
public boolean isPossibleToRearrange(String s, String t, int k) {
10+
int size = s.length();
11+
int div = size / k;
12+
Map<String, Integer> map = new HashMap<>();
13+
for (int i = 0; i < size; i += div) {
14+
String sub = s.substring(i, i + div);
15+
map.put(sub, map.getOrDefault(sub, 0) + 1);
16+
}
17+
for (int i = 0; i < size; i += div) {
18+
String sub = t.substring(i, i + div);
19+
if (map.getOrDefault(sub, 0) > 0) {
20+
map.put(sub, map.get(sub) - 1);
21+
} else {
22+
return false;
23+
}
24+
}
25+
return true;
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
3365\. Rearrange K Substrings to Form Target String
2+
3+
Medium
4+
5+
You are given two strings `s` and `t`, both of which are anagrams of each other, and an integer `k`.
6+
7+
Your task is to determine whether it is possible to split the string `s` into `k` equal-sized substrings, rearrange the substrings, and concatenate them in _any order_ to create a new string that matches the given string `t`.
8+
9+
Return `true` if this is possible, otherwise, return `false`.
10+
11+
An **anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, using all the original letters exactly once.
12+
13+
A **substring** is a contiguous **non-empty** sequence of characters within a string.
14+
15+
**Example 1:**
16+
17+
**Input:** s = "abcd", t = "cdab", k = 2
18+
19+
**Output:** true
20+
21+
**Explanation:**
22+
23+
* Split `s` into 2 substrings of length 2: `["ab", "cd"]`.
24+
* Rearranging these substrings as `["cd", "ab"]`, and then concatenating them results in `"cdab"`, which matches `t`.
25+
26+
**Example 2:**
27+
28+
**Input:** s = "aabbcc", t = "bbaacc", k = 3
29+
30+
**Output:** true
31+
32+
**Explanation:**
33+
34+
* Split `s` into 3 substrings of length 2: `["aa", "bb", "cc"]`.
35+
* Rearranging these substrings as `["bb", "aa", "cc"]`, and then concatenating them results in `"bbaacc"`, which matches `t`.
36+
37+
**Example 3:**
38+
39+
**Input:** s = "aabbcc", t = "bbaacc", k = 2
40+
41+
**Output:** false
42+
43+
**Explanation:**
44+
45+
* Split `s` into 2 substrings of length 3: `["aab", "bcc"]`.
46+
* These substrings cannot be rearranged to form `t = "bbaacc"`, so the output is `false`.
47+
48+
**Constraints:**
49+
50+
* <code>1 <= s.length == t.length <= 2 * 10<sup>5</sup></code>
51+
* `1 <= k <= s.length`
52+
* `s.length` is divisible by `k`.
53+
* `s` and `t` consist only of lowercase English letters.
54+
* The input is generated such that `s` and `t` are anagrams of each other.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g3301_3400.s3366_minimum_array_sum;
2+
3+
// #Medium #2024_11_24_Time_66_ms_(100.00%)_Space_55_MB_(100.00%)
4+
5+
public class Solution {
6+
public int minArraySum(int[] nums, int k, int op1, int op2) {
7+
Integer[][][] dp = new Integer[nums.length][op1 + 1][op2 + 1];
8+
return sub(dp, nums, 0, k, op1, op2);
9+
}
10+
11+
private int sub(Integer[][][] dp, int[] nums, int i, int k, int op1, int op2) {
12+
if (i == nums.length) {
13+
return 0;
14+
}
15+
if (dp[i][op1][op2] != null) {
16+
return dp[i][op1][op2];
17+
}
18+
int res = sub(dp, nums, i + 1, k, op1, op2) + nums[i];
19+
if (nums[i] >= k && op2 > 0) {
20+
res = Math.min(res, sub(dp, nums, i + 1, k, op1, op2 - 1) + nums[i] - k);
21+
int v = (int) Math.ceil(nums[i] / 2.0);
22+
if (v < k) {
23+
v = (int) Math.ceil((nums[i] - k) / 2.0);
24+
} else {
25+
v -= k;
26+
}
27+
if (op1 > 0) {
28+
res = Math.min(res, sub(dp, nums, i + 1, k, op1 - 1, op2 - 1) + v);
29+
}
30+
}
31+
if (op1 > 0) {
32+
int v = (int) Math.ceil(nums[i] / 2.0);
33+
res = Math.min(res, sub(dp, nums, i + 1, k, op1 - 1, op2) + v);
34+
}
35+
return dp[i][op1][op2] = res;
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
3366\. Minimum Array Sum
2+
3+
Medium
4+
5+
You are given an integer array `nums` and three integers `k`, `op1`, and `op2`.
6+
7+
You can perform the following operations on `nums`:
8+
9+
* **Operation 1**: Choose an index `i` and divide `nums[i]` by 2, **rounding up** to the nearest whole number. You can perform this operation at most `op1` times, and not more than **once** per index.
10+
* **Operation 2**: Choose an index `i` and subtract `k` from `nums[i]`, but only if `nums[i]` is greater than or equal to `k`. You can perform this operation at most `op2` times, and not more than **once** per index.
11+
12+
**Note:** Both operations can be applied to the same index, but at most once each.
13+
14+
Return the **minimum** possible **sum** of all elements in `nums` after performing any number of operations.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [2,8,3,19,3], k = 3, op1 = 1, op2 = 1
19+
20+
**Output:** 23
21+
22+
**Explanation:**
23+
24+
* Apply Operation 2 to `nums[1] = 8`, making `nums[1] = 5`.
25+
* Apply Operation 1 to `nums[3] = 19`, making `nums[3] = 10`.
26+
* The resulting array becomes `[2, 5, 3, 10, 3]`, which has the minimum possible sum of 23 after applying the operations.
27+
28+
**Example 2:**
29+
30+
**Input:** nums = [2,4,3], k = 3, op1 = 2, op2 = 1
31+
32+
**Output:** 3
33+
34+
**Explanation:**
35+
36+
* Apply Operation 1 to `nums[0] = 2`, making `nums[0] = 1`.
37+
* Apply Operation 1 to `nums[1] = 4`, making `nums[1] = 2`.
38+
* Apply Operation 2 to `nums[2] = 3`, making `nums[2] = 0`.
39+
* The resulting array becomes `[1, 2, 0]`, which has the minimum possible sum of 3 after applying the operations.
40+
41+
**Constraints:**
42+
43+
* `1 <= nums.length <= 100`
44+
* <code>0 <= nums[i] <= 10<sup>5</sup></code>
45+
* <code>0 <= k <= 10<sup>5</sup></code>
46+
* `0 <= op1, op2 <= nums.length`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g3301_3400.s3367_maximize_sum_of_weights_after_edge_removals;
2+
3+
// #Hard #2024_11_24_Time_147_ms_(100.00%)_Space_142.2_MB_(100.00%)
4+
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.PriorityQueue;
8+
9+
public class Solution {
10+
public long maximizeSumOfWeights(int[][] edges, int k) {
11+
HashMap<Integer, ArrayList<int[]>> map = new HashMap<>();
12+
for (int[] edge : edges) {
13+
map.computeIfAbsent(edge[0], t -> new ArrayList<>()).add(new int[] {edge[1], edge[2]});
14+
map.computeIfAbsent(edge[1], t -> new ArrayList<>()).add(new int[] {edge[0], edge[2]});
15+
}
16+
return maximizeSumOfWeights(0, -1, k, map)[0];
17+
}
18+
19+
private long[] maximizeSumOfWeights(
20+
int v, int from, int k, HashMap<Integer, ArrayList<int[]>> map) {
21+
long sum = 0;
22+
PriorityQueue<Long> queue = new PriorityQueue<>();
23+
for (int[] i : map.get(v)) {
24+
if (i[0] != from) {
25+
long[] next = maximizeSumOfWeights(i[0], v, k, map);
26+
sum += Math.max(next[0], next[1] += i[1]);
27+
if (next[0] < next[1]) {
28+
queue.offer(next[1] - next[0]);
29+
sum -= queue.size() > k ? queue.poll() : 0;
30+
}
31+
}
32+
}
33+
return new long[] {sum, sum - (queue.size() < k ? 0 : queue.peek())};
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
3367\. Maximize Sum of Weights after Edge Removals
2+
3+
Hard
4+
5+
There exists an **undirected** tree with `n` nodes numbered `0` to `n - 1`. You are given a 2D integer array `edges` of length `n - 1`, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> with weight <code>w<sub>i</sub></code> in the tree.
6+
7+
Your task is to remove _zero or more_ edges such that:
8+
9+
* Each node has an edge with **at most** `k` other nodes, where `k` is given.
10+
* The sum of the weights of the remaining edges is **maximized**.
11+
12+
Return the **maximum** possible sum of weights for the remaining edges after making the necessary removals.
13+
14+
**Example 1:**
15+
16+
**Input:** edges = [[0,1,4],[0,2,2],[2,3,12],[2,4,6]], k = 2
17+
18+
**Output:** 22
19+
20+
**Explanation:**
21+
22+
![](https://assets.leetcode.com/uploads/2024/10/30/test1drawio.png)
23+
24+
* Node 2 has edges with 3 other nodes. We remove the edge `[0, 2, 2]`, ensuring that no node has edges with more than `k = 2` nodes.
25+
* The sum of weights is 22, and we can't achieve a greater sum. Thus, the answer is 22.
26+
27+
**Example 2:**
28+
29+
**Input:** edges = [[0,1,5],[1,2,10],[0,3,15],[3,4,20],[3,5,5],[0,6,10]], k = 3
30+
31+
**Output:** 65
32+
33+
**Explanation:**
34+
35+
* Since no node has edges connecting it to more than `k = 3` nodes, we don't remove any edges.
36+
* The sum of weights is 65. Thus, the answer is 65.
37+
38+
**Constraints:**
39+
40+
* <code>2 <= n <= 10<sup>5</sup></code>
41+
* `1 <= k <= n - 1`
42+
* `edges.length == n - 1`
43+
* `edges[i].length == 3`
44+
* `0 <= edges[i][0] <= n - 1`
45+
* `0 <= edges[i][1] <= n - 1`
46+
* <code>1 <= edges[i][2] <= 10<sup>6</sup></code>
47+
* The input is generated such that `edges` form a valid tree.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g3301_3400.s3364_minimum_positive_sum_subarray;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import java.util.List;
7+
import org.junit.jupiter.api.Test;
8+
9+
class SolutionTest {
10+
@Test
11+
void minimumSumSubarray() {
12+
assertThat(new Solution().minimumSumSubarray(List.of(3, -2, 1, 4), 2, 3), equalTo(1));
13+
}
14+
15+
@Test
16+
void minimumSumSubarray2() {
17+
assertThat(new Solution().minimumSumSubarray(List.of(-2, 2, -3, 1), 2, 3), equalTo(-1));
18+
}
19+
20+
@Test
21+
void minimumSumSubarray3() {
22+
assertThat(new Solution().minimumSumSubarray(List.of(1, 2, 3, 4), 2, 4), equalTo(3));
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3301_3400.s3365_rearrange_k_substrings_to_form_target_string;
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 isPossibleToRearrange() {
11+
assertThat(new Solution().isPossibleToRearrange("abcd", "cdab", 2), equalTo(true));
12+
}
13+
14+
@Test
15+
void isPossibleToRearrange2() {
16+
assertThat(new Solution().isPossibleToRearrange("aabbcc", "bbaacc", 3), equalTo(true));
17+
}
18+
19+
@Test
20+
void isPossibleToRearrange3() {
21+
assertThat(new Solution().isPossibleToRearrange("aabbcc", "bbaacc", 2), equalTo(false));
22+
}
23+
}

0 commit comments

Comments
 (0)