Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 952c5da

Browse files
authoredDec 3, 2024
Added tasks 3360-3373
1 parent 818ed59 commit 952c5da

File tree

36 files changed

+1462
-0
lines changed

36 files changed

+1462
-0
lines changed
 
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g3301_3400.s3360_stone_removal_game;
2+
3+
// #Easy #Math #Simulation #2024_12_03_Time_0_ms_(100.00%)_Space_40.3_MB_(80.86%)
4+
5+
public class Solution {
6+
public boolean canAliceWin(int n) {
7+
if (n < 10) {
8+
return false;
9+
}
10+
int stonesRemaining = n - 10;
11+
int stonesToBeRemoved = 9;
12+
int i = 1;
13+
while (stonesRemaining >= stonesToBeRemoved) {
14+
stonesRemaining -= stonesToBeRemoved;
15+
i++;
16+
stonesToBeRemoved--;
17+
}
18+
return i % 2 != 0;
19+
}
20+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
3360\. Stone Removal Game
2+
3+
Easy
4+
5+
Alice and Bob are playing a game where they take turns removing stones from a pile, with _Alice going first_.
6+
7+
* Alice starts by removing **exactly** 10 stones on her first turn.
8+
* For each subsequent turn, each player removes **exactly** 1 fewer stone than the previous opponent.
9+
10+
The player who cannot make a move loses the game.
11+
12+
Given a positive integer `n`, return `true` if Alice wins the game and `false` otherwise.
13+
14+
**Example 1:**
15+
16+
**Input:** n = 12
17+
18+
**Output:** true
19+
20+
**Explanation:**
21+
22+
* Alice removes 10 stones on her first turn, leaving 2 stones for Bob.
23+
* Bob cannot remove 9 stones, so Alice wins.
24+
25+
**Example 2:**
26+
27+
**Input:** n = 1
28+
29+
**Output:** false
30+
31+
**Explanation:**
32+
33+
* Alice cannot remove 10 stones, so Alice loses.
34+
35+
**Constraints:**
36+
37+
* `1 <= n <= 50`
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package g3301_3400.s3361_shift_distance_between_two_strings;
2+
3+
// #Medium #Array #String #Prefix_Sum #2024_12_03_Time_9_ms_(100.00%)_Space_45.8_MB_(36.02%)
4+
5+
public class Solution {
6+
public long shiftDistance(String s, String t, int[] nextCost, int[] previousCost) {
7+
long[][] costs = new long[26][26];
8+
long cost;
9+
for (int i = 0; i < 26; i++) {
10+
cost = nextCost[i];
11+
int j = i == 25 ? 0 : i + 1;
12+
while (j != i) {
13+
costs[i][j] = cost;
14+
cost += nextCost[j];
15+
if (j == 25) {
16+
j = -1;
17+
}
18+
j++;
19+
}
20+
}
21+
for (int i = 0; i < 26; i++) {
22+
cost = previousCost[i];
23+
int j = i == 0 ? 25 : i - 1;
24+
while (j != i) {
25+
costs[i][j] = Math.min(costs[i][j], cost);
26+
cost += previousCost[j];
27+
if (j == 0) {
28+
j = 26;
29+
}
30+
j--;
31+
}
32+
}
33+
int n = s.length();
34+
long ans = 0;
35+
for (int i = 0; i < n; i++) {
36+
ans += costs[s.charAt(i) - 'a'][t.charAt(i) - 'a'];
37+
}
38+
return ans;
39+
}
40+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
3361\. Shift Distance Between Two Strings
2+
3+
Medium
4+
5+
You are given two strings `s` and `t` of the same length, and two integer arrays `nextCost` and `previousCost`.
6+
7+
In one operation, you can pick any index `i` of `s`, and perform **either one** of the following actions:
8+
9+
* Shift `s[i]` to the next letter in the alphabet. If `s[i] == 'z'`, you should replace it with `'a'`. This operation costs `nextCost[j]` where `j` is the index of `s[i]` in the alphabet.
10+
* Shift `s[i]` to the previous letter in the alphabet. If `s[i] == 'a'`, you should replace it with `'z'`. This operation costs `previousCost[j]` where `j` is the index of `s[i]` in the alphabet.
11+
12+
The **shift distance** is the **minimum** total cost of operations required to transform `s` into `t`.
13+
14+
Return the **shift distance** from `s` to `t`.
15+
16+
**Example 1:**
17+
18+
**Input:** s = "abab", t = "baba", nextCost = [100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], previousCost = [1,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
19+
20+
**Output:** 2
21+
22+
**Explanation:**
23+
24+
* We choose index `i = 0` and shift `s[0]` 25 times to the previous character for a total cost of 1.
25+
* We choose index `i = 1` and shift `s[1]` 25 times to the next character for a total cost of 0.
26+
* We choose index `i = 2` and shift `s[2]` 25 times to the previous character for a total cost of 1.
27+
* We choose index `i = 3` and shift `s[3]` 25 times to the next character for a total cost of 0.
28+
29+
**Example 2:**
30+
31+
**Input:** s = "leet", t = "code", nextCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], previousCost = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
32+
33+
**Output:** 31
34+
35+
**Explanation:**
36+
37+
* We choose index `i = 0` and shift `s[0]` 9 times to the previous character for a total cost of 9.
38+
* We choose index `i = 1` and shift `s[1]` 10 times to the next character for a total cost of 10.
39+
* We choose index `i = 2` and shift `s[2]` 1 time to the previous character for a total cost of 1.
40+
* We choose index `i = 3` and shift `s[3]` 11 times to the next character for a total cost of 11.
41+
42+
**Constraints:**
43+
44+
* <code>1 <= s.length == t.length <= 10<sup>5</sup></code>
45+
* `s` and `t` consist only of lowercase English letters.
46+
* `nextCost.length == previousCost.length == 26`
47+
* <code>0 <= nextCost[i], previousCost[i] <= 10<sup>9</sup></code>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g3301_3400.s3362_zero_array_transformation_iii;
2+
3+
// #Medium #Array #Sorting #Greedy #Heap_Priority_Queue #Prefix_Sum
4+
// #2024_12_03_Time_68_ms_(91.99%)_Space_93.6_MB_(45.88%)
5+
6+
import java.util.Arrays;
7+
import java.util.PriorityQueue;
8+
9+
public class Solution {
10+
public int maxRemoval(int[] nums, int[][] queries) {
11+
Arrays.sort(queries, (a, b) -> a[0] - b[0]);
12+
PriorityQueue<Integer> last = new PriorityQueue<>((a, b) -> b - a);
13+
int[] diffs = new int[nums.length + 1];
14+
int idx = 0;
15+
int cur = 0;
16+
for (int i = 0; i < nums.length; i++) {
17+
while (idx < queries.length && queries[idx][0] == i) {
18+
last.add(queries[idx][1]);
19+
idx++;
20+
}
21+
cur += diffs[i];
22+
while (cur < nums[i] && !last.isEmpty() && last.peek() >= i) {
23+
cur++;
24+
diffs[last.poll() + 1]--;
25+
}
26+
if (cur < nums[i]) {
27+
return -1;
28+
}
29+
}
30+
return last.size();
31+
}
32+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
3362\. Zero Array Transformation III
2+
3+
Medium
4+
5+
You are given an integer array `nums` of length `n` and a 2D array `queries` where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>.
6+
7+
Each `queries[i]` represents the following action on `nums`:
8+
9+
* Decrement the value at each index in the range <code>[l<sub>i</sub>, r<sub>i</sub>]</code> in `nums` by **at most** 1.
10+
* The amount by which the value is decremented can be chosen **independently** for each index.
11+
12+
A **Zero Array** is an array with all its elements equal to 0.
13+
14+
Return the **maximum** number of elements that can be removed from `queries`, such that `nums` can still be converted to a **zero array** using the _remaining_ queries. If it is not possible to convert `nums` to a **zero array**, return -1.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [2,0,2], queries = [[0,2],[0,2],[1,1]]
19+
20+
**Output:** 1
21+
22+
**Explanation:**
23+
24+
After removing `queries[2]`, `nums` can still be converted to a zero array.
25+
26+
* Using `queries[0]`, decrement `nums[0]` and `nums[2]` by 1 and `nums[1]` by 0.
27+
* Using `queries[1]`, decrement `nums[0]` and `nums[2]` by 1 and `nums[1]` by 0.
28+
29+
**Example 2:**
30+
31+
**Input:** nums = [1,1,1,1], queries = [[1,3],[0,2],[1,3],[1,2]]
32+
33+
**Output:** 2
34+
35+
**Explanation:**
36+
37+
We can remove `queries[2]` and `queries[3]`.
38+
39+
**Example 3:**
40+
41+
**Input:** nums = [1,2,3,4], queries = [[0,3]]
42+
43+
**Output:** \-1
44+
45+
**Explanation:**
46+
47+
`nums` cannot be converted to a zero array even after using all the queries.
48+
49+
**Constraints:**
50+
51+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
52+
* <code>0 <= nums[i] <= 10<sup>5</sup></code>
53+
* <code>1 <= queries.length <= 10<sup>5</sup></code>
54+
* `queries[i].length == 2`
55+
* <code>0 <= l<sub>i</sub> <= r<sub>i</sub> < nums.length</code>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package g3301_3400.s3363_find_the_maximum_number_of_fruits_collected;
2+
3+
// #Hard #Array #Dynamic_Programming #Matrix #2024_12_03_Time_12_ms_(91.34%)_Space_142.1_MB_(88.96%)
4+
5+
public class Solution {
6+
public int maxCollectedFruits(int[][] fruits) {
7+
int n = fruits.length;
8+
// Set inaccessible cells to 0
9+
for (int i = 0; i < n; ++i) {
10+
for (int j = 0; j < n; ++j) {
11+
if (i < j && j < n - 1 - i) {
12+
fruits[i][j] = 0;
13+
}
14+
if (j < i && i < n - 1 - j) {
15+
fruits[i][j] = 0;
16+
}
17+
}
18+
}
19+
int res = 0;
20+
for (int i = 0; i < n; ++i) {
21+
res += fruits[i][i];
22+
}
23+
for (int i = 1; i < n; ++i) {
24+
for (int j = i + 1; j < n; ++j) {
25+
fruits[i][j] +=
26+
Math.max(
27+
fruits[i - 1][j - 1],
28+
Math.max(fruits[i - 1][j], (j + 1 < n) ? fruits[i - 1][j + 1] : 0));
29+
}
30+
}
31+
for (int j = 1; j < n; ++j) {
32+
for (int i = j + 1; i < n; ++i) {
33+
fruits[i][j] +=
34+
Math.max(
35+
fruits[i - 1][j - 1],
36+
Math.max(fruits[i][j - 1], (i + 1 < n) ? fruits[i + 1][j - 1] : 0));
37+
}
38+
}
39+
return res + fruits[n - 1][n - 2] + fruits[n - 2][n - 1];
40+
}
41+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
3363\. Find the Maximum Number of Fruits Collected
2+
3+
Hard
4+
5+
There is a game dungeon comprised of `n x n` rooms arranged in a grid.
6+
7+
You are given a 2D array `fruits` of size `n x n`, where `fruits[i][j]` represents the number of fruits in the room `(i, j)`. Three children will play in the game dungeon, with **initial** positions at the corner rooms `(0, 0)`, `(0, n - 1)`, and `(n - 1, 0)`.
8+
9+
The children will make **exactly** `n - 1` moves according to the following rules to reach the room `(n - 1, n - 1)`:
10+
11+
* The child starting from `(0, 0)` must move from their current room `(i, j)` to one of the rooms `(i + 1, j + 1)`, `(i + 1, j)`, and `(i, j + 1)` if the target room exists.
12+
* The child starting from `(0, n - 1)` must move from their current room `(i, j)` to one of the rooms `(i + 1, j - 1)`, `(i + 1, j)`, and `(i + 1, j + 1)` if the target room exists.
13+
* The child starting from `(n - 1, 0)` must move from their current room `(i, j)` to one of the rooms `(i - 1, j + 1)`, `(i, j + 1)`, and `(i + 1, j + 1)` if the target room exists.
14+
15+
When a child enters a room, they will collect all the fruits there. If two or more children enter the same room, only one child will collect the fruits, and the room will be emptied after they leave.
16+
17+
Return the **maximum** number of fruits the children can collect from the dungeon.
18+
19+
**Example 1:**
20+
21+
**Input:** fruits = [[1,2,3,4],[5,6,8,7],[9,10,11,12],[13,14,15,16]]
22+
23+
**Output:** 100
24+
25+
**Explanation:**
26+
27+
![](https://assets.leetcode.com/uploads/2024/10/15/example_1.gif)
28+
29+
In this example:
30+
31+
* The 1<sup>st</sup> child (green) moves on the path `(0,0) -> (1,1) -> (2,2) -> (3, 3)`.
32+
* The 2<sup>nd</sup> child (red) moves on the path `(0,3) -> (1,2) -> (2,3) -> (3, 3)`.
33+
* The 3<sup>rd</sup> child (blue) moves on the path `(3,0) -> (3,1) -> (3,2) -> (3, 3)`.
34+
35+
In total they collect `1 + 6 + 11 + 1 + 4 + 8 + 12 + 13 + 14 + 15 = 100` fruits.
36+
37+
**Example 2:**
38+
39+
**Input:** fruits = [[1,1],[1,1]]
40+
41+
**Output:** 4
42+
43+
**Explanation:**
44+
45+
In this example:
46+
47+
* The 1<sup>st</sup> child moves on the path `(0,0) -> (1,1)`.
48+
* The 2<sup>nd</sup> child moves on the path `(0,1) -> (1,1)`.
49+
* The 3<sup>rd</sup> child moves on the path `(1,0) -> (1,1)`.
50+
51+
In total they collect `1 + 1 + 1 + 1 = 4` fruits.
52+
53+
**Constraints:**
54+
55+
* `2 <= n == fruits.length == fruits[i].length <= 1000`
56+
* `0 <= fruits[i][j] <= 1000`
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g3301_3400.s3364_minimum_positive_sum_subarray;
2+
3+
// #Easy #Array #Prefix_Sum #Sliding_Window #2024_12_03_Time_1_ms_(100.00%)_Space_44.5_MB_(38.75%)
4+
5+
import java.util.List;
6+
7+
public class Solution {
8+
public int minimumSumSubarray(List<Integer> li, int l, int r) {
9+
int n = li.size();
10+
int min = Integer.MAX_VALUE;
11+
int[] a = new int[n + 1];
12+
for (int i = 1; i <= n; i++) {
13+
a[i] = a[i - 1] + li.get(i - 1);
14+
}
15+
for (int size = l; size <= r; size++) {
16+
for (int i = size - 1; i < n; i++) {
17+
int sum = a[i + 1] - a[i + 1 - size];
18+
if (sum > 0) {
19+
min = Math.min(min, sum);
20+
}
21+
}
22+
}
23+
return min == Integer.MAX_VALUE ? -1 : min;
24+
}
25+
}
Lines changed: 52 additions & 0 deletions
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`
Lines changed: 27 additions & 0 deletions
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 #String #Hash_Table #Sorting #2024_12_03_Time_59_ms_(94.24%)_Space_49.2_MB_(97.33%)
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+
}
Lines changed: 54 additions & 0 deletions
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.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package g3301_3400.s3366_minimum_array_sum;
2+
3+
// #Medium #Array #Dynamic_Programming #2024_12_03_Time_4_ms_(99.77%)_Space_43_MB_(99.69%)
4+
5+
import java.util.Arrays;
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
public class Solution {
10+
public int minArraySum(int[] nums, int k, int op1, int op2) {
11+
Arrays.sort(nums);
12+
int high = lowerBound(nums, k * 2 - 1);
13+
int low = lowerBound(nums, k);
14+
int n = nums.length;
15+
for (int i = n - 1; i >= high; i--) {
16+
if (op1 > 0) {
17+
nums[i] = (nums[i] + 1) / 2;
18+
op1--;
19+
}
20+
if (op2 > 0) {
21+
nums[i] -= k;
22+
op2--;
23+
}
24+
}
25+
Map<Integer, Integer> count = new HashMap<>();
26+
int odd = 0;
27+
for (int i = low; i < high; i++) {
28+
if (op2 > 0) {
29+
nums[i] -= k;
30+
if (k % 2 > 0 && nums[i] % 2 > 0) {
31+
count.merge(nums[i], 1, Integer::sum);
32+
}
33+
op2--;
34+
} else {
35+
odd += nums[i] % 2;
36+
}
37+
}
38+
Arrays.sort(nums, 0, high);
39+
int ans = 0;
40+
if (k % 2 > 0) {
41+
for (int i = high - op1; i < high && odd > 0; i++) {
42+
int x = nums[i];
43+
if (count.containsKey(x)) {
44+
if (count.merge(x, -1, Integer::sum) == 0) {
45+
count.remove(x);
46+
}
47+
odd--;
48+
ans--;
49+
}
50+
}
51+
}
52+
for (int i = high - 1; i >= 0 && op1 > 0; i--, op1--) {
53+
nums[i] = (nums[i] + 1) / 2;
54+
}
55+
for (int x : nums) {
56+
ans += x;
57+
}
58+
return ans;
59+
}
60+
61+
private int lowerBound(int[] nums, int target) {
62+
int left = -1;
63+
int right = nums.length;
64+
while (left + 1 < right) {
65+
int mid = (left + right) >>> 1;
66+
if (nums[mid] >= target) {
67+
right = mid;
68+
} else {
69+
left = mid;
70+
}
71+
}
72+
return right;
73+
}
74+
}
Lines changed: 46 additions & 0 deletions
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`
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package g3301_3400.s3367_maximize_sum_of_weights_after_edge_removals;
2+
3+
// #Hard #Dynamic_Programming #Depth_First_Search #Tree
4+
// #2024_12_03_Time_104_ms_(91.49%)_Space_147.1_MB_(58.51%)
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import java.util.PriorityQueue;
9+
10+
@SuppressWarnings("unchecked")
11+
public class Solution {
12+
private List<int[]>[] adj;
13+
private int k;
14+
15+
public long maximizeSumOfWeights(int[][] edges, int k) {
16+
int n = edges.length + 1;
17+
adj = new List[n];
18+
this.k = k;
19+
for (int i = 0; i < n; i++) {
20+
adj[i] = new ArrayList<>();
21+
}
22+
for (int[] e : edges) {
23+
adj[e[0]].add(e);
24+
adj[e[1]].add(e);
25+
}
26+
return dfs(0, -1)[1];
27+
}
28+
29+
private long[] dfs(int v, int parent) {
30+
long sum = 0;
31+
PriorityQueue<Long> pq = new PriorityQueue<>();
32+
for (int[] e : adj[v]) {
33+
int w = e[0] == v ? e[1] : e[0];
34+
if (w == parent) {
35+
continue;
36+
}
37+
long[] res = dfs(w, v);
38+
long max = Math.max(e[2] + res[0], res[1]);
39+
sum += max;
40+
pq.add(max - res[1]);
41+
}
42+
long[] res = new long[2];
43+
while (pq.size() > k) {
44+
sum -= pq.poll();
45+
}
46+
res[1] = sum;
47+
while (pq.size() > k - 1) {
48+
sum -= pq.poll();
49+
}
50+
res[0] = sum;
51+
return res;
52+
}
53+
}
Lines changed: 47 additions & 0 deletions
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.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package g3301_3400.s3370_smallest_number_with_all_set_bits;
2+
3+
// #Easy #Math #Bit_Manipulation #2024_12_03_Time_0_ms_(100.00%)_Space_41.1_MB_(45.50%)
4+
5+
public class Solution {
6+
public int smallestNumber(int n) {
7+
int res = 1;
8+
while (res < n) {
9+
res = res * 2 + 1;
10+
}
11+
return res;
12+
}
13+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
3370\. Smallest Number With All Set Bits
2+
3+
Easy
4+
5+
You are given a _positive_ number `n`.
6+
7+
Return the **smallest** number `x` **greater than** or **equal to** `n`, such that the binary representation of `x` contains only **set** bits.
8+
9+
A **set** bit refers to a bit in the binary representation of a number that has a value of `1`.
10+
11+
**Example 1:**
12+
13+
**Input:** n = 5
14+
15+
**Output:** 7
16+
17+
**Explanation:**
18+
19+
The binary representation of 7 is `"111"`.
20+
21+
**Example 2:**
22+
23+
**Input:** n = 10
24+
25+
**Output:** 15
26+
27+
**Explanation:**
28+
29+
The binary representation of 15 is `"1111"`.
30+
31+
**Example 3:**
32+
33+
**Input:** n = 3
34+
35+
**Output:** 3
36+
37+
**Explanation:**
38+
39+
The binary representation of 3 is `"11"`.
40+
41+
**Constraints:**
42+
43+
* `1 <= n <= 1000`
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g3301_3400.s3371_identify_the_largest_outlier_in_an_array;
2+
3+
// #Medium #Array #Hash_Table #Counting #Enumeration
4+
// #2024_12_03_Time_5_ms_(100.00%)_Space_60.6_MB_(33.40%)
5+
6+
public class Solution {
7+
public int getLargestOutlier(int[] nums) {
8+
int[] cnt = new int[2001];
9+
int sum = 0;
10+
for (int i : nums) {
11+
sum += i;
12+
cnt[i + 1000]++;
13+
}
14+
for (int i = cnt.length - 1; i >= 0; --i) {
15+
int j = i - 1000;
16+
if (cnt[i] == 0) {
17+
continue;
18+
}
19+
sum -= j;
20+
int csum = (sum >> 1) + 1000;
21+
cnt[i]--;
22+
if (sum % 2 == 0 && csum >= 0 && csum < cnt.length && cnt[csum] > 0) {
23+
return j;
24+
}
25+
sum += j;
26+
cnt[i]++;
27+
}
28+
return 0;
29+
}
30+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
3371\. Identify the Largest Outlier in an Array
2+
3+
Medium
4+
5+
You are given an integer array `nums`. This array contains `n` elements, where **exactly** `n - 2` elements are **special** **numbers**. One of the remaining **two** elements is the _sum_ of these **special numbers**, and the other is an **outlier**.
6+
7+
An **outlier** is defined as a number that is _neither_ one of the original special numbers _nor_ the element representing the sum of those numbers.
8+
9+
**Note** that special numbers, the sum element, and the outlier must have **distinct** indices, but _may_ share the **same** value.
10+
11+
Return the **largest** potential **outlier** in `nums`.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [2,3,5,10]
16+
17+
**Output:** 10
18+
19+
**Explanation:**
20+
21+
The special numbers could be 2 and 3, thus making their sum 5 and the outlier 10.
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [-2,-1,-3,-6,4]
26+
27+
**Output:** 4
28+
29+
**Explanation:**
30+
31+
The special numbers could be -2, -1, and -3, thus making their sum -6 and the outlier 4.
32+
33+
**Example 3:**
34+
35+
**Input:** nums = [1,1,1,1,1,5,5]
36+
37+
**Output:** 5
38+
39+
**Explanation:**
40+
41+
The special numbers could be 1, 1, 1, 1, and 1, thus making their sum 5 and the other 5 as the outlier.
42+
43+
**Constraints:**
44+
45+
* <code>3 <= nums.length <= 10<sup>5</sup></code>
46+
* `-1000 <= nums[i] <= 1000`
47+
* The input is generated such that at least **one** potential outlier exists in `nums`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package g3301_3400.s3372_maximize_the_number_of_target_nodes_after_connecting_trees_i;
2+
3+
// #Medium #Depth_First_Search #Breadth_First_Search #Tree
4+
// #2024_12_03_Time_50_ms_(99.49%)_Space_75.7_MB_(5.10%)
5+
6+
import java.util.ArrayList;
7+
8+
@SuppressWarnings("unchecked")
9+
public class Solution {
10+
private ArrayList<Integer>[] getGraph(int[][] edges) {
11+
int n = edges.length + 1;
12+
ArrayList<Integer>[] graph = new ArrayList[n];
13+
for (int i = 0; i < n; i++) {
14+
graph[i] = new ArrayList<>();
15+
}
16+
for (int[] edge : edges) {
17+
int u = edge[0];
18+
int v = edge[1];
19+
graph[u].add(v);
20+
graph[v].add(u);
21+
}
22+
return graph;
23+
}
24+
25+
private void dfs(ArrayList<Integer>[] graph, int u, int pt, int[][] dp, int k) {
26+
for (int v : graph[u]) {
27+
if (v == pt) {
28+
continue;
29+
}
30+
dfs(graph, v, u, dp, k);
31+
for (int i = 0; i < k; i++) {
32+
dp[u][i + 1] += dp[v][i];
33+
}
34+
}
35+
dp[u][0]++;
36+
}
37+
38+
private void dfs2(
39+
ArrayList<Integer>[] graph, int u, int pt, int[] ptv, int[][] fdp, int[][] dp, int k) {
40+
fdp[u][0] = dp[u][0];
41+
for (int i = 1; i <= k; i++) {
42+
fdp[u][i] = (dp[u][i] + ptv[i - 1]);
43+
}
44+
for (int v : graph[u]) {
45+
if (v == pt) {
46+
continue;
47+
}
48+
int[] nptv = new int[k + 1];
49+
for (int i = 0; i < k; i++) {
50+
nptv[i + 1] = dp[u][i + 1] - dp[v][i] + ptv[i];
51+
}
52+
nptv[0] = 1;
53+
dfs2(graph, v, u, nptv, fdp, dp, k);
54+
}
55+
}
56+
57+
private int[][] get(int[][] edges, int k) {
58+
ArrayList<Integer>[] graph = getGraph(edges);
59+
int n = graph.length;
60+
int[][] dp = new int[n][k + 1];
61+
int[][] fdp = new int[n][k + 1];
62+
dfs(graph, 0, -1, dp, k);
63+
dfs2(graph, 0, -1, new int[k + 1], fdp, dp, k);
64+
for (int i = 0; i < n; i++) {
65+
for (int j = 1; j <= k; j++) {
66+
fdp[i][j] += fdp[i][j - 1];
67+
}
68+
}
69+
return fdp;
70+
}
71+
72+
public int[] maxTargetNodes(int[][] edges1, int[][] edges2, int k) {
73+
int[][] a = get(edges1, k);
74+
int[][] b = get(edges2, k);
75+
int n = a.length;
76+
int m = b.length;
77+
int[] ans = new int[n];
78+
int max = 0;
79+
for (int i = 0; k != 0 && i < m; i++) {
80+
max = Math.max(max, b[i][k - 1]);
81+
}
82+
for (int i = 0; i < n; i++) {
83+
ans[i] = a[i][k] + max;
84+
}
85+
return ans;
86+
}
87+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
3372\. Maximize the Number of Target Nodes After Connecting Trees I
2+
3+
Medium
4+
5+
There exist two **undirected** trees with `n` and `m` nodes, with **distinct** labels in ranges `[0, n - 1]` and `[0, m - 1]`, respectively.
6+
7+
You are given two 2D integer arrays `edges1` and `edges2` of lengths `n - 1` and `m - 1`, respectively, where <code>edges1[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 first tree and <code>edges2[i] = [u<sub>i</sub>, v<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> in the second tree. You are also given an integer `k`.
8+
9+
Node `u` is **target** to node `v` if the number of edges on the path from `u` to `v` is less than or equal to `k`. **Note** that a node is _always_ **target** to itself.
10+
11+
Return an array of `n` integers `answer`, where `answer[i]` is the **maximum** possible number of nodes **target** to node `i` of the first tree if you have to connect one node from the first tree to another node in the second tree.
12+
13+
**Note** that queries are independent from each other. That is, for every query you will remove the added edge before proceeding to the next query.
14+
15+
**Example 1:**
16+
17+
**Input:** edges1 = [[0,1],[0,2],[2,3],[2,4]], edges2 = [[0,1],[0,2],[0,3],[2,7],[1,4],[4,5],[4,6]], k = 2
18+
19+
**Output:** [9,7,9,8,8]
20+
21+
**Explanation:**
22+
23+
* For `i = 0`, connect node 0 from the first tree to node 0 from the second tree.
24+
* For `i = 1`, connect node 1 from the first tree to node 0 from the second tree.
25+
* For `i = 2`, connect node 2 from the first tree to node 4 from the second tree.
26+
* For `i = 3`, connect node 3 from the first tree to node 4 from the second tree.
27+
* For `i = 4`, connect node 4 from the first tree to node 4 from the second tree.
28+
29+
![](https://assets.leetcode.com/uploads/2024/09/24/3982-1.png)
30+
31+
**Example 2:**
32+
33+
**Input:** edges1 = [[0,1],[0,2],[0,3],[0,4]], edges2 = [[0,1],[1,2],[2,3]], k = 1
34+
35+
**Output:** [6,3,3,3,3]
36+
37+
**Explanation:**
38+
39+
For every `i`, connect node `i` of the first tree with any node of the second tree.
40+
41+
![](https://assets.leetcode.com/uploads/2024/09/24/3928-2.png)
42+
43+
**Constraints:**
44+
45+
* `2 <= n, m <= 1000`
46+
* `edges1.length == n - 1`
47+
* `edges2.length == m - 1`
48+
* `edges1[i].length == edges2[i].length == 2`
49+
* <code>edges1[i] = [a<sub>i</sub>, b<sub>i</sub>]</code>
50+
* <code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code>
51+
* <code>edges2[i] = [u<sub>i</sub>, v<sub>i</sub>]</code>
52+
* <code>0 <= u<sub>i</sub>, v<sub>i</sub> < m</code>
53+
* The input is generated such that `edges1` and `edges2` represent valid trees.
54+
* `0 <= k <= 1000`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package g3301_3400.s3373_maximize_the_number_of_target_nodes_after_connecting_trees_ii;
2+
3+
// #Hard #Depth_First_Search #Breadth_First_Search #Tree
4+
// #2024_12_03_Time_26_ms_(98.75%)_Space_114.7_MB_(80.00%)
5+
6+
import java.util.Arrays;
7+
8+
public class Solution {
9+
public int[] maxTargetNodes(int[][] edges1, int[][] edges2) {
10+
int n = edges1.length + 1;
11+
int[][] g1 = packU(n, edges1);
12+
int m = edges2.length + 1;
13+
int[][] g2 = packU(m, edges2);
14+
int[][] p2 = parents(g2);
15+
int[] eo2 = new int[2];
16+
for (int i = 0; i < m; i++) {
17+
eo2[p2[2][i] % 2]++;
18+
}
19+
int max = Math.max(eo2[0], eo2[1]);
20+
int[][] p1 = parents(g1);
21+
int[] eo1 = new int[2];
22+
for (int i = 0; i < n; i++) {
23+
eo1[p1[2][i] % 2]++;
24+
}
25+
int[] ans = new int[n];
26+
for (int i = 0; i < n; i++) {
27+
ans[i] = eo1[p1[2][i] % 2] + max;
28+
}
29+
return ans;
30+
}
31+
32+
private int[][] parents(int[][] g) {
33+
int n = g.length;
34+
int[] par = new int[n];
35+
Arrays.fill(par, -1);
36+
int[] depth = new int[n];
37+
depth[0] = 0;
38+
int[] q = new int[n];
39+
q[0] = 0;
40+
int p = 0;
41+
int r = 1;
42+
while (p < r) {
43+
int cur = q[p];
44+
for (int nex : g[cur]) {
45+
if (par[cur] != nex) {
46+
q[r++] = nex;
47+
par[nex] = cur;
48+
depth[nex] = depth[cur] + 1;
49+
}
50+
}
51+
p++;
52+
}
53+
return new int[][] {par, q, depth};
54+
}
55+
56+
private int[][] packU(int n, int[][] ft) {
57+
int[][] g = new int[n][];
58+
int[] p = new int[n];
59+
for (int[] u : ft) {
60+
p[u[0]]++;
61+
p[u[1]]++;
62+
}
63+
for (int i = 0; i < n; i++) {
64+
g[i] = new int[p[i]];
65+
}
66+
for (int[] u : ft) {
67+
g[u[0]][--p[u[0]]] = u[1];
68+
g[u[1]][--p[u[1]]] = u[0];
69+
}
70+
return g;
71+
}
72+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
3373\. Maximize the Number of Target Nodes After Connecting Trees II
2+
3+
Hard
4+
5+
There exist two **undirected** trees with `n` and `m` nodes, labeled from `[0, n - 1]` and `[0, m - 1]`, respectively.
6+
7+
You are given two 2D integer arrays `edges1` and `edges2` of lengths `n - 1` and `m - 1`, respectively, where <code>edges1[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 first tree and <code>edges2[i] = [u<sub>i</sub>, v<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> in the second tree.
8+
9+
Node `u` is **target** to node `v` if the number of edges on the path from `u` to `v` is even. **Note** that a node is _always_ **target** to itself.
10+
11+
Return an array of `n` integers `answer`, where `answer[i]` is the **maximum** possible number of nodes that are **target** to node `i` of the first tree if you had to connect one node from the first tree to another node in the second tree.
12+
13+
**Note** that queries are independent from each other. That is, for every query you will remove the added edge before proceeding to the next query.
14+
15+
**Example 1:**
16+
17+
**Input:** edges1 = [[0,1],[0,2],[2,3],[2,4]], edges2 = [[0,1],[0,2],[0,3],[2,7],[1,4],[4,5],[4,6]]
18+
19+
**Output:** [8,7,7,8,8]
20+
21+
**Explanation:**
22+
23+
* For `i = 0`, connect node 0 from the first tree to node 0 from the second tree.
24+
* For `i = 1`, connect node 1 from the first tree to node 4 from the second tree.
25+
* For `i = 2`, connect node 2 from the first tree to node 7 from the second tree.
26+
* For `i = 3`, connect node 3 from the first tree to node 0 from the second tree.
27+
* For `i = 4`, connect node 4 from the first tree to node 4 from the second tree.
28+
29+
![](https://assets.leetcode.com/uploads/2024/09/24/3982-1.png)
30+
31+
**Example 2:**
32+
33+
**Input:** edges1 = [[0,1],[0,2],[0,3],[0,4]], edges2 = [[0,1],[1,2],[2,3]]
34+
35+
**Output:** [3,6,6,6,6]
36+
37+
**Explanation:**
38+
39+
For every `i`, connect node `i` of the first tree with any node of the second tree.
40+
41+
![](https://assets.leetcode.com/uploads/2024/09/24/3928-2.png)
42+
43+
**Constraints:**
44+
45+
* <code>2 <= n, m <= 10<sup>5</sup></code>
46+
* `edges1.length == n - 1`
47+
* `edges2.length == m - 1`
48+
* `edges1[i].length == edges2[i].length == 2`
49+
* <code>edges1[i] = [a<sub>i</sub>, b<sub>i</sub>]</code>
50+
* <code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code>
51+
* <code>edges2[i] = [u<sub>i</sub>, v<sub>i</sub>]</code>
52+
* <code>0 <= u<sub>i</sub>, v<sub>i</sub> < m</code>
53+
* The input is generated such that `edges1` and `edges2` represent valid trees.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3301_3400.s3360_stone_removal_game;
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 canAliceWin() {
11+
assertThat(new Solution().canAliceWin(12), equalTo(true));
12+
}
13+
14+
@Test
15+
void canAliceWin2() {
16+
assertThat(new Solution().canAliceWin(1), equalTo(false));
17+
}
18+
19+
@Test
20+
void canAliceWin3() {
21+
assertThat(new Solution().canAliceWin(19), equalTo(false));
22+
}
23+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package g3301_3400.s3361_shift_distance_between_two_strings;
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 shiftDistance() {
11+
assertThat(
12+
new Solution()
13+
.shiftDistance(
14+
"abab",
15+
"baba",
16+
new int[] {
17+
100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18+
0, 0, 0, 0, 0
19+
},
20+
new int[] {
21+
1, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
22+
0, 0, 0, 0, 0
23+
}),
24+
equalTo(2L));
25+
}
26+
27+
@Test
28+
void shiftDistance2() {
29+
assertThat(
30+
new Solution()
31+
.shiftDistance(
32+
"leet",
33+
"code",
34+
new int[] {
35+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
36+
1, 1, 1, 1, 1
37+
},
38+
new int[] {
39+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
40+
1, 1, 1, 1, 1
41+
}),
42+
equalTo(31L));
43+
}
44+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g3301_3400.s3362_zero_array_transformation_iii;
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 maxRemoval() {
11+
assertThat(
12+
new Solution()
13+
.maxRemoval(new int[] {2, 0, 2}, new int[][] {{0, 2}, {0, 2}, {1, 1}}),
14+
equalTo(1));
15+
}
16+
17+
@Test
18+
void maxRemoval2() {
19+
assertThat(
20+
new Solution()
21+
.maxRemoval(
22+
new int[] {1, 1, 1, 1},
23+
new int[][] {{1, 3}, {0, 2}, {1, 3}, {1, 2}}),
24+
equalTo(2));
25+
}
26+
27+
@Test
28+
void maxRemoval3() {
29+
assertThat(
30+
new Solution().maxRemoval(new int[] {1, 2, 3, 4}, new int[][] {{0, 3}}),
31+
equalTo(-1));
32+
}
33+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g3301_3400.s3363_find_the_maximum_number_of_fruits_collected;
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 maxCollectedFruits() {
11+
assertThat(
12+
new Solution()
13+
.maxCollectedFruits(
14+
new int[][] {
15+
{1, 2, 3, 4}, {5, 6, 8, 7}, {9, 10, 11, 12}, {13, 14, 15, 16}
16+
}),
17+
equalTo(100));
18+
}
19+
20+
@Test
21+
void maxCollectedFruits2() {
22+
assertThat(new Solution().maxCollectedFruits(new int[][] {{1, 1}, {1, 1}}), equalTo(4));
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
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+
}
Lines changed: 23 additions & 0 deletions
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+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g3301_3400.s3366_minimum_array_sum;
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 minArraySum() {
11+
assertThat(new Solution().minArraySum(new int[] {2, 8, 3, 19, 3}, 3, 1, 1), equalTo(23));
12+
}
13+
14+
@Test
15+
void minArraySum2() {
16+
assertThat(new Solution().minArraySum(new int[] {2, 4, 3}, 3, 2, 1), equalTo(3));
17+
}
18+
19+
@Test
20+
void minArraySum3() {
21+
assertThat(
22+
new Solution()
23+
.minArraySum(
24+
new int[] {
25+
1, 3, 5, 7, 9, 12, 12, 12, 13, 15, 15, 15, 16, 17, 19, 20
26+
},
27+
11,
28+
15,
29+
4),
30+
equalTo(77));
31+
}
32+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package g3301_3400.s3367_maximize_sum_of_weights_after_edge_removals;
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 maximizeSumOfWeights() {
11+
assertThat(
12+
new Solution()
13+
.maximizeSumOfWeights(
14+
new int[][] {{0, 1, 4}, {0, 2, 2}, {2, 3, 12}, {2, 4, 6}}, 2),
15+
equalTo(22L));
16+
}
17+
18+
@Test
19+
void maximizeSumOfWeights2() {
20+
assertThat(
21+
new Solution()
22+
.maximizeSumOfWeights(
23+
new int[][] {
24+
{0, 1, 5},
25+
{1, 2, 10},
26+
{0, 3, 15},
27+
{3, 4, 20},
28+
{3, 5, 5},
29+
{0, 6, 10}
30+
},
31+
3),
32+
equalTo(65L));
33+
}
34+
35+
@Test
36+
void maximizeSumOfWeights3() {
37+
assertThat(
38+
new Solution().maximizeSumOfWeights(new int[][] {{0, 1, 34}, {0, 2, 17}}, 1),
39+
equalTo(34L));
40+
}
41+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3301_3400.s3370_smallest_number_with_all_set_bits;
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 smallestNumber() {
11+
assertThat(new Solution().smallestNumber(5), equalTo(7));
12+
}
13+
14+
@Test
15+
void smallestNumber2() {
16+
assertThat(new Solution().smallestNumber(10), equalTo(15));
17+
}
18+
19+
@Test
20+
void smallestNumber3() {
21+
assertThat(new Solution().smallestNumber(3), equalTo(3));
22+
}
23+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g3301_3400.s3371_identify_the_largest_outlier_in_an_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 getLargestOutlier() {
11+
assertThat(new Solution().getLargestOutlier(new int[] {2, 3, 5, 10}), equalTo(10));
12+
}
13+
14+
@Test
15+
void getLargestOutlier2() {
16+
assertThat(new Solution().getLargestOutlier(new int[] {-2, -1, -3, -6, 4}), equalTo(4));
17+
}
18+
19+
@Test
20+
void getLargestOutlier3() {
21+
assertThat(new Solution().getLargestOutlier(new int[] {1, 1, 1, 1, 1, 5, 5}), equalTo(5));
22+
}
23+
24+
@Test
25+
void getLargestOutlier4() {
26+
assertThat(new Solution().getLargestOutlier(new int[] {-108, -108, -517}), equalTo(-517));
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g3301_3400.s3372_maximize_the_number_of_target_nodes_after_connecting_trees_i;
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 maxTargetNodes() {
11+
assertThat(
12+
new Solution()
13+
.maxTargetNodes(
14+
new int[][] {{0, 1}, {0, 2}, {2, 3}, {2, 4}},
15+
new int[][] {
16+
{0, 1}, {0, 2}, {0, 3}, {2, 7}, {1, 4}, {4, 5}, {4, 6}
17+
},
18+
2),
19+
equalTo(new int[] {9, 7, 9, 8, 8}));
20+
}
21+
22+
@Test
23+
void maxTargetNodes2() {
24+
assertThat(
25+
new Solution()
26+
.maxTargetNodes(
27+
new int[][] {{0, 1}, {0, 2}, {0, 3}, {0, 4}},
28+
new int[][] {{0, 1}, {1, 2}, {2, 3}},
29+
1),
30+
equalTo(new int[] {6, 3, 3, 3, 3}));
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g3301_3400.s3373_maximize_the_number_of_target_nodes_after_connecting_trees_ii;
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 maxTargetNodes() {
11+
assertThat(
12+
new Solution()
13+
.maxTargetNodes(
14+
new int[][] {{0, 1}, {0, 2}, {2, 3}, {2, 4}},
15+
new int[][] {
16+
{0, 1}, {0, 2}, {0, 3}, {2, 7}, {1, 4}, {4, 5}, {4, 6}
17+
}),
18+
equalTo(new int[] {8, 7, 7, 8, 8}));
19+
}
20+
21+
@Test
22+
void maxTargetNodes2() {
23+
assertThat(
24+
new Solution()
25+
.maxTargetNodes(
26+
new int[][] {{0, 1}, {0, 2}, {0, 3}, {0, 4}},
27+
new int[][] {{0, 1}, {1, 2}, {2, 3}}),
28+
equalTo(new int[] {3, 6, 6, 6, 6}));
29+
}
30+
}

0 commit comments

Comments
 (0)
Please sign in to comment.