diff --git a/src/main/kotlin/g3301_3400/s3360_stone_removal_game/Solution.kt b/src/main/kotlin/g3301_3400/s3360_stone_removal_game/Solution.kt new file mode 100644 index 000000000..03d32e9a3 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3360_stone_removal_game/Solution.kt @@ -0,0 +1,20 @@ +package g3301_3400.s3360_stone_removal_game + +// #Easy #Math #Simulation #2024_12_03_Time_0_ms_(100.00%)_Space_34.3_MB_(6.00%) + +class Solution { + fun canAliceWin(n: Int): Boolean { + if (n < 10) { + return false + } + var stonesRemaining = n - 10 + var stonesToBeRemoved = 9 + var i = 1 + while (stonesRemaining >= stonesToBeRemoved) { + stonesRemaining -= stonesToBeRemoved + i++ + stonesToBeRemoved-- + } + return i % 2 != 0 + } +} diff --git a/src/main/kotlin/g3301_3400/s3360_stone_removal_game/readme.md b/src/main/kotlin/g3301_3400/s3360_stone_removal_game/readme.md new file mode 100644 index 000000000..aa45026ba --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3360_stone_removal_game/readme.md @@ -0,0 +1,37 @@ +3360\. Stone Removal Game + +Easy + +Alice and Bob are playing a game where they take turns removing stones from a pile, with _Alice going first_. + +* Alice starts by removing **exactly** 10 stones on her first turn. +* For each subsequent turn, each player removes **exactly** 1 fewer stone than the previous opponent. + +The player who cannot make a move loses the game. + +Given a positive integer `n`, return `true` if Alice wins the game and `false` otherwise. + +**Example 1:** + +**Input:** n = 12 + +**Output:** true + +**Explanation:** + +* Alice removes 10 stones on her first turn, leaving 2 stones for Bob. +* Bob cannot remove 9 stones, so Alice wins. + +**Example 2:** + +**Input:** n = 1 + +**Output:** false + +**Explanation:** + +* Alice cannot remove 10 stones, so Alice loses. + +**Constraints:** + +* `1 <= n <= 50` \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/Solution.kt b/src/main/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/Solution.kt new file mode 100644 index 000000000..171fc37ae --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/Solution.kt @@ -0,0 +1,42 @@ +package g3301_3400.s3361_shift_distance_between_two_strings + +// #Medium #Array #String #Prefix_Sum #2024_12_03_Time_350_ms_(82.50%)_Space_41.7_MB_(57.50%) + +import kotlin.math.min + +class Solution { + fun shiftDistance(s: String, t: String, nextCost: IntArray, previousCost: IntArray): Long { + val costs = Array(26) { LongArray(26) } + var cost: Long + for (i in 0..25) { + cost = nextCost[i].toLong() + var j = if (i == 25) 0 else i + 1 + while (j != i) { + costs[i]!![j] = cost + cost += nextCost[j].toLong() + if (j == 25) { + j = -1 + } + j++ + } + } + for (i in 0..25) { + cost = previousCost[i].toLong() + var j = if (i == 0) 25 else i - 1 + while (j != i) { + costs[i]!![j] = min(costs[i]!![j].toDouble(), cost.toDouble()).toLong() + cost += previousCost[j].toLong() + if (j == 0) { + j = 26 + } + j-- + } + } + val n = s.length + var ans: Long = 0 + for (i in 0..1 <= s.length == t.length <= 105 +* `s` and `t` consist only of lowercase English letters. +* `nextCost.length == previousCost.length == 26` +* 0 <= nextCost[i], previousCost[i] <= 109 \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3362_zero_array_transformation_iii/Solution.kt b/src/main/kotlin/g3301_3400/s3362_zero_array_transformation_iii/Solution.kt new file mode 100644 index 000000000..ebd432a9e --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3362_zero_array_transformation_iii/Solution.kt @@ -0,0 +1,31 @@ +package g3301_3400.s3362_zero_array_transformation_iii + +// #Medium #Array #Sorting #Greedy #Heap_Priority_Queue #Prefix_Sum +// #2024_12_03_Time_195_ms_(81.82%)_Space_106.3_MB_(72.73%) + +import java.util.PriorityQueue + +class Solution { + fun maxRemoval(nums: IntArray, queries: Array): Int { + queries.sortWith { a: IntArray, b: IntArray -> a[0] - b[0] } + val last = PriorityQueue(Comparator { a: Int?, b: Int? -> b!! - a!! }) + val diffs = IntArray(nums.size + 1) + var idx = 0 + var cur = 0 + for (i in nums.indices) { + while (idx < queries.size && queries[idx][0] == i) { + last.add(queries[idx][1]) + idx++ + } + cur += diffs[i] + while (cur < nums[i] && last.isNotEmpty() && last.peek()!! >= i) { + cur++ + diffs[last.poll()!! + 1]-- + } + if (cur < nums[i]) { + return -1 + } + } + return last.size + } +} diff --git a/src/main/kotlin/g3301_3400/s3362_zero_array_transformation_iii/readme.md b/src/main/kotlin/g3301_3400/s3362_zero_array_transformation_iii/readme.md new file mode 100644 index 000000000..c82de843a --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3362_zero_array_transformation_iii/readme.md @@ -0,0 +1,55 @@ +3362\. Zero Array Transformation III + +Medium + +You are given an integer array `nums` of length `n` and a 2D array `queries` where queries[i] = [li, ri]. + +Each `queries[i]` represents the following action on `nums`: + +* Decrement the value at each index in the range [li, ri] in `nums` by **at most** 1. +* The amount by which the value is decremented can be chosen **independently** for each index. + +A **Zero Array** is an array with all its elements equal to 0. + +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. + +**Example 1:** + +**Input:** nums = [2,0,2], queries = [[0,2],[0,2],[1,1]] + +**Output:** 1 + +**Explanation:** + +After removing `queries[2]`, `nums` can still be converted to a zero array. + +* Using `queries[0]`, decrement `nums[0]` and `nums[2]` by 1 and `nums[1]` by 0. +* Using `queries[1]`, decrement `nums[0]` and `nums[2]` by 1 and `nums[1]` by 0. + +**Example 2:** + +**Input:** nums = [1,1,1,1], queries = [[1,3],[0,2],[1,3],[1,2]] + +**Output:** 2 + +**Explanation:** + +We can remove `queries[2]` and `queries[3]`. + +**Example 3:** + +**Input:** nums = [1,2,3,4], queries = [[0,3]] + +**Output:** \-1 + +**Explanation:** + +`nums` cannot be converted to a zero array even after using all the queries. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 0 <= nums[i] <= 105 +* 1 <= queries.length <= 105 +* `queries[i].length == 2` +* 0 <= li <= ri < nums.length \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3363_find_the_maximum_number_of_fruits_collected/Solution.kt b/src/main/kotlin/g3301_3400/s3363_find_the_maximum_number_of_fruits_collected/Solution.kt new file mode 100644 index 000000000..c843e3896 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3363_find_the_maximum_number_of_fruits_collected/Solution.kt @@ -0,0 +1,48 @@ +package g3301_3400.s3363_find_the_maximum_number_of_fruits_collected + +// #Hard #Array #Dynamic_Programming #Matrix +// #2024_12_03_Time_39_ms_(88.89%)_Space_161.2_MB_(100.00%) + +import kotlin.math.max + +class Solution { + fun maxCollectedFruits(fruits: Array): Int { + val n = fruits.size + // Set inaccessible cells to 0 + for (i in 0..st child (green) moves on the path `(0,0) -> (1,1) -> (2,2) -> (3, 3)`. +* The 2nd child (red) moves on the path `(0,3) -> (1,2) -> (2,3) -> (3, 3)`. +* The 3rd child (blue) moves on the path `(3,0) -> (3,1) -> (3,2) -> (3, 3)`. + +In total they collect `1 + 6 + 11 + 1 + 4 + 8 + 12 + 13 + 14 + 15 = 100` fruits. + +**Example 2:** + +**Input:** fruits = [[1,1],[1,1]] + +**Output:** 4 + +**Explanation:** + +In this example: + +* The 1st child moves on the path `(0,0) -> (1,1)`. +* The 2nd child moves on the path `(0,1) -> (1,1)`. +* The 3rd child moves on the path `(1,0) -> (1,1)`. + +In total they collect `1 + 1 + 1 + 1 = 4` fruits. + +**Constraints:** + +* `2 <= n == fruits.length == fruits[i].length <= 1000` +* `0 <= fruits[i][j] <= 1000` \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/Solution.kt b/src/main/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/Solution.kt new file mode 100644 index 000000000..b0dc06cbe --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/Solution.kt @@ -0,0 +1,29 @@ +package g3301_3400.s3364_minimum_positive_sum_subarray + +// #Easy #Array #Prefix_Sum #Sliding_Window #2024_12_03_Time_3_ms_(98.15%)_Space_38.1_MB_(33.33%) + +import kotlin.math.min + +class Solution { + fun minimumSumSubarray(li: List, l: Int, r: Int): Int { + val n = li.size + var min = Int.Companion.MAX_VALUE + val a = IntArray(n + 1) + for (i in 1..n) { + a[i] = a[i - 1] + li[i - 1] + } + for (size in l..r) { + for (i in size - 1.. 0) { + min = min(min, sum) + } + } + } + return if (min == Int.Companion.MAX_VALUE) { + -1 + } else { + min + } + } +} diff --git a/src/main/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/readme.md b/src/main/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/readme.md new file mode 100644 index 000000000..97fae38a4 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/readme.md @@ -0,0 +1,52 @@ +3364\. Minimum Positive Sum Subarray + +Easy + +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. + +Return the **minimum** sum of such a subarray. If no such subarray exists, return -1. + +A **subarray** is a contiguous **non-empty** sequence of elements within an array. + +**Example 1:** + +**Input:** nums = [3, -2, 1, 4], l = 2, r = 3 + +**Output:** 1 + +**Explanation:** + +The subarrays of length between `l = 2` and `r = 3` where the sum is greater than 0 are: + +* `[3, -2]` with a sum of 1 +* `[1, 4]` with a sum of 5 +* `[3, -2, 1]` with a sum of 2 +* `[-2, 1, 4]` with a sum of 3 + +Out of these, the subarray `[3, -2]` has a sum of 1, which is the smallest positive sum. Hence, the answer is 1. + +**Example 2:** + +**Input:** nums = [-2, 2, -3, 1], l = 2, r = 3 + +**Output:** \-1 + +**Explanation:** + +There is no subarray of length between `l` and `r` that has a sum greater than 0. So, the answer is -1. + +**Example 3:** + +**Input:** nums = [1, 2, 3, 4], l = 2, r = 4 + +**Output:** 3 + +**Explanation:** + +The subarray `[1, 2]` has a length of 2 and the minimum sum greater than 0. So, the answer is 3. + +**Constraints:** + +* `1 <= nums.length <= 100` +* `1 <= l <= r <= nums.length` +* `-1000 <= nums[i] <= 1000` \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/Solution.kt b/src/main/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/Solution.kt new file mode 100644 index 000000000..7d30add83 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/Solution.kt @@ -0,0 +1,30 @@ +package g3301_3400.s3365_rearrange_k_substrings_to_form_target_string + +// #Medium #String #Hash_Table #Sorting #2024_12_03_Time_457_ms_(100.00%)_Space_51.5_MB_(81.40%) + +class Solution { + fun isPossibleToRearrange(s: String, t: String, k: Int): Boolean { + val size = s.length + val div = size / k + val map: MutableMap = HashMap() + run { + var i = 0 + while (i < size) { + val sub = s.substring(i, i + div) + map.put(sub, map.getOrDefault(sub, 0)!! + 1) + i += div + } + } + var i = 0 + while (i < size) { + val sub = t.substring(i, i + div) + if (map.getOrDefault(sub, 0)!! > 0) { + map.put(sub, map[sub]!! - 1) + } else { + return false + } + i += div + } + return true + } +} diff --git a/src/main/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/readme.md b/src/main/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/readme.md new file mode 100644 index 000000000..d9eb1a2db --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/readme.md @@ -0,0 +1,54 @@ +3365\. Rearrange K Substrings to Form Target String + +Medium + +You are given two strings `s` and `t`, both of which are anagrams of each other, and an integer `k`. + +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`. + +Return `true` if this is possible, otherwise, return `false`. + +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. + +A **substring** is a contiguous **non-empty** sequence of characters within a string. + +**Example 1:** + +**Input:** s = "abcd", t = "cdab", k = 2 + +**Output:** true + +**Explanation:** + +* Split `s` into 2 substrings of length 2: `["ab", "cd"]`. +* Rearranging these substrings as `["cd", "ab"]`, and then concatenating them results in `"cdab"`, which matches `t`. + +**Example 2:** + +**Input:** s = "aabbcc", t = "bbaacc", k = 3 + +**Output:** true + +**Explanation:** + +* Split `s` into 3 substrings of length 2: `["aa", "bb", "cc"]`. +* Rearranging these substrings as `["bb", "aa", "cc"]`, and then concatenating them results in `"bbaacc"`, which matches `t`. + +**Example 3:** + +**Input:** s = "aabbcc", t = "bbaacc", k = 2 + +**Output:** false + +**Explanation:** + +* Split `s` into 2 substrings of length 3: `["aab", "bcc"]`. +* These substrings cannot be rearranged to form `t = "bbaacc"`, so the output is `false`. + +**Constraints:** + +* 1 <= s.length == t.length <= 2 * 105 +* `1 <= k <= s.length` +* `s.length` is divisible by `k`. +* `s` and `t` consist only of lowercase English letters. +* The input is generated such that `s` and `t` are anagrams of each other. \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3366_minimum_array_sum/Solution.kt b/src/main/kotlin/g3301_3400/s3366_minimum_array_sum/Solution.kt new file mode 100644 index 000000000..6ce986d69 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3366_minimum_array_sum/Solution.kt @@ -0,0 +1,80 @@ +package g3301_3400.s3366_minimum_array_sum + +// #Medium #Array #Dynamic_Programming #2024_12_03_Time_15_ms_(100.00%)_Space_39.5_MB_(92.86%) + +class Solution { + fun minArraySum(nums: IntArray, k: Int, op1: Int, op2: Int): Int { + var op1 = op1 + var op2 = op2 + nums.sort() + val high = lowerBound(nums, k * 2 - 1) + val low = lowerBound(nums, k) + val n = nums.size + for (i in n - 1 downTo high) { + if (op1 > 0) { + nums[i] = (nums[i] + 1) / 2 + op1-- + } + if (op2 > 0) { + nums[i] -= k + op2-- + } + } + val count: MutableMap = HashMap() + var odd = 0 + for (i in low.. 0) { + nums[i] -= k + if (k % 2 > 0 && nums[i] % 2 > 0) { + count.merge(nums[i], 1) { a: Int?, b: Int? -> Integer.sum(a!!, b!!) } + } + op2-- + } else { + odd += nums[i] % 2 + } + } + nums.sort(0, high) + var ans = 0 + if (k % 2 > 0) { + var i = high - op1 + while (i < high && odd > 0) { + val x = nums[i] + if (count.containsKey(x)) { + if (count.merge(x, -1) { a: Int?, b: Int? -> + Integer.sum(a!!, b!!) + } == 0 + ) { + count.remove(x) + } + odd-- + ans-- + } + i++ + } + } + var i = high - 1 + while (i >= 0 && op1 > 0) { + nums[i] = (nums[i] + 1) / 2 + i-- + op1-- + } + for (x in nums) { + ans += x + } + return ans + } + + private fun lowerBound(nums: IntArray, target: Int): Int { + var left = -1 + var right = nums.size + while (left + 1 < right) { + val mid = (left + right) ushr 1 + if (nums[mid] >= target) { + right = mid + } else { + left = mid + } + } + return right + } +} diff --git a/src/main/kotlin/g3301_3400/s3366_minimum_array_sum/readme.md b/src/main/kotlin/g3301_3400/s3366_minimum_array_sum/readme.md new file mode 100644 index 000000000..086dfd9db --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3366_minimum_array_sum/readme.md @@ -0,0 +1,46 @@ +3366\. Minimum Array Sum + +Medium + +You are given an integer array `nums` and three integers `k`, `op1`, and `op2`. + +You can perform the following operations on `nums`: + +* **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. +* **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. + +**Note:** Both operations can be applied to the same index, but at most once each. + +Return the **minimum** possible **sum** of all elements in `nums` after performing any number of operations. + +**Example 1:** + +**Input:** nums = [2,8,3,19,3], k = 3, op1 = 1, op2 = 1 + +**Output:** 23 + +**Explanation:** + +* Apply Operation 2 to `nums[1] = 8`, making `nums[1] = 5`. +* Apply Operation 1 to `nums[3] = 19`, making `nums[3] = 10`. +* The resulting array becomes `[2, 5, 3, 10, 3]`, which has the minimum possible sum of 23 after applying the operations. + +**Example 2:** + +**Input:** nums = [2,4,3], k = 3, op1 = 2, op2 = 1 + +**Output:** 3 + +**Explanation:** + +* Apply Operation 1 to `nums[0] = 2`, making `nums[0] = 1`. +* Apply Operation 1 to `nums[1] = 4`, making `nums[1] = 2`. +* Apply Operation 2 to `nums[2] = 3`, making `nums[2] = 0`. +* The resulting array becomes `[1, 2, 0]`, which has the minimum possible sum of 3 after applying the operations. + +**Constraints:** + +* `1 <= nums.length <= 100` +* 0 <= nums[i] <= 105 +* 0 <= k <= 105 +* `0 <= op1, op2 <= nums.length` \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/Solution.kt b/src/main/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/Solution.kt new file mode 100644 index 000000000..8beb4e525 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/Solution.kt @@ -0,0 +1,51 @@ +package g3301_3400.s3367_maximize_sum_of_weights_after_edge_removals + +// #Hard #Dynamic_Programming #Depth_First_Search #Tree +// #2024_12_03_Time_113_ms_(100.00%)_Space_141.5_MB_(81.82%) + +import java.util.PriorityQueue +import kotlin.math.max + +class Solution { + private lateinit var adj: Array> + private var k = 0 + + fun maximizeSumOfWeights(edges: Array, k: Int): Long { + val n = edges.size + 1 + adj = Array(n) { ArrayList() } + this.k = k + for (i in 0..() + } + for (e in edges) { + adj[e[0]].add(e) + adj[e[1]].add(e) + } + return dfs(0, -1)[1] + } + + private fun dfs(v: Int, parent: Int): LongArray { + var sum: Long = 0 + val pq = PriorityQueue() + for (e in adj[v]) { + val w = if (e[0] == v) e[1] else e[0] + if (w == parent) { + continue + } + val res = dfs(w, v) + val max = max(e[2] + res[0], res[1]) + sum += max + pq.add(max - res[1]) + } + val res = LongArray(2) + while (pq.size > k) { + sum -= pq.poll()!! + } + res[1] = sum + while (pq.size > k - 1) { + sum -= pq.poll()!! + } + res[0] = sum + return res + } +} diff --git a/src/main/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/readme.md b/src/main/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/readme.md new file mode 100644 index 000000000..e28eec246 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/readme.md @@ -0,0 +1,47 @@ +3367\. Maximize Sum of Weights after Edge Removals + +Hard + +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 edges[i] = [ui, vi, wi] indicates that there is an edge between nodes ui and vi with weight wi in the tree. + +Your task is to remove _zero or more_ edges such that: + +* Each node has an edge with **at most** `k` other nodes, where `k` is given. +* The sum of the weights of the remaining edges is **maximized**. + +Return the **maximum** possible sum of weights for the remaining edges after making the necessary removals. + +**Example 1:** + +**Input:** edges = [[0,1,4],[0,2,2],[2,3,12],[2,4,6]], k = 2 + +**Output:** 22 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/10/30/test1drawio.png) + +* 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. +* The sum of weights is 22, and we can't achieve a greater sum. Thus, the answer is 22. + +**Example 2:** + +**Input:** edges = [[0,1,5],[1,2,10],[0,3,15],[3,4,20],[3,5,5],[0,6,10]], k = 3 + +**Output:** 65 + +**Explanation:** + +* Since no node has edges connecting it to more than `k = 3` nodes, we don't remove any edges. +* The sum of weights is 65. Thus, the answer is 65. + +**Constraints:** + +* 2 <= n <= 105 +* `1 <= k <= n - 1` +* `edges.length == n - 1` +* `edges[i].length == 3` +* `0 <= edges[i][0] <= n - 1` +* `0 <= edges[i][1] <= n - 1` +* 1 <= edges[i][2] <= 106 +* The input is generated such that `edges` form a valid tree. \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3370_smallest_number_with_all_set_bits/Solution.kt b/src/main/kotlin/g3301_3400/s3370_smallest_number_with_all_set_bits/Solution.kt new file mode 100644 index 000000000..5342373a2 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3370_smallest_number_with_all_set_bits/Solution.kt @@ -0,0 +1,13 @@ +package g3301_3400.s3370_smallest_number_with_all_set_bits + +// #Easy #Math #Bit_Manipulation #2024_12_03_Time_0_ms_(100.00%)_Space_41.1_MB_(45.50%) + +class Solution { + fun smallestNumber(n: Int): Int { + var res = 1 + while (res < n) { + res = res * 2 + 1 + } + return res + } +} diff --git a/src/main/kotlin/g3301_3400/s3370_smallest_number_with_all_set_bits/readme.md b/src/main/kotlin/g3301_3400/s3370_smallest_number_with_all_set_bits/readme.md new file mode 100644 index 000000000..ce5862a88 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3370_smallest_number_with_all_set_bits/readme.md @@ -0,0 +1,43 @@ +3370\. Smallest Number With All Set Bits + +Easy + +You are given a _positive_ number `n`. + +Return the **smallest** number `x` **greater than** or **equal to** `n`, such that the binary representation of `x` contains only **set** bits. + +A **set** bit refers to a bit in the binary representation of a number that has a value of `1`. + +**Example 1:** + +**Input:** n = 5 + +**Output:** 7 + +**Explanation:** + +The binary representation of 7 is `"111"`. + +**Example 2:** + +**Input:** n = 10 + +**Output:** 15 + +**Explanation:** + +The binary representation of 15 is `"1111"`. + +**Example 3:** + +**Input:** n = 3 + +**Output:** 3 + +**Explanation:** + +The binary representation of 3 is `"11"`. + +**Constraints:** + +* `1 <= n <= 1000` \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3371_identify_the_largest_outlier_in_an_array/Solution.kt b/src/main/kotlin/g3301_3400/s3371_identify_the_largest_outlier_in_an_array/Solution.kt new file mode 100644 index 000000000..a2a141c44 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3371_identify_the_largest_outlier_in_an_array/Solution.kt @@ -0,0 +1,30 @@ +package g3301_3400.s3371_identify_the_largest_outlier_in_an_array + +// #Medium #Array #Hash_Table #Counting #Enumeration +// #2024_12_03_Time_5_ms_(100.00%)_Space_60.6_MB_(33.40%) + +class Solution { + fun getLargestOutlier(nums: IntArray): Int { + val cnt = IntArray(2001) + var sum = 0 + for (i in nums) { + sum += i + cnt[i + 1000]++ + } + for (i in cnt.indices.reversed()) { + val j = i - 1000 + if (cnt[i] == 0) { + continue + } + sum -= j + val csum = (sum shr 1) + 1000 + cnt[i]-- + if (sum % 2 == 0 && csum >= 0 && csum < cnt.size && cnt[csum] > 0) { + return j + } + sum += j + cnt[i]++ + } + return 0 + } +} diff --git a/src/main/kotlin/g3301_3400/s3371_identify_the_largest_outlier_in_an_array/readme.md b/src/main/kotlin/g3301_3400/s3371_identify_the_largest_outlier_in_an_array/readme.md new file mode 100644 index 000000000..091cd961f --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3371_identify_the_largest_outlier_in_an_array/readme.md @@ -0,0 +1,47 @@ +3371\. Identify the Largest Outlier in an Array + +Medium + +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**. + +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. + +**Note** that special numbers, the sum element, and the outlier must have **distinct** indices, but _may_ share the **same** value. + +Return the **largest** potential **outlier** in `nums`. + +**Example 1:** + +**Input:** nums = [2,3,5,10] + +**Output:** 10 + +**Explanation:** + +The special numbers could be 2 and 3, thus making their sum 5 and the outlier 10. + +**Example 2:** + +**Input:** nums = [-2,-1,-3,-6,4] + +**Output:** 4 + +**Explanation:** + +The special numbers could be -2, -1, and -3, thus making their sum -6 and the outlier 4. + +**Example 3:** + +**Input:** nums = [1,1,1,1,1,5,5] + +**Output:** 5 + +**Explanation:** + +The special numbers could be 1, 1, 1, 1, and 1, thus making their sum 5 and the other 5 as the outlier. + +**Constraints:** + +* 3 <= nums.length <= 105 +* `-1000 <= nums[i] <= 1000` +* The input is generated such that at least **one** potential outlier exists in `nums`. \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3372_maximize_the_number_of_target_nodes_after_connecting_trees_i/Solution.kt b/src/main/kotlin/g3301_3400/s3372_maximize_the_number_of_target_nodes_after_connecting_trees_i/Solution.kt new file mode 100644 index 000000000..a5f627c96 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3372_maximize_the_number_of_target_nodes_after_connecting_trees_i/Solution.kt @@ -0,0 +1,97 @@ +package g3301_3400.s3372_maximize_the_number_of_target_nodes_after_connecting_trees_i + +// #Medium #Depth_First_Search #Breadth_First_Search #Tree +// #2024_12_03_Time_50_ms_(99.49%)_Space_75.7_MB_(5.10%) + +import kotlin.math.max + +class Solution { + private fun getGraph(edges: Array): Array?> { + val n = edges.size + 1 + val graph: Array?> = arrayOfNulls?>(n) + for (i in 0..() + } + for (edge in edges) { + val u = edge[0] + val v = edge[1] + graph[u]!!.add(v) + graph[v]!!.add(u) + } + return graph + } + + private fun dfs(graph: Array?>, u: Int, pt: Int, dp: Array, k: Int) { + for (v in graph[u]!!) { + if (v == pt) { + continue + } + dfs(graph, v!!, u, dp, k) + for (i in 0..?>, + u: Int, + pt: Int, + ptv: IntArray, + fdp: Array, + dp: Array, + k: Int, + ) { + fdp[u]!![0] = dp[u]!![0] + for (i in 1..k) { + fdp[u]!![i] = (dp[u]!![i] + ptv[i - 1]) + } + for (v in graph[u]!!) { + if (v == pt) { + continue + } + val nptv = IntArray(k + 1) + for (i in 0.., k: Int): Array { + val graph = getGraph(edges) + val n = graph.size + val dp = Array(n) { IntArray(k + 1) } + val fdp = Array(n) { IntArray(k + 1) } + dfs(graph, 0, -1, dp, k) + dfs2(graph, 0, -1, IntArray(k + 1), fdp, dp, k) + for (i in 0.., edges2: Array, k: Int): IntArray { + val a = get(edges1, k) + val b = get(edges2, k) + val n = a.size + val m = b.size + val ans = IntArray(n) + var max = 0 + run { + var i = 0 + while (k != 0 && i < m) { + max = max(max.toDouble(), b[i]!![k - 1].toDouble()).toInt() + i++ + } + } + for (i in 0..edges1[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the first tree and edges2[i] = [ui, vi] indicates that there is an edge between nodes ui and vi in the second tree. You are also given an integer `k`. + +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. + +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. + +**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. + +**Example 1:** + +**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 + +**Output:** [9,7,9,8,8] + +**Explanation:** + +* For `i = 0`, connect node 0 from the first tree to node 0 from the second tree. +* For `i = 1`, connect node 1 from the first tree to node 0 from the second tree. +* For `i = 2`, connect node 2 from the first tree to node 4 from the second tree. +* For `i = 3`, connect node 3 from the first tree to node 4 from the second tree. +* For `i = 4`, connect node 4 from the first tree to node 4 from the second tree. + +![](https://assets.leetcode.com/uploads/2024/09/24/3982-1.png) + +**Example 2:** + +**Input:** edges1 = [[0,1],[0,2],[0,3],[0,4]], edges2 = [[0,1],[1,2],[2,3]], k = 1 + +**Output:** [6,3,3,3,3] + +**Explanation:** + +For every `i`, connect node `i` of the first tree with any node of the second tree. + +![](https://assets.leetcode.com/uploads/2024/09/24/3928-2.png) + +**Constraints:** + +* `2 <= n, m <= 1000` +* `edges1.length == n - 1` +* `edges2.length == m - 1` +* `edges1[i].length == edges2[i].length == 2` +* edges1[i] = [ai, bi] +* 0 <= ai, bi < n +* edges2[i] = [ui, vi] +* 0 <= ui, vi < m +* The input is generated such that `edges1` and `edges2` represent valid trees. +* `0 <= k <= 1000` \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3373_maximize_the_number_of_target_nodes_after_connecting_trees_ii/Solution.kt b/src/main/kotlin/g3301_3400/s3373_maximize_the_number_of_target_nodes_after_connecting_trees_ii/Solution.kt new file mode 100644 index 000000000..42494a4d7 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3373_maximize_the_number_of_target_nodes_after_connecting_trees_ii/Solution.kt @@ -0,0 +1,72 @@ +package g3301_3400.s3373_maximize_the_number_of_target_nodes_after_connecting_trees_ii + +// #Hard #Depth_First_Search #Breadth_First_Search #Tree +// #2024_12_03_Time_26_ms_(98.75%)_Space_114.7_MB_(80.00%) + +import kotlin.math.max + +class Solution { + fun maxTargetNodes(edges1: Array, edges2: Array): IntArray { + val n = edges1.size + 1 + val g1 = packU(n, edges1) + val m = edges2.size + 1 + val g2 = packU(m, edges2) + val p2 = parents(g2) + val eo2 = IntArray(2) + for (i in 0..): Array { + val n = g.size + val par = IntArray(n) + par.fill(-1) + val depth = IntArray(n) + depth[0] = 0 + val q = IntArray(n) + q[0] = 0 + var p = 0 + var r = 1 + while (p < r) { + val cur = q[p] + for (nex in g[cur]!!) { + if (par[cur] != nex) { + q[r++] = nex + par[nex] = cur + depth[nex] = depth[cur] + 1 + } + } + p++ + } + return arrayOf(par, q, depth) + } + + private fun packU(n: Int, ft: Array): Array { + val g = arrayOfNulls(n) + val p = IntArray(n) + for (u in ft) { + p[u[0]]++ + p[u[1]]++ + } + for (i in 0..edges1[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the first tree and edges2[i] = [ui, vi] indicates that there is an edge between nodes ui and vi in the second tree. + +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. + +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. + +**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. + +**Example 1:** + +**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]] + +**Output:** [8,7,7,8,8] + +**Explanation:** + +* For `i = 0`, connect node 0 from the first tree to node 0 from the second tree. +* For `i = 1`, connect node 1 from the first tree to node 4 from the second tree. +* For `i = 2`, connect node 2 from the first tree to node 7 from the second tree. +* For `i = 3`, connect node 3 from the first tree to node 0 from the second tree. +* For `i = 4`, connect node 4 from the first tree to node 4 from the second tree. + +![](https://assets.leetcode.com/uploads/2024/09/24/3982-1.png) + +**Example 2:** + +**Input:** edges1 = [[0,1],[0,2],[0,3],[0,4]], edges2 = [[0,1],[1,2],[2,3]] + +**Output:** [3,6,6,6,6] + +**Explanation:** + +For every `i`, connect node `i` of the first tree with any node of the second tree. + +![](https://assets.leetcode.com/uploads/2024/09/24/3928-2.png) + +**Constraints:** + +* 2 <= n, m <= 105 +* `edges1.length == n - 1` +* `edges2.length == m - 1` +* `edges1[i].length == edges2[i].length == 2` +* edges1[i] = [ai, bi] +* 0 <= ai, bi < n +* edges2[i] = [ui, vi] +* 0 <= ui, vi < m +* The input is generated such that `edges1` and `edges2` represent valid trees. \ No newline at end of file diff --git a/src/test/kotlin/g3301_3400/s3360_stone_removal_game/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3360_stone_removal_game/SolutionTest.kt new file mode 100644 index 000000000..1a1ce52cf --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3360_stone_removal_game/SolutionTest.kt @@ -0,0 +1,22 @@ +package g3301_3400.s3360_stone_removal_game + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun canAliceWin() { + assertThat(Solution().canAliceWin(12), equalTo(true)) + } + + @Test + fun canAliceWin2() { + assertThat(Solution().canAliceWin(1), equalTo(false)) + } + + @Test + fun canAliceWin3() { + assertThat(Solution().canAliceWin(19), equalTo(false)) + } +} diff --git a/src/test/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/SolutionTest.kt new file mode 100644 index 000000000..6c2685673 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/SolutionTest.kt @@ -0,0 +1,47 @@ +package g3301_3400.s3361_shift_distance_between_two_strings + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun shiftDistance() { + assertThat( + Solution() + .shiftDistance( + "abab", + "baba", + intArrayOf( + 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, + ), + intArrayOf( + 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, + ), + ), + equalTo(2L), + ) + } + + @Test + fun shiftDistance2() { + assertThat( + Solution() + .shiftDistance( + "leet", + "code", + intArrayOf( + 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, + ), + intArrayOf( + 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, + ), + ), + equalTo(31L), + ) + } +} diff --git a/src/test/kotlin/g3301_3400/s3362_zero_array_transformation_iii/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3362_zero_array_transformation_iii/SolutionTest.kt new file mode 100644 index 000000000..27c57506b --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3362_zero_array_transformation_iii/SolutionTest.kt @@ -0,0 +1,39 @@ +package g3301_3400.s3362_zero_array_transformation_iii + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maxRemoval() { + assertThat( + Solution() + .maxRemoval( + intArrayOf(2, 0, 2), + arrayOf(intArrayOf(0, 2), intArrayOf(0, 2), intArrayOf(1, 1)), + ), + equalTo(1), + ) + } + + @Test + fun maxRemoval2() { + assertThat( + Solution() + .maxRemoval( + intArrayOf(1, 1, 1, 1), + arrayOf(intArrayOf(1, 3), intArrayOf(0, 2), intArrayOf(1, 3), intArrayOf(1, 2)), + ), + equalTo(2), + ) + } + + @Test + fun maxRemoval3() { + assertThat( + Solution().maxRemoval(intArrayOf(1, 2, 3, 4), arrayOf(intArrayOf(0, 3))), + equalTo(-1), + ) + } +} diff --git a/src/test/kotlin/g3301_3400/s3363_find_the_maximum_number_of_fruits_collected/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3363_find_the_maximum_number_of_fruits_collected/SolutionTest.kt new file mode 100644 index 000000000..0800baff1 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3363_find_the_maximum_number_of_fruits_collected/SolutionTest.kt @@ -0,0 +1,36 @@ +package g3301_3400.s3363_find_the_maximum_number_of_fruits_collected + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maxCollectedFruits() { + assertThat( + Solution() + .maxCollectedFruits( + arrayOf( + intArrayOf(1, 2, 3, 4), + intArrayOf(5, 6, 8, 7), + intArrayOf(9, 10, 11, 12), + intArrayOf(13, 14, 15, 16), + ), + ), + equalTo(100), + ) + } + + @Test + fun maxCollectedFruits2() { + assertThat( + Solution().maxCollectedFruits( + arrayOf( + intArrayOf(1, 1), + intArrayOf(1, 1), + ), + ), + equalTo(4), + ) + } +} diff --git a/src/test/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/SolutionTest.kt new file mode 100644 index 000000000..c6110c125 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/SolutionTest.kt @@ -0,0 +1,31 @@ +package g3301_3400.s3364_minimum_positive_sum_subarray + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minimumSumSubarray() { + assertThat( + Solution().minimumSumSubarray(listOf(3, -2, 1, 4), 2, 3), + equalTo(1), + ) + } + + @Test + fun minimumSumSubarray2() { + assertThat( + Solution().minimumSumSubarray(listOf(-2, 2, -3, 1), 2, 3), + equalTo(-1), + ) + } + + @Test + fun minimumSumSubarray3() { + assertThat( + Solution().minimumSumSubarray(listOf(1, 2, 3, 4), 2, 4), + equalTo(3), + ) + } +} diff --git a/src/test/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/SolutionTest.kt new file mode 100644 index 000000000..1ece00b4e --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/SolutionTest.kt @@ -0,0 +1,34 @@ +package g3301_3400.s3365_rearrange_k_substrings_to_form_target_string + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun isPossibleToRearrange() { + assertThat( + Solution() + .isPossibleToRearrange("abcd", "cdab", 2), + equalTo(true), + ) + } + + @Test + fun isPossibleToRearrange2() { + assertThat( + Solution() + .isPossibleToRearrange("aabbcc", "bbaacc", 3), + equalTo(true), + ) + } + + @Test + fun isPossibleToRearrange3() { + assertThat( + Solution() + .isPossibleToRearrange("aabbcc", "bbaacc", 2), + equalTo(false), + ) + } +} diff --git a/src/test/kotlin/g3301_3400/s3366_minimum_array_sum/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3366_minimum_array_sum/SolutionTest.kt new file mode 100644 index 000000000..d2d8a5c78 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3366_minimum_array_sum/SolutionTest.kt @@ -0,0 +1,39 @@ +package g3301_3400.s3366_minimum_array_sum + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minArraySum() { + assertThat( + Solution().minArraySum(intArrayOf(2, 8, 3, 19, 3), 3, 1, 1), + equalTo(23), + ) + } + + @Test + fun minArraySum2() { + assertThat( + Solution().minArraySum(intArrayOf(2, 4, 3), 3, 2, 1), + equalTo(3), + ) + } + + @Test + fun minArraySum3() { + assertThat( + Solution() + .minArraySum( + intArrayOf( + 1, 3, 5, 7, 9, 12, 12, 12, 13, 15, 15, 15, 16, 17, 19, 20, + ), + 11, + 15, + 4, + ), + equalTo(77), + ) + } +} diff --git a/src/test/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/SolutionTest.kt new file mode 100644 index 000000000..afda61f02 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/SolutionTest.kt @@ -0,0 +1,54 @@ +package g3301_3400.s3367_maximize_sum_of_weights_after_edge_removals + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maximizeSumOfWeights() { + assertThat( + Solution() + .maximizeSumOfWeights( + arrayOf( + intArrayOf(0, 1, 4), + intArrayOf(0, 2, 2), + intArrayOf(2, 3, 12), + intArrayOf(2, 4, 6), + ), + 2, + ), + equalTo(22L), + ) + } + + @Test + fun maximizeSumOfWeights2() { + assertThat( + Solution() + .maximizeSumOfWeights( + arrayOf( + intArrayOf(0, 1, 5), + intArrayOf(1, 2, 10), + intArrayOf(0, 3, 15), + intArrayOf(3, 4, 20), + intArrayOf(3, 5, 5), + intArrayOf(0, 6, 10), + ), + 3, + ), + equalTo(65L), + ) + } + + @Test + fun maximizeSumOfWeights3() { + assertThat( + Solution().maximizeSumOfWeights( + arrayOf(intArrayOf(0, 1, 34), intArrayOf(0, 2, 17)), + 1, + ), + equalTo(34L), + ) + } +} diff --git a/src/test/kotlin/g3301_3400/s3370_smallest_number_with_all_set_bits/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3370_smallest_number_with_all_set_bits/SolutionTest.kt new file mode 100644 index 000000000..136f10459 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3370_smallest_number_with_all_set_bits/SolutionTest.kt @@ -0,0 +1,22 @@ +package g3301_3400.s3370_smallest_number_with_all_set_bits + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun smallestNumber() { + assertThat(Solution().smallestNumber(5), equalTo(7)) + } + + @Test + fun smallestNumber2() { + assertThat(Solution().smallestNumber(10), equalTo(15)) + } + + @Test + fun smallestNumber3() { + assertThat(Solution().smallestNumber(3), equalTo(3)) + } +} diff --git a/src/test/kotlin/g3301_3400/s3371_identify_the_largest_outlier_in_an_array/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3371_identify_the_largest_outlier_in_an_array/SolutionTest.kt new file mode 100644 index 000000000..cf36644cc --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3371_identify_the_largest_outlier_in_an_array/SolutionTest.kt @@ -0,0 +1,43 @@ +package g3301_3400.s3371_identify_the_largest_outlier_in_an_array + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun largestOutlier() { + assertThat( + Solution() + .getLargestOutlier(intArrayOf(2, 3, 5, 10)), + equalTo(10), + ) + } + + @Test + fun largestOutlier2() { + assertThat( + Solution() + .getLargestOutlier(intArrayOf(-2, -1, -3, -6, 4)), + equalTo(4), + ) + } + + @Test + fun largestOutlier3() { + assertThat( + Solution() + .getLargestOutlier(intArrayOf(1, 1, 1, 1, 1, 5, 5)), + equalTo(5), + ) + } + + @Test + fun largestOutlier4() { + assertThat( + Solution() + .getLargestOutlier(intArrayOf(-108, -108, -517)), + equalTo(-517), + ) + } +} diff --git a/src/test/kotlin/g3301_3400/s3372_maximize_the_number_of_target_nodes_after_connecting_trees_i/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3372_maximize_the_number_of_target_nodes_after_connecting_trees_i/SolutionTest.kt new file mode 100644 index 000000000..c4c87ab0d --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3372_maximize_the_number_of_target_nodes_after_connecting_trees_i/SolutionTest.kt @@ -0,0 +1,41 @@ +package g3301_3400.s3372_maximize_the_number_of_target_nodes_after_connecting_trees_i + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maxTargetNodes() { + assertThat( + Solution() + .maxTargetNodes( + arrayOf(intArrayOf(0, 1), intArrayOf(0, 2), intArrayOf(2, 3), intArrayOf(2, 4)), + arrayOf( + intArrayOf(0, 1), + intArrayOf(0, 2), + intArrayOf(0, 3), + intArrayOf(2, 7), + intArrayOf(1, 4), + intArrayOf(4, 5), + intArrayOf(4, 6), + ), + 2, + ), + equalTo(intArrayOf(9, 7, 9, 8, 8)), + ) + } + + @Test + fun maxTargetNodes2() { + assertThat( + Solution() + .maxTargetNodes( + arrayOf(intArrayOf(0, 1), intArrayOf(0, 2), intArrayOf(0, 3), intArrayOf(0, 4)), + arrayOf(intArrayOf(0, 1), intArrayOf(1, 2), intArrayOf(2, 3)), + 1, + ), + equalTo(intArrayOf(6, 3, 3, 3, 3)), + ) + } +} diff --git a/src/test/kotlin/g3301_3400/s3373_maximize_the_number_of_target_nodes_after_connecting_trees_ii/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3373_maximize_the_number_of_target_nodes_after_connecting_trees_ii/SolutionTest.kt new file mode 100644 index 000000000..65af994be --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3373_maximize_the_number_of_target_nodes_after_connecting_trees_ii/SolutionTest.kt @@ -0,0 +1,39 @@ +package g3301_3400.s3373_maximize_the_number_of_target_nodes_after_connecting_trees_ii + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maxTargetNodes() { + assertThat( + Solution() + .maxTargetNodes( + arrayOf(intArrayOf(0, 1), intArrayOf(0, 2), intArrayOf(2, 3), intArrayOf(2, 4)), + arrayOf( + intArrayOf(0, 1), + intArrayOf(0, 2), + intArrayOf(0, 3), + intArrayOf(2, 7), + intArrayOf(1, 4), + intArrayOf(4, 5), + intArrayOf(4, 6), + ), + ), + equalTo(intArrayOf(8, 7, 7, 8, 8)), + ) + } + + @Test + fun maxTargetNodes2() { + assertThat( + Solution() + .maxTargetNodes( + arrayOf(intArrayOf(0, 1), intArrayOf(0, 2), intArrayOf(0, 3), intArrayOf(0, 4)), + arrayOf(intArrayOf(0, 1), intArrayOf(1, 2), intArrayOf(2, 3)), + ), + equalTo(intArrayOf(3, 6, 6, 6, 6)), + ) + } +}