Skip to content

Commit 8eec1ea

Browse files
authoredDec 3, 2024··
Added tasks 3360-3373
1 parent bf2b4dc commit 8eec1ea

File tree

36 files changed

+1581
-0
lines changed

36 files changed

+1581
-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_34.3_MB_(6.00%)
4+
5+
class Solution {
6+
fun canAliceWin(n: Int): Boolean {
7+
if (n < 10) {
8+
return false
9+
}
10+
var stonesRemaining = n - 10
11+
var stonesToBeRemoved = 9
12+
var 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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package g3301_3400.s3361_shift_distance_between_two_strings
2+
3+
// #Medium #Array #String #Prefix_Sum #2024_12_03_Time_350_ms_(82.50%)_Space_41.7_MB_(57.50%)
4+
5+
import kotlin.math.min
6+
7+
class Solution {
8+
fun shiftDistance(s: String, t: String, nextCost: IntArray, previousCost: IntArray): Long {
9+
val costs = Array<LongArray?>(26) { LongArray(26) }
10+
var cost: Long
11+
for (i in 0..25) {
12+
cost = nextCost[i].toLong()
13+
var j = if (i == 25) 0 else i + 1
14+
while (j != i) {
15+
costs[i]!![j] = cost
16+
cost += nextCost[j].toLong()
17+
if (j == 25) {
18+
j = -1
19+
}
20+
j++
21+
}
22+
}
23+
for (i in 0..25) {
24+
cost = previousCost[i].toLong()
25+
var j = if (i == 0) 25 else i - 1
26+
while (j != i) {
27+
costs[i]!![j] = min(costs[i]!![j].toDouble(), cost.toDouble()).toLong()
28+
cost += previousCost[j].toLong()
29+
if (j == 0) {
30+
j = 26
31+
}
32+
j--
33+
}
34+
}
35+
val n = s.length
36+
var ans: Long = 0
37+
for (i in 0..<n) {
38+
ans += costs[s[i].code - 'a'.code]!![t[i].code - 'a'.code]
39+
}
40+
return ans
41+
}
42+
}
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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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_195_ms_(81.82%)_Space_106.3_MB_(72.73%)
5+
6+
import java.util.PriorityQueue
7+
8+
class Solution {
9+
fun maxRemoval(nums: IntArray, queries: Array<IntArray>): Int {
10+
queries.sortWith { a: IntArray, b: IntArray -> a[0] - b[0] }
11+
val last = PriorityQueue<Int?>(Comparator { a: Int?, b: Int? -> b!! - a!! })
12+
val diffs = IntArray(nums.size + 1)
13+
var idx = 0
14+
var cur = 0
15+
for (i in nums.indices) {
16+
while (idx < queries.size && queries[idx][0] == i) {
17+
last.add(queries[idx][1])
18+
idx++
19+
}
20+
cur += diffs[i]
21+
while (cur < nums[i] && last.isNotEmpty() && last.peek()!! >= i) {
22+
cur++
23+
diffs[last.poll()!! + 1]--
24+
}
25+
if (cur < nums[i]) {
26+
return -1
27+
}
28+
}
29+
return last.size
30+
}
31+
}
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: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package g3301_3400.s3363_find_the_maximum_number_of_fruits_collected
2+
3+
// #Hard #Array #Dynamic_Programming #Matrix
4+
// #2024_12_03_Time_39_ms_(88.89%)_Space_161.2_MB_(100.00%)
5+
6+
import kotlin.math.max
7+
8+
class Solution {
9+
fun maxCollectedFruits(fruits: Array<IntArray>): Int {
10+
val n = fruits.size
11+
// Set inaccessible cells to 0
12+
for (i in 0..<n) {
13+
for (j in 0..<n) {
14+
if (i < j && j < n - 1 - i) {
15+
fruits[i][j] = 0
16+
}
17+
if (j < i && i < n - 1 - j) {
18+
fruits[i][j] = 0
19+
}
20+
}
21+
}
22+
var res = 0
23+
for (i in 0..<n) {
24+
res += fruits[i][i]
25+
}
26+
for (i in 1..<n) {
27+
for (j in i + 1..<n) {
28+
fruits[i][j] = (
29+
fruits[i][j] + max(
30+
fruits[i - 1][j - 1],
31+
max(fruits[i - 1][j], if (j + 1 < n) fruits[i - 1][j + 1] else 0),
32+
)
33+
)
34+
}
35+
}
36+
for (j in 1..<n) {
37+
for (i in j + 1..<n) {
38+
fruits[i][j] = (
39+
fruits[i][j] + max(
40+
fruits[i - 1][j - 1],
41+
max(fruits[i][j - 1], if (i + 1 < n) fruits[i + 1][j - 1] else 0),
42+
)
43+
)
44+
}
45+
}
46+
return res + fruits[n - 1][n - 2] + fruits[n - 2][n - 1]
47+
}
48+
}
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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g3301_3400.s3364_minimum_positive_sum_subarray
2+
3+
// #Easy #Array #Prefix_Sum #Sliding_Window #2024_12_03_Time_3_ms_(98.15%)_Space_38.1_MB_(33.33%)
4+
5+
import kotlin.math.min
6+
7+
class Solution {
8+
fun minimumSumSubarray(li: List<Int>, l: Int, r: Int): Int {
9+
val n = li.size
10+
var min = Int.Companion.MAX_VALUE
11+
val a = IntArray(n + 1)
12+
for (i in 1..n) {
13+
a[i] = a[i - 1] + li[i - 1]
14+
}
15+
for (size in l..r) {
16+
for (i in size - 1..<n) {
17+
val sum = a[i + 1] - a[i + 1 - size]
18+
if (sum > 0) {
19+
min = min(min, sum)
20+
}
21+
}
22+
}
23+
return if (min == Int.Companion.MAX_VALUE) {
24+
-1
25+
} else {
26+
min
27+
}
28+
}
29+
}
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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g3301_3400.s3365_rearrange_k_substrings_to_form_target_string
2+
3+
// #Medium #String #Hash_Table #Sorting #2024_12_03_Time_457_ms_(100.00%)_Space_51.5_MB_(81.40%)
4+
5+
class Solution {
6+
fun isPossibleToRearrange(s: String, t: String, k: Int): Boolean {
7+
val size = s.length
8+
val div = size / k
9+
val map: MutableMap<String?, Int?> = HashMap<String?, Int?>()
10+
run {
11+
var i = 0
12+
while (i < size) {
13+
val sub = s.substring(i, i + div)
14+
map.put(sub, map.getOrDefault(sub, 0)!! + 1)
15+
i += div
16+
}
17+
}
18+
var i = 0
19+
while (i < size) {
20+
val sub = t.substring(i, i + div)
21+
if (map.getOrDefault(sub, 0)!! > 0) {
22+
map.put(sub, map[sub]!! - 1)
23+
} else {
24+
return false
25+
}
26+
i += div
27+
}
28+
return true
29+
}
30+
}
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: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package g3301_3400.s3366_minimum_array_sum
2+
3+
// #Medium #Array #Dynamic_Programming #2024_12_03_Time_15_ms_(100.00%)_Space_39.5_MB_(92.86%)
4+
5+
class Solution {
6+
fun minArraySum(nums: IntArray, k: Int, op1: Int, op2: Int): Int {
7+
var op1 = op1
8+
var op2 = op2
9+
nums.sort()
10+
val high = lowerBound(nums, k * 2 - 1)
11+
val low = lowerBound(nums, k)
12+
val n = nums.size
13+
for (i in n - 1 downTo high) {
14+
if (op1 > 0) {
15+
nums[i] = (nums[i] + 1) / 2
16+
op1--
17+
}
18+
if (op2 > 0) {
19+
nums[i] -= k
20+
op2--
21+
}
22+
}
23+
val count: MutableMap<Int?, Int?> = HashMap<Int?, Int?>()
24+
var odd = 0
25+
for (i in low..<high) {
26+
if (op2 > 0) {
27+
nums[i] -= k
28+
if (k % 2 > 0 && nums[i] % 2 > 0) {
29+
count.merge(nums[i], 1) { a: Int?, b: Int? -> Integer.sum(a!!, b!!) }
30+
}
31+
op2--
32+
} else {
33+
odd += nums[i] % 2
34+
}
35+
}
36+
nums.sort(0, high)
37+
var ans = 0
38+
if (k % 2 > 0) {
39+
var i = high - op1
40+
while (i < high && odd > 0) {
41+
val x = nums[i]
42+
if (count.containsKey(x)) {
43+
if (count.merge(x, -1) { a: Int?, b: Int? ->
44+
Integer.sum(a!!, b!!)
45+
} == 0
46+
) {
47+
count.remove(x)
48+
}
49+
odd--
50+
ans--
51+
}
52+
i++
53+
}
54+
}
55+
var i = high - 1
56+
while (i >= 0 && op1 > 0) {
57+
nums[i] = (nums[i] + 1) / 2
58+
i--
59+
op1--
60+
}
61+
for (x in nums) {
62+
ans += x
63+
}
64+
return ans
65+
}
66+
67+
private fun lowerBound(nums: IntArray, target: Int): Int {
68+
var left = -1
69+
var right = nums.size
70+
while (left + 1 < right) {
71+
val mid = (left + right) ushr 1
72+
if (nums[mid] >= target) {
73+
right = mid
74+
} else {
75+
left = mid
76+
}
77+
}
78+
return right
79+
}
80+
}
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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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_113_ms_(100.00%)_Space_141.5_MB_(81.82%)
5+
6+
import java.util.PriorityQueue
7+
import kotlin.math.max
8+
9+
class Solution {
10+
private lateinit var adj: Array<MutableList<IntArray>>
11+
private var k = 0
12+
13+
fun maximizeSumOfWeights(edges: Array<IntArray>, k: Int): Long {
14+
val n = edges.size + 1
15+
adj = Array(n) { ArrayList<IntArray>() }
16+
this.k = k
17+
for (i in 0..<n) {
18+
adj[i] = ArrayList<IntArray>()
19+
}
20+
for (e in edges) {
21+
adj[e[0]].add(e)
22+
adj[e[1]].add(e)
23+
}
24+
return dfs(0, -1)[1]
25+
}
26+
27+
private fun dfs(v: Int, parent: Int): LongArray {
28+
var sum: Long = 0
29+
val pq = PriorityQueue<Long?>()
30+
for (e in adj[v]) {
31+
val w = if (e[0] == v) e[1] else e[0]
32+
if (w == parent) {
33+
continue
34+
}
35+
val res = dfs(w, v)
36+
val max = max(e[2] + res[0], res[1])
37+
sum += max
38+
pq.add(max - res[1])
39+
}
40+
val res = LongArray(2)
41+
while (pq.size > k) {
42+
sum -= pq.poll()!!
43+
}
44+
res[1] = sum
45+
while (pq.size > k - 1) {
46+
sum -= pq.poll()!!
47+
}
48+
res[0] = sum
49+
return res
50+
}
51+
}
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+
class Solution {
6+
fun smallestNumber(n: Int): Int {
7+
var 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+
class Solution {
7+
fun getLargestOutlier(nums: IntArray): Int {
8+
val cnt = IntArray(2001)
9+
var sum = 0
10+
for (i in nums) {
11+
sum += i
12+
cnt[i + 1000]++
13+
}
14+
for (i in cnt.indices.reversed()) {
15+
val j = i - 1000
16+
if (cnt[i] == 0) {
17+
continue
18+
}
19+
sum -= j
20+
val csum = (sum shr 1) + 1000
21+
cnt[i]--
22+
if (sum % 2 == 0 && csum >= 0 && csum < cnt.size && 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,97 @@
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 kotlin.math.max
7+
8+
class Solution {
9+
private fun getGraph(edges: Array<IntArray>): Array<ArrayList<Int?>?> {
10+
val n = edges.size + 1
11+
val graph: Array<ArrayList<Int?>?> = arrayOfNulls<ArrayList<Int?>?>(n)
12+
for (i in 0..<n) {
13+
graph[i] = ArrayList<Int?>()
14+
}
15+
for (edge in edges) {
16+
val u = edge[0]
17+
val v = edge[1]
18+
graph[u]!!.add(v)
19+
graph[v]!!.add(u)
20+
}
21+
return graph
22+
}
23+
24+
private fun dfs(graph: Array<ArrayList<Int?>?>, u: Int, pt: Int, dp: Array<IntArray?>, k: Int) {
25+
for (v in graph[u]!!) {
26+
if (v == pt) {
27+
continue
28+
}
29+
dfs(graph, v!!, u, dp, k)
30+
for (i in 0..<k) {
31+
dp[u]!![i + 1] += dp[v]!![i]
32+
}
33+
}
34+
dp[u]!![0] = dp[u]!![0] + 1
35+
}
36+
37+
private fun dfs2(
38+
graph: Array<ArrayList<Int?>?>,
39+
u: Int,
40+
pt: Int,
41+
ptv: IntArray,
42+
fdp: Array<IntArray?>,
43+
dp: Array<IntArray?>,
44+
k: Int,
45+
) {
46+
fdp[u]!![0] = dp[u]!![0]
47+
for (i in 1..k) {
48+
fdp[u]!![i] = (dp[u]!![i] + ptv[i - 1])
49+
}
50+
for (v in graph[u]!!) {
51+
if (v == pt) {
52+
continue
53+
}
54+
val nptv = IntArray(k + 1)
55+
for (i in 0..<k) {
56+
nptv[i + 1] = dp[u]!![i + 1] - dp[v!!]!![i] + ptv[i]
57+
}
58+
nptv[0] = 1
59+
dfs2(graph, v!!, u, nptv, fdp, dp, k)
60+
}
61+
}
62+
63+
private fun get(edges: Array<IntArray>, k: Int): Array<IntArray?> {
64+
val graph = getGraph(edges)
65+
val n = graph.size
66+
val dp = Array<IntArray?>(n) { IntArray(k + 1) }
67+
val fdp = Array<IntArray?>(n) { IntArray(k + 1) }
68+
dfs(graph, 0, -1, dp, k)
69+
dfs2(graph, 0, -1, IntArray(k + 1), fdp, dp, k)
70+
for (i in 0..<n) {
71+
for (j in 1..k) {
72+
fdp[i]!![j] += fdp[i]!![j - 1]
73+
}
74+
}
75+
return fdp
76+
}
77+
78+
fun maxTargetNodes(edges1: Array<IntArray>, edges2: Array<IntArray>, k: Int): IntArray {
79+
val a = get(edges1, k)
80+
val b = get(edges2, k)
81+
val n = a.size
82+
val m = b.size
83+
val ans = IntArray(n)
84+
var max = 0
85+
run {
86+
var i = 0
87+
while (k != 0 && i < m) {
88+
max = max(max.toDouble(), b[i]!![k - 1].toDouble()).toInt()
89+
i++
90+
}
91+
}
92+
for (i in 0..<n) {
93+
ans[i] = a[i]!![k] + max
94+
}
95+
return ans
96+
}
97+
}
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 kotlin.math.max
7+
8+
class Solution {
9+
fun maxTargetNodes(edges1: Array<IntArray>, edges2: Array<IntArray>): IntArray {
10+
val n = edges1.size + 1
11+
val g1 = packU(n, edges1)
12+
val m = edges2.size + 1
13+
val g2 = packU(m, edges2)
14+
val p2 = parents(g2)
15+
val eo2 = IntArray(2)
16+
for (i in 0..<m) {
17+
eo2[p2[2]!![i] % 2]++
18+
}
19+
val max = max(eo2[0], eo2[1])
20+
val p1 = parents(g1)
21+
val eo1 = IntArray(2)
22+
for (i in 0..<n) {
23+
eo1[p1[2]!![i] % 2]++
24+
}
25+
val ans = IntArray(n)
26+
for (i in 0..<n) {
27+
ans[i] = eo1[p1[2]!![i] % 2] + max
28+
}
29+
return ans
30+
}
31+
32+
private fun parents(g: Array<IntArray?>): Array<IntArray?> {
33+
val n = g.size
34+
val par = IntArray(n)
35+
par.fill(-1)
36+
val depth = IntArray(n)
37+
depth[0] = 0
38+
val q = IntArray(n)
39+
q[0] = 0
40+
var p = 0
41+
var r = 1
42+
while (p < r) {
43+
val cur = q[p]
44+
for (nex in 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 arrayOf<IntArray?>(par, q, depth)
54+
}
55+
56+
private fun packU(n: Int, ft: Array<IntArray>): Array<IntArray?> {
57+
val g = arrayOfNulls<IntArray>(n)
58+
val p = IntArray(n)
59+
for (u in ft) {
60+
p[u[0]]++
61+
p[u[1]]++
62+
}
63+
for (i in 0..<n) {
64+
g[i] = IntArray(p[i])
65+
}
66+
for (u in 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+
}
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g3301_3400.s3360_stone_removal_game
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun canAliceWin() {
10+
assertThat<Boolean>(Solution().canAliceWin(12), equalTo<Boolean>(true))
11+
}
12+
13+
@Test
14+
fun canAliceWin2() {
15+
assertThat<Boolean>(Solution().canAliceWin(1), equalTo<Boolean>(false))
16+
}
17+
18+
@Test
19+
fun canAliceWin3() {
20+
assertThat<Boolean>(Solution().canAliceWin(19), equalTo<Boolean>(false))
21+
}
22+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package g3301_3400.s3361_shift_distance_between_two_strings
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun shiftDistance() {
10+
assertThat<Long>(
11+
Solution()
12+
.shiftDistance(
13+
"abab",
14+
"baba",
15+
intArrayOf(
16+
100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17+
0, 0, 0, 0, 0,
18+
),
19+
intArrayOf(
20+
1, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
21+
0, 0, 0, 0, 0,
22+
),
23+
),
24+
equalTo<Long>(2L),
25+
)
26+
}
27+
28+
@Test
29+
fun shiftDistance2() {
30+
assertThat<Long>(
31+
Solution()
32+
.shiftDistance(
33+
"leet",
34+
"code",
35+
intArrayOf(
36+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
37+
1, 1, 1, 1, 1,
38+
),
39+
intArrayOf(
40+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
41+
1, 1, 1, 1, 1,
42+
),
43+
),
44+
equalTo<Long>(31L),
45+
)
46+
}
47+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package g3301_3400.s3362_zero_array_transformation_iii
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun maxRemoval() {
10+
assertThat<Int>(
11+
Solution()
12+
.maxRemoval(
13+
intArrayOf(2, 0, 2),
14+
arrayOf<IntArray>(intArrayOf(0, 2), intArrayOf(0, 2), intArrayOf(1, 1)),
15+
),
16+
equalTo<Int>(1),
17+
)
18+
}
19+
20+
@Test
21+
fun maxRemoval2() {
22+
assertThat<Int>(
23+
Solution()
24+
.maxRemoval(
25+
intArrayOf(1, 1, 1, 1),
26+
arrayOf<IntArray>(intArrayOf(1, 3), intArrayOf(0, 2), intArrayOf(1, 3), intArrayOf(1, 2)),
27+
),
28+
equalTo<Int>(2),
29+
)
30+
}
31+
32+
@Test
33+
fun maxRemoval3() {
34+
assertThat<Int>(
35+
Solution().maxRemoval(intArrayOf(1, 2, 3, 4), arrayOf<IntArray>(intArrayOf(0, 3))),
36+
equalTo<Int>(-1),
37+
)
38+
}
39+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g3301_3400.s3363_find_the_maximum_number_of_fruits_collected
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun maxCollectedFruits() {
10+
assertThat<Int>(
11+
Solution()
12+
.maxCollectedFruits(
13+
arrayOf<IntArray>(
14+
intArrayOf(1, 2, 3, 4),
15+
intArrayOf(5, 6, 8, 7),
16+
intArrayOf(9, 10, 11, 12),
17+
intArrayOf(13, 14, 15, 16),
18+
),
19+
),
20+
equalTo<Int>(100),
21+
)
22+
}
23+
24+
@Test
25+
fun maxCollectedFruits2() {
26+
assertThat<Int>(
27+
Solution().maxCollectedFruits(
28+
arrayOf<IntArray>(
29+
intArrayOf(1, 1),
30+
intArrayOf(1, 1),
31+
),
32+
),
33+
equalTo<Int>(4),
34+
)
35+
}
36+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g3301_3400.s3364_minimum_positive_sum_subarray
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun minimumSumSubarray() {
10+
assertThat<Int>(
11+
Solution().minimumSumSubarray(listOf<Int>(3, -2, 1, 4), 2, 3),
12+
equalTo<Int>(1),
13+
)
14+
}
15+
16+
@Test
17+
fun minimumSumSubarray2() {
18+
assertThat<Int>(
19+
Solution().minimumSumSubarray(listOf<Int>(-2, 2, -3, 1), 2, 3),
20+
equalTo<Int>(-1),
21+
)
22+
}
23+
24+
@Test
25+
fun minimumSumSubarray3() {
26+
assertThat<Int>(
27+
Solution().minimumSumSubarray(listOf<Int>(1, 2, 3, 4), 2, 4),
28+
equalTo<Int>(3),
29+
)
30+
}
31+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g3301_3400.s3365_rearrange_k_substrings_to_form_target_string
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun isPossibleToRearrange() {
10+
assertThat<Boolean>(
11+
Solution()
12+
.isPossibleToRearrange("abcd", "cdab", 2),
13+
equalTo<Boolean>(true),
14+
)
15+
}
16+
17+
@Test
18+
fun isPossibleToRearrange2() {
19+
assertThat<Boolean>(
20+
Solution()
21+
.isPossibleToRearrange("aabbcc", "bbaacc", 3),
22+
equalTo<Boolean>(true),
23+
)
24+
}
25+
26+
@Test
27+
fun isPossibleToRearrange3() {
28+
assertThat<Boolean>(
29+
Solution()
30+
.isPossibleToRearrange("aabbcc", "bbaacc", 2),
31+
equalTo<Boolean>(false),
32+
)
33+
}
34+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package g3301_3400.s3366_minimum_array_sum
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun minArraySum() {
10+
assertThat<Int>(
11+
Solution().minArraySum(intArrayOf(2, 8, 3, 19, 3), 3, 1, 1),
12+
equalTo<Int>(23),
13+
)
14+
}
15+
16+
@Test
17+
fun minArraySum2() {
18+
assertThat<Int>(
19+
Solution().minArraySum(intArrayOf(2, 4, 3), 3, 2, 1),
20+
equalTo<Int>(3),
21+
)
22+
}
23+
24+
@Test
25+
fun minArraySum3() {
26+
assertThat<Int>(
27+
Solution()
28+
.minArraySum(
29+
intArrayOf(
30+
1, 3, 5, 7, 9, 12, 12, 12, 13, 15, 15, 15, 16, 17, 19, 20,
31+
),
32+
11,
33+
15,
34+
4,
35+
),
36+
equalTo<Int>(77),
37+
)
38+
}
39+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package g3301_3400.s3367_maximize_sum_of_weights_after_edge_removals
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun maximizeSumOfWeights() {
10+
assertThat<Long>(
11+
Solution()
12+
.maximizeSumOfWeights(
13+
arrayOf<IntArray>(
14+
intArrayOf(0, 1, 4),
15+
intArrayOf(0, 2, 2),
16+
intArrayOf(2, 3, 12),
17+
intArrayOf(2, 4, 6),
18+
),
19+
2,
20+
),
21+
equalTo<Long>(22L),
22+
)
23+
}
24+
25+
@Test
26+
fun maximizeSumOfWeights2() {
27+
assertThat<Long>(
28+
Solution()
29+
.maximizeSumOfWeights(
30+
arrayOf<IntArray>(
31+
intArrayOf(0, 1, 5),
32+
intArrayOf(1, 2, 10),
33+
intArrayOf(0, 3, 15),
34+
intArrayOf(3, 4, 20),
35+
intArrayOf(3, 5, 5),
36+
intArrayOf(0, 6, 10),
37+
),
38+
3,
39+
),
40+
equalTo<Long>(65L),
41+
)
42+
}
43+
44+
@Test
45+
fun maximizeSumOfWeights3() {
46+
assertThat<Long>(
47+
Solution().maximizeSumOfWeights(
48+
arrayOf<IntArray>(intArrayOf(0, 1, 34), intArrayOf(0, 2, 17)),
49+
1,
50+
),
51+
equalTo<Long>(34L),
52+
)
53+
}
54+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g3301_3400.s3370_smallest_number_with_all_set_bits
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun smallestNumber() {
10+
assertThat<Int>(Solution().smallestNumber(5), equalTo<Int>(7))
11+
}
12+
13+
@Test
14+
fun smallestNumber2() {
15+
assertThat<Int>(Solution().smallestNumber(10), equalTo<Int>(15))
16+
}
17+
18+
@Test
19+
fun smallestNumber3() {
20+
assertThat<Int>(Solution().smallestNumber(3), equalTo<Int>(3))
21+
}
22+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package g3301_3400.s3371_identify_the_largest_outlier_in_an_array
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun largestOutlier() {
10+
assertThat<Int>(
11+
Solution()
12+
.getLargestOutlier(intArrayOf(2, 3, 5, 10)),
13+
equalTo<Int>(10),
14+
)
15+
}
16+
17+
@Test
18+
fun largestOutlier2() {
19+
assertThat<Int>(
20+
Solution()
21+
.getLargestOutlier(intArrayOf(-2, -1, -3, -6, 4)),
22+
equalTo<Int>(4),
23+
)
24+
}
25+
26+
@Test
27+
fun largestOutlier3() {
28+
assertThat<Int>(
29+
Solution()
30+
.getLargestOutlier(intArrayOf(1, 1, 1, 1, 1, 5, 5)),
31+
equalTo<Int>(5),
32+
)
33+
}
34+
35+
@Test
36+
fun largestOutlier4() {
37+
assertThat<Int>(
38+
Solution()
39+
.getLargestOutlier(intArrayOf(-108, -108, -517)),
40+
equalTo<Int>(-517),
41+
)
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package g3301_3400.s3372_maximize_the_number_of_target_nodes_after_connecting_trees_i
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun maxTargetNodes() {
10+
assertThat<IntArray>(
11+
Solution()
12+
.maxTargetNodes(
13+
arrayOf<IntArray>(intArrayOf(0, 1), intArrayOf(0, 2), intArrayOf(2, 3), intArrayOf(2, 4)),
14+
arrayOf<IntArray>(
15+
intArrayOf(0, 1),
16+
intArrayOf(0, 2),
17+
intArrayOf(0, 3),
18+
intArrayOf(2, 7),
19+
intArrayOf(1, 4),
20+
intArrayOf(4, 5),
21+
intArrayOf(4, 6),
22+
),
23+
2,
24+
),
25+
equalTo<IntArray>(intArrayOf(9, 7, 9, 8, 8)),
26+
)
27+
}
28+
29+
@Test
30+
fun maxTargetNodes2() {
31+
assertThat<IntArray>(
32+
Solution()
33+
.maxTargetNodes(
34+
arrayOf<IntArray>(intArrayOf(0, 1), intArrayOf(0, 2), intArrayOf(0, 3), intArrayOf(0, 4)),
35+
arrayOf<IntArray>(intArrayOf(0, 1), intArrayOf(1, 2), intArrayOf(2, 3)),
36+
1,
37+
),
38+
equalTo<IntArray>(intArrayOf(6, 3, 3, 3, 3)),
39+
)
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package g3301_3400.s3373_maximize_the_number_of_target_nodes_after_connecting_trees_ii
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun maxTargetNodes() {
10+
assertThat<IntArray>(
11+
Solution()
12+
.maxTargetNodes(
13+
arrayOf<IntArray>(intArrayOf(0, 1), intArrayOf(0, 2), intArrayOf(2, 3), intArrayOf(2, 4)),
14+
arrayOf<IntArray>(
15+
intArrayOf(0, 1),
16+
intArrayOf(0, 2),
17+
intArrayOf(0, 3),
18+
intArrayOf(2, 7),
19+
intArrayOf(1, 4),
20+
intArrayOf(4, 5),
21+
intArrayOf(4, 6),
22+
),
23+
),
24+
equalTo<IntArray>(intArrayOf(8, 7, 7, 8, 8)),
25+
)
26+
}
27+
28+
@Test
29+
fun maxTargetNodes2() {
30+
assertThat<IntArray>(
31+
Solution()
32+
.maxTargetNodes(
33+
arrayOf<IntArray>(intArrayOf(0, 1), intArrayOf(0, 2), intArrayOf(0, 3), intArrayOf(0, 4)),
34+
arrayOf<IntArray>(intArrayOf(0, 1), intArrayOf(1, 2), intArrayOf(2, 3)),
35+
),
36+
equalTo<IntArray>(intArrayOf(3, 6, 6, 6, 6)),
37+
)
38+
}
39+
}

0 commit comments

Comments
 (0)
Please sign in to comment.