From c7a31a7b2e0aa58d14d40c43053f8bcab49e1392 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 16 Apr 2024 22:21:42 +0300 Subject: [PATCH] Added tasks 3070-3081 --- .../Solution.kt | 24 +++++ .../readme.md | 35 +++++++ .../Solution.kt | 50 ++++++++++ .../readme.md | 46 +++++++++ .../Solution.kt | 94 +++++++++++++++++++ .../readme.md | 59 ++++++++++++ .../Solution.kt | 34 +++++++ .../readme.md | 34 +++++++ .../Solution.kt | 17 ++++ .../readme.md | 52 ++++++++++ .../Solution.kt | 79 ++++++++++++++++ .../readme.md | 41 ++++++++ .../Solution.kt | 48 ++++++++++ .../readme.md | 45 +++++++++ .../Solution.kt | 31 ++++++ .../readme.md | 28 ++++++ .../Solution.kt | 84 +++++++++++++++++ .../readme.md | 47 ++++++++++ .../Solution.kt | 51 ++++++++++ .../readme.md | 53 +++++++++++ .../SolutionTest.kt | 36 +++++++ .../SolutionTest.kt | 34 +++++++ .../SolutionTest.kt | 31 ++++++ .../SolutionTest.kt | 23 +++++ .../SolutionTest.kt | 22 +++++ .../SolutionTest.kt | 23 +++++ .../SolutionTest.kt | 25 +++++ .../SolutionTest.kt | 17 ++++ .../SolutionTest.kt | 30 ++++++ .../SolutionTest.kt | 17 ++++ 30 files changed, 1210 insertions(+) create mode 100644 src/main/kotlin/g3001_3100/s3070_count_submatrices_with_top_left_element_and_sum_less_than_k/Solution.kt create mode 100644 src/main/kotlin/g3001_3100/s3070_count_submatrices_with_top_left_element_and_sum_less_than_k/readme.md create mode 100644 src/main/kotlin/g3001_3100/s3071_minimum_operations_to_write_the_letter_y_on_a_grid/Solution.kt create mode 100644 src/main/kotlin/g3001_3100/s3071_minimum_operations_to_write_the_letter_y_on_a_grid/readme.md create mode 100644 src/main/kotlin/g3001_3100/s3072_distribute_elements_into_two_arrays_ii/Solution.kt create mode 100644 src/main/kotlin/g3001_3100/s3072_distribute_elements_into_two_arrays_ii/readme.md create mode 100644 src/main/kotlin/g3001_3100/s3074_apple_redistribution_into_boxes/Solution.kt create mode 100644 src/main/kotlin/g3001_3100/s3074_apple_redistribution_into_boxes/readme.md create mode 100644 src/main/kotlin/g3001_3100/s3075_maximize_happiness_of_selected_children/Solution.kt create mode 100644 src/main/kotlin/g3001_3100/s3075_maximize_happiness_of_selected_children/readme.md create mode 100644 src/main/kotlin/g3001_3100/s3076_shortest_uncommon_substring_in_an_array/Solution.kt create mode 100644 src/main/kotlin/g3001_3100/s3076_shortest_uncommon_substring_in_an_array/readme.md create mode 100644 src/main/kotlin/g3001_3100/s3077_maximum_strength_of_k_disjoint_subarrays/Solution.kt create mode 100644 src/main/kotlin/g3001_3100/s3077_maximum_strength_of_k_disjoint_subarrays/readme.md create mode 100644 src/main/kotlin/g3001_3100/s3079_find_the_sum_of_encrypted_integers/Solution.kt create mode 100644 src/main/kotlin/g3001_3100/s3079_find_the_sum_of_encrypted_integers/readme.md create mode 100644 src/main/kotlin/g3001_3100/s3080_mark_elements_on_array_by_performing_queries/Solution.kt create mode 100644 src/main/kotlin/g3001_3100/s3080_mark_elements_on_array_by_performing_queries/readme.md create mode 100644 src/main/kotlin/g3001_3100/s3081_replace_question_marks_in_string_to_minimize_its_value/Solution.kt create mode 100644 src/main/kotlin/g3001_3100/s3081_replace_question_marks_in_string_to_minimize_its_value/readme.md create mode 100644 src/test/kotlin/g3001_3100/s3070_count_submatrices_with_top_left_element_and_sum_less_than_k/SolutionTest.kt create mode 100644 src/test/kotlin/g3001_3100/s3071_minimum_operations_to_write_the_letter_y_on_a_grid/SolutionTest.kt create mode 100644 src/test/kotlin/g3001_3100/s3072_distribute_elements_into_two_arrays_ii/SolutionTest.kt create mode 100644 src/test/kotlin/g3001_3100/s3074_apple_redistribution_into_boxes/SolutionTest.kt create mode 100644 src/test/kotlin/g3001_3100/s3075_maximize_happiness_of_selected_children/SolutionTest.kt create mode 100644 src/test/kotlin/g3001_3100/s3076_shortest_uncommon_substring_in_an_array/SolutionTest.kt create mode 100644 src/test/kotlin/g3001_3100/s3077_maximum_strength_of_k_disjoint_subarrays/SolutionTest.kt create mode 100644 src/test/kotlin/g3001_3100/s3079_find_the_sum_of_encrypted_integers/SolutionTest.kt create mode 100644 src/test/kotlin/g3001_3100/s3080_mark_elements_on_array_by_performing_queries/SolutionTest.kt create mode 100644 src/test/kotlin/g3001_3100/s3081_replace_question_marks_in_string_to_minimize_its_value/SolutionTest.kt diff --git a/src/main/kotlin/g3001_3100/s3070_count_submatrices_with_top_left_element_and_sum_less_than_k/Solution.kt b/src/main/kotlin/g3001_3100/s3070_count_submatrices_with_top_left_element_and_sum_less_than_k/Solution.kt new file mode 100644 index 00000000..28e815d4 --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3070_count_submatrices_with_top_left_element_and_sum_less_than_k/Solution.kt @@ -0,0 +1,24 @@ +package g3001_3100.s3070_count_submatrices_with_top_left_element_and_sum_less_than_k + +// #Medium #Array #Matrix #Prefix_Sum #2024_04_16_Time_773_ms_(85.71%)_Space_134.2_MB_(71.43%) + +class Solution { + fun countSubmatrices(grid: Array, k: Int): Int { + val n = grid[0].size + val sums = IntArray(n) + var ans = 0 + for (ints in grid) { + var sum = 0 + for (col in 0 until n) { + sum += ints[col] + sums[col] += sum + if (sums[col] <= k) { + ans++ + } else { + break + } + } + } + return ans + } +} diff --git a/src/main/kotlin/g3001_3100/s3070_count_submatrices_with_top_left_element_and_sum_less_than_k/readme.md b/src/main/kotlin/g3001_3100/s3070_count_submatrices_with_top_left_element_and_sum_less_than_k/readme.md new file mode 100644 index 00000000..760f1f3a --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3070_count_submatrices_with_top_left_element_and_sum_less_than_k/readme.md @@ -0,0 +1,35 @@ +3070\. Count Submatrices with Top-Left Element and Sum Less Than k + +Medium + +You are given a **0-indexed** integer matrix `grid` and an integer `k`. + +Return _the **number** of submatrices that contain the top-left element of the_ `grid`, _and have a sum less than or equal to_ `k`. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2024/01/01/example1.png) + +**Input:** grid = [[7,6,3],[6,6,1]], k = 18 + +**Output:** 4 + +**Explanation:** There are only 4 submatrices, shown in the image above, that contain the top-left element of grid, and have a sum less than or equal to 18. + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2024/01/01/example21.png) + +**Input:** grid = [[7,2,9],[1,5,0],[2,6,6]], k = 20 + +**Output:** 6 + +**Explanation:** There are only 6 submatrices, shown in the image above, that contain the top-left element of grid, and have a sum less than or equal to 20. + +**Constraints:** + +* `m == grid.length` +* `n == grid[i].length` +* `1 <= n, m <= 1000` +* `0 <= grid[i][j] <= 1000` +* 1 <= k <= 109 \ No newline at end of file diff --git a/src/main/kotlin/g3001_3100/s3071_minimum_operations_to_write_the_letter_y_on_a_grid/Solution.kt b/src/main/kotlin/g3001_3100/s3071_minimum_operations_to_write_the_letter_y_on_a_grid/Solution.kt new file mode 100644 index 00000000..ec602d6d --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3071_minimum_operations_to_write_the_letter_y_on_a_grid/Solution.kt @@ -0,0 +1,50 @@ +package g3001_3100.s3071_minimum_operations_to_write_the_letter_y_on_a_grid + +// #Medium #Array #Hash_Table #Matrix #Counting +// #2024_04_16_Time_268_ms_(91.11%)_Space_42.6_MB_(93.33%) + +import kotlin.math.min + +class Solution { + fun minimumOperationsToWriteY(arr: Array): Int { + val n = arr.size + val cnt1 = IntArray(3) + val cnt2 = IntArray(3) + val x = n / 2 + val y = n / 2 + for (j in x until n) { + cnt1[arr[j][y]]++ + arr[j][y] = 3 + } + for (j in x downTo 0) { + if (arr[j][j] != 3) { + cnt1[arr[j][j]]++ + } + arr[j][j] = 3 + } + for (j in x downTo 0) { + if (arr[j][n - 1 - j] != 3) { + cnt1[arr[j][n - 1 - j]]++ + } + arr[j][n - 1 - j] = 3 + } + for (ints in arr) { + for (j in 0 until n) { + if (ints[j] != 3) { + cnt2[ints[j]]++ + } + } + } + val s1 = cnt1[0] + cnt1[1] + cnt1[2] + val s2 = cnt2[0] + cnt2[1] + cnt2[2] + var min = Int.MAX_VALUE + for (i in 0..2) { + for (j in 0..2) { + if (i != j) { + min = min((s1 - cnt1[i] + s2 - cnt2[j]), min) + } + } + } + return min + } +} diff --git a/src/main/kotlin/g3001_3100/s3071_minimum_operations_to_write_the_letter_y_on_a_grid/readme.md b/src/main/kotlin/g3001_3100/s3071_minimum_operations_to_write_the_letter_y_on_a_grid/readme.md new file mode 100644 index 00000000..a3efc92b --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3071_minimum_operations_to_write_the_letter_y_on_a_grid/readme.md @@ -0,0 +1,46 @@ +3071\. Minimum Operations to Write the Letter Y on a Grid + +Medium + +You are given a **0-indexed** `n x n` grid where `n` is odd, and `grid[r][c]` is `0`, `1`, or `2`. + +We say that a cell belongs to the Letter **Y** if it belongs to one of the following: + +* The diagonal starting at the top-left cell and ending at the center cell of the grid. +* The diagonal starting at the top-right cell and ending at the center cell of the grid. +* The vertical line starting at the center cell and ending at the bottom border of the grid. + +The Letter **Y** is written on the grid if and only if: + +* All values at cells belonging to the Y are equal. +* All values at cells not belonging to the Y are equal. +* The values at cells belonging to the Y are different from the values at cells not belonging to the Y. + +Return _the **minimum** number of operations needed to write the letter Y on the grid given that in one operation you can change the value at any cell to_ `0`_,_ `1`_,_ _or_ `2`_._ + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2024/01/22/y2.png) + +**Input:** grid = [[1,2,2],[1,1,0],[0,1,0]] + +**Output:** 3 + +**Explanation:** We can write Y on the grid by applying the changes highlighted in blue in the image above. After the operations, all cells that belong to Y, denoted in bold, have the same value of 1 while those that do not belong to Y are equal to 0. It can be shown that 3 is the minimum number of operations needed to write Y on the grid. + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2024/01/22/y3.png) + +**Input:** grid = [[0,1,0,1,0],[2,1,0,1,2],[2,2,2,0,1],[2,2,2,2,2],[2,1,2,2,2]] + +**Output:** 12 + +**Explanation:** We can write Y on the grid by applying the changes highlighted in blue in the image above. After the operations, all cells that belong to Y, denoted in bold, have the same value of 0 while those that do not belong to Y are equal to 2. It can be shown that 12 is the minimum number of operations needed to write Y on the grid. + +**Constraints:** + +* `3 <= n <= 49` +* `n == grid.length == grid[i].length` +* `0 <= grid[i][j] <= 2` +* `n` is odd. \ No newline at end of file diff --git a/src/main/kotlin/g3001_3100/s3072_distribute_elements_into_two_arrays_ii/Solution.kt b/src/main/kotlin/g3001_3100/s3072_distribute_elements_into_two_arrays_ii/Solution.kt new file mode 100644 index 00000000..21133ca8 --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3072_distribute_elements_into_two_arrays_ii/Solution.kt @@ -0,0 +1,94 @@ +package g3001_3100.s3072_distribute_elements_into_two_arrays_ii + +// #Hard #Array #Simulation #Segment_Tree #Binary_Indexed_Tree +// #2024_04_16_Time_890_ms_(100.00%)_Space_77.6_MB_(54.17%) + +@Suppress("NAME_SHADOWING") +class Solution { + internal inner class BIT(size: Int) { + private val tree = IntArray(size + 1) + + fun update(ind: Int) { + var ind = ind + while (ind < tree.size) { + tree[ind]++ + ind += lsb(ind) + } + } + + fun rsq(ind: Int): Int { + var ind = ind + var sum = 0 + while (ind > 0) { + sum += tree[ind] + ind -= lsb(ind) + } + return sum + } + + private fun lsb(n: Int): Int { + return n and (-n) + } + } + + fun resultArray(source: IntArray): IntArray { + val nums = shrink(source) + val arr1 = IntArray(nums.size) + val arr2 = IntArray(nums.size) + arr1[0] = source[0] + arr2[0] = source[1] + var p1 = 0 + var p2 = 0 + val bit1 = BIT(nums.size) + bit1.update(nums[0]) + val bit2 = BIT(nums.size) + bit2.update(nums[1]) + for (i in 2 until nums.size) { + val g1 = p1 + 1 - bit1.rsq(nums[i]) + val g2 = p2 + 1 - bit2.rsq(nums[i]) + if (g1 > g2) { + p1++ + arr1[p1] = source[i] + bit1.update(nums[i]) + } else if (g1 < g2) { + p2++ + arr2[p2] = source[i] + bit2.update(nums[i]) + } else if (p1 < p2) { + p1++ + arr1[p1] = source[i] + bit1.update(nums[i]) + } else if (p1 > p2) { + p2++ + arr2[p2] = source[i] + bit2.update(nums[i]) + } else { + p1++ + arr1[p1] = source[i] + bit1.update(nums[i]) + } + } + for (i in p1 + 1 until arr1.size) { + arr1[i] = arr2[i - p1 - 1] + } + + return arr1 + } + + private fun shrink(nums: IntArray): IntArray { + val b = LongArray(nums.size) + for (i in nums.indices) { + b[i] = nums[i].toLong() shl 32 or i.toLong() + } + b.sort() + val result = IntArray(nums.size) + var p = 1 + for (i in nums.indices) { + if (i > 0 && (b[i] xor b[i - 1]) shr 32 != 0L) { + p++ + } + result[b[i].toInt()] = p + } + return result + } +} diff --git a/src/main/kotlin/g3001_3100/s3072_distribute_elements_into_two_arrays_ii/readme.md b/src/main/kotlin/g3001_3100/s3072_distribute_elements_into_two_arrays_ii/readme.md new file mode 100644 index 00000000..dbb65039 --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3072_distribute_elements_into_two_arrays_ii/readme.md @@ -0,0 +1,59 @@ +3072\. Distribute Elements Into Two Arrays II + +Hard + +You are given a **1-indexed** array of integers `nums` of length `n`. + +We define a function `greaterCount` such that `greaterCount(arr, val)` returns the number of elements in `arr` that are **strictly greater** than `val`. + +You need to distribute all the elements of `nums` between two arrays `arr1` and `arr2` using `n` operations. In the first operation, append `nums[1]` to `arr1`. In the second operation, append `nums[2]` to `arr2`. Afterwards, in the ith operation: + +* If `greaterCount(arr1, nums[i]) > greaterCount(arr2, nums[i])`, append `nums[i]` to `arr1`. +* If `greaterCount(arr1, nums[i]) < greaterCount(arr2, nums[i])`, append `nums[i]` to `arr2`. +* If `greaterCount(arr1, nums[i]) == greaterCount(arr2, nums[i])`, append `nums[i]` to the array with a **lesser** number of elements. +* If there is still a tie, append `nums[i]` to `arr1`. + +The array `result` is formed by concatenating the arrays `arr1` and `arr2`. For example, if `arr1 == [1,2,3]` and `arr2 == [4,5,6]`, then `result = [1,2,3,4,5,6]`. + +Return _the integer array_ `result`. + +**Example 1:** + +**Input:** nums = [2,1,3,3] + +**Output:** [2,3,1,3] + +**Explanation:** After the first 2 operations, arr1 = [2] and arr2 = [1]. + +In the 3rd operation, the number of elements greater than 3 is zero in both arrays. + +Also, the lengths are equal, hence, append nums[3] to arr1. In the 4th operation, the number of elements greater than 3 is zero in both arrays. As the length of arr2 is lesser, hence, append nums[4] to arr2. + +After 4 operations, arr1 = [2,3] and arr2 = [1,3]. Hence, the array result formed by concatenation is [2,3,1,3]. + +**Example 2:** + +**Input:** nums = [5,14,3,1,2] + +**Output:** [5,3,1,2,14] + +**Explanation:** After the first 2 operations, arr1 = [5] and arr2 = [14]. + +In the 3rd operation, the number of elements greater than 3 is one in both arrays. Also, the lengths are equal, hence, append nums[3] to arr1. + +In the 4th operation, the number of elements greater than 1 is greater in arr1 than arr2 (2 > 1). Hence, append nums[4] to arr1. In the 5th operation, the number of elements greater than 2 is greater in arr1 than arr2 (2 > 1). Hence, append nums[5] to arr1. + +zAfter 5 operations, arr1 = [5,3,1,2] and arr2 = [14]. Hence, the array result formed by concatenation is [5,3,1,2,14]. + +**Example 3:** + +**Input:** nums = [3,3,3,3] + +**Output:** [3,3,3,3] + +**Explanation:** At the end of 4 operations, arr1 = [3,3] and arr2 = [3,3]. Hence, the array result formed by concatenation is [3,3,3,3]. + +**Constraints:** + +* 3 <= n <= 105 +* 1 <= nums[i] <= 109 \ No newline at end of file diff --git a/src/main/kotlin/g3001_3100/s3074_apple_redistribution_into_boxes/Solution.kt b/src/main/kotlin/g3001_3100/s3074_apple_redistribution_into_boxes/Solution.kt new file mode 100644 index 00000000..aebfae3c --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3074_apple_redistribution_into_boxes/Solution.kt @@ -0,0 +1,34 @@ +package g3001_3100.s3074_apple_redistribution_into_boxes + +// #Easy #Array #Sorting #Greedy #2024_04_16_Time_168_ms_(97.37%)_Space_35.4_MB_(100.00%) + +import kotlin.math.max + +class Solution { + fun minimumBoxes(apple: IntArray, capacity: IntArray): Int { + val count = IntArray(51) + var appleSum = 0 + for (j in apple) { + appleSum += j + } + var reqCapacity = 0 + var max = 0 + for (j in capacity) { + count[j]++ + max = max(max.toDouble(), j.toDouble()).toInt() + } + for (i in max downTo 0) { + if (count[i] >= 1) { + while (count[i] != 0) { + appleSum -= i + reqCapacity++ + if (appleSum <= 0) { + return reqCapacity + } + count[i]-- + } + } + } + return reqCapacity + } +} diff --git a/src/main/kotlin/g3001_3100/s3074_apple_redistribution_into_boxes/readme.md b/src/main/kotlin/g3001_3100/s3074_apple_redistribution_into_boxes/readme.md new file mode 100644 index 00000000..a79cb4d2 --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3074_apple_redistribution_into_boxes/readme.md @@ -0,0 +1,34 @@ +3074\. Apple Redistribution into Boxes + +Easy + +You are given an array `apple` of size `n` and an array `capacity` of size `m`. + +There are `n` packs where the ith pack contains `apple[i]` apples. There are `m` boxes as well, and the ith box has a capacity of `capacity[i]` apples. + +Return _the **minimum** number of boxes you need to select to redistribute these_ `n` _packs of apples into boxes_. + +**Note** that, apples from the same pack can be distributed into different boxes. + +**Example 1:** + +**Input:** apple = [1,3,2], capacity = [4,3,1,5,2] + +**Output:** 2 + +**Explanation:** We will use boxes with capacities 4 and 5. It is possible to distribute the apples as the total capacity is greater than or equal to the total number of apples. + +**Example 2:** + +**Input:** apple = [5,5,5], capacity = [2,4,2,7] + +**Output:** 4 + +**Explanation:** We will need to use all the boxes. + +**Constraints:** + +* `1 <= n == apple.length <= 50` +* `1 <= m == capacity.length <= 50` +* `1 <= apple[i], capacity[i] <= 50` +* The input is generated such that it's possible to redistribute packs of apples into boxes. \ No newline at end of file diff --git a/src/main/kotlin/g3001_3100/s3075_maximize_happiness_of_selected_children/Solution.kt b/src/main/kotlin/g3001_3100/s3075_maximize_happiness_of_selected_children/Solution.kt new file mode 100644 index 00000000..9afb04c1 --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3075_maximize_happiness_of_selected_children/Solution.kt @@ -0,0 +1,17 @@ +package g3001_3100.s3075_maximize_happiness_of_selected_children + +// #Medium #Array #Sorting #Greedy #2024_04_16_Time_608_ms_(93.24%)_Space_73.6_MB_(66.22%) + +import kotlin.math.max + +class Solution { + fun maximumHappinessSum(happiness: IntArray, k: Int): Long { + happiness.sort() + var sum: Long = 0 + for (i in happiness.size - 1 downTo happiness.size - k) { + happiness[i] = max(0, happiness[i] - (happiness.size - 1 - i)) + sum += happiness[i].toLong() + } + return sum + } +} diff --git a/src/main/kotlin/g3001_3100/s3075_maximize_happiness_of_selected_children/readme.md b/src/main/kotlin/g3001_3100/s3075_maximize_happiness_of_selected_children/readme.md new file mode 100644 index 00000000..2826c464 --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3075_maximize_happiness_of_selected_children/readme.md @@ -0,0 +1,52 @@ +3075\. Maximize Happiness of Selected Children + +Medium + +You are given an array `happiness` of length `n`, and a **positive** integer `k`. + +There are `n` children standing in a queue, where the ith child has **happiness value** `happiness[i]`. You want to select `k` children from these `n` children in `k` turns. + +In each turn, when you select a child, the **happiness value** of all the children that have **not** been selected till now decreases by `1`. Note that the happiness value **cannot** become negative and gets decremented **only** if it is positive. + +Return _the **maximum** sum of the happiness values of the selected children you can achieve by selecting_ `k` _children_. + +**Example 1:** + +**Input:** happiness = [1,2,3], k = 2 + +**Output:** 4 + +**Explanation:** We can pick 2 children in the following way: +- Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1]. +- Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0. + +The sum of the happiness values of the selected children is 3 + 1 = 4. + +**Example 2:** + +**Input:** happiness = [1,1,1,1], k = 2 + +**Output:** 1 + +**Explanation:** We can pick 2 children in the following way: +- Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0]. +- Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0]. + +The sum of the happiness values of the selected children is 1 + 0 = 1. + +**Example 3:** + +**Input:** happiness = [2,3,4,5], k = 1 + +**Output:** 5 + +**Explanation:** We can pick 1 child in the following way: +- Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3]. + +The sum of the happiness values of the selected children is 5. + +**Constraints:** + +* 1 <= n == happiness.length <= 2 * 105 +* 1 <= happiness[i] <= 108 +* `1 <= k <= n` \ No newline at end of file diff --git a/src/main/kotlin/g3001_3100/s3076_shortest_uncommon_substring_in_an_array/Solution.kt b/src/main/kotlin/g3001_3100/s3076_shortest_uncommon_substring_in_an_array/Solution.kt new file mode 100644 index 00000000..aee30a5c --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3076_shortest_uncommon_substring_in_an_array/Solution.kt @@ -0,0 +1,79 @@ +package g3001_3100.s3076_shortest_uncommon_substring_in_an_array + +// #Medium #Array #String #Hash_Table #Trie #2024_04_16_Time_256_ms_(100.00%)_Space_39_MB_(96.36%) + +import kotlin.math.min + +class Solution { + private val root = Trie() + + fun shortestSubstrings(arr: Array): Array { + val n = arr.size + for (k in 0 until n) { + val s = arr[k] + val cs = s.toCharArray() + val m = cs.size + for (i in 0 until m) { + insert(cs, i, m, k) + } + } + val ans = arrayOfNulls(n) + for (k in 0 until n) { + val s = arr[k] + val cs = s.toCharArray() + val m = cs.size + var result = "" + var resultLen = m + 1 + for (i in 0 until m) { + val curLen = search( + cs, i, + min(m.toDouble(), (i + resultLen).toDouble()) + .toInt(), + k + ) + if (curLen != -1) { + val sub = String(cs, i, curLen) + if (curLen < resultLen || result.compareTo(sub) > 0) { + result = sub + resultLen = curLen + } + } + } + ans[k] = result + } + return ans + } + + private fun insert(cs: CharArray, start: Int, end: Int, wordIndex: Int) { + var curr: Trie? = root + for (i in start until end) { + val index = cs[i].code - 'a'.code + if (curr!!.children[index] == null) { + curr.children[index] = Trie() + } + curr = curr.children[index] + if (curr!!.wordIndex == -1 || curr.wordIndex == wordIndex) { + curr.wordIndex = wordIndex + } else { + curr.wordIndex = -2 + } + } + } + + private fun search(cs: CharArray, start: Int, end: Int, wordIndex: Int): Int { + var cur: Trie? = root + for (i in start until end) { + val index = cs[i].code - 'a'.code + cur = cur!!.children[index] + if (cur!!.wordIndex == wordIndex) { + return i - start + 1 + } + } + return -1 + } + + private class Trie { + var children: Array = arrayOfNulls(26) + var wordIndex: Int = -1 + } +} diff --git a/src/main/kotlin/g3001_3100/s3076_shortest_uncommon_substring_in_an_array/readme.md b/src/main/kotlin/g3001_3100/s3076_shortest_uncommon_substring_in_an_array/readme.md new file mode 100644 index 00000000..f0c5b481 --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3076_shortest_uncommon_substring_in_an_array/readme.md @@ -0,0 +1,41 @@ +3076\. Shortest Uncommon Substring in an Array + +Medium + +You are given an array `arr` of size `n` consisting of **non-empty** strings. + +Find a string array `answer` of size `n` such that: + +* `answer[i]` is the **shortest** substring of `arr[i]` that does **not** occur as a substring in any other string in `arr`. If multiple such substrings exist, `answer[i]` should be the lexicographically smallest. And if no such substring exists, `answer[i]` should be an empty string. + +Return _the array_ `answer`. + +**Example 1:** + +**Input:** arr = ["cab","ad","bad","c"] + +**Output:** ["ab","","ba",""] + +**Explanation:** We have the following: +- For the string "cab", the shortest substring that does not occur in any other string is either "ca" or "ab", we choose the lexicographically smaller substring, which is "ab". +- For the string "ad", there is no substring that does not occur in any other string. +- For the string "bad", the shortest substring that does not occur in any other string is "ba". +- For the string "c", there is no substring that does not occur in any other string. + +**Example 2:** + +**Input:** arr = ["abc","bcd","abcd"] + +**Output:** ["","","abcd"] + +**Explanation:** We have the following: +- For the string "abc", there is no substring that does not occur in any other string. +- For the string "bcd", there is no substring that does not occur in any other string. +- For the string "abcd", the shortest substring that does not occur in any other string is "abcd". + +**Constraints:** + +* `n == arr.length` +* `2 <= n <= 100` +* `1 <= arr[i].length <= 20` +* `arr[i]` consists only of lowercase English letters. \ No newline at end of file diff --git a/src/main/kotlin/g3001_3100/s3077_maximum_strength_of_k_disjoint_subarrays/Solution.kt b/src/main/kotlin/g3001_3100/s3077_maximum_strength_of_k_disjoint_subarrays/Solution.kt new file mode 100644 index 00000000..fac41afb --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3077_maximum_strength_of_k_disjoint_subarrays/Solution.kt @@ -0,0 +1,48 @@ +package g3001_3100.s3077_maximum_strength_of_k_disjoint_subarrays + +// #Hard #Array #Dynamic_Programming #Prefix_Sum +// #2024_04_16_Time_351_ms_(75.00%)_Space_51.3_MB_(60.00%) + +import kotlin.math.max + +class Solution { + fun maximumStrength(n: IntArray, k: Int): Long { + if (n.size == 1) { + return n[0].toLong() + } + val dp = Array(n.size) { LongArray(k) } + dp[0][0] = k.toLong() * n[0] + for (i in 1 until k) { + var pm: Long = -1 + dp[i][0] = (max(0, dp[i - 1][0]) + k.toLong() * n[i]) + for (j in 1 until i) { + dp[i][j] = ( + max( + dp[i - 1][j], + dp[i - 1][j - 1] + ) + (k.toLong() - j) * n[i] * pm + ) + pm = -pm + } + dp[i][i] = dp[i - 1][i - 1] + (k.toLong() - i) * n[i] * pm + } + var max = dp[k - 1][k - 1] + for (i in k until n.size) { + var pm: Long = 1 + dp[i][0] = (max(0, dp[i - 1][0]) + k.toLong() * n[i]) + for (j in 1 until k) { + pm = -pm + dp[i][j] = ( + max( + dp[i - 1][j], + dp[i - 1][j - 1] + ) + (k.toLong() - j) * n[i] * pm + ) + } + if (max < dp[i][k - 1]) { + max = dp[i][k - 1] + } + } + return max + } +} diff --git a/src/main/kotlin/g3001_3100/s3077_maximum_strength_of_k_disjoint_subarrays/readme.md b/src/main/kotlin/g3001_3100/s3077_maximum_strength_of_k_disjoint_subarrays/readme.md new file mode 100644 index 00000000..ded7a7f3 --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3077_maximum_strength_of_k_disjoint_subarrays/readme.md @@ -0,0 +1,45 @@ +3077\. Maximum Strength of K Disjoint Subarrays + +Hard + +You are given a **0-indexed** array of integers `nums` of length `n`, and a **positive** **odd** integer `k`. + +The strength of `x` subarrays is defined as `strength = sum[1] * x - sum[2] * (x - 1) + sum[3] * (x - 2) - sum[4] * (x - 3) + ... + sum[x] * 1` where `sum[i]` is the sum of the elements in the ith subarray. Formally, strength is sum of (-1)i+1 * sum[i] * (x - i + 1) over all `i`'s such that `1 <= i <= x`. + +You need to select `k` **disjoint subarrays** from `nums`, such that their **strength** is **maximum**. + +Return _the **maximum** possible **strength** that can be obtained_. + +**Note** that the selected subarrays **don't** need to cover the entire array. + +**Example 1:** + +**Input:** nums = [1,2,3,-1,2], k = 3 + +**Output:** 22 + +**Explanation:** The best possible way to select 3 subarrays is: nums[0..2], nums[3..3], and nums[4..4]. The strength is (1 + 2 + 3) \* 3 - (-1) \* 2 + 2 \* 1 = 22. + +**Example 2:** + +**Input:** nums = [12,-2,-2,-2,-2], k = 5 + +**Output:** 64 + +**Explanation:** The only possible way to select 5 disjoint subarrays is: nums[0..0], nums[1..1], nums[2..2], nums[3..3], and nums[4..4]. The strength is 12 \* 5 - (-2) \* 4 + (-2) \* 3 - (-2) \* 2 + (-2) \* 1 = 64. + +**Example 3:** + +**Input:** nums = [-1,-2,-3], k = 1 + +**Output:** -1 + +**Explanation:** The best possible way to select 1 subarray is: nums[0..0]. The strength is -1. + +**Constraints:** + +* 1 <= n <= 104 +* -109 <= nums[i] <= 109 +* `1 <= k <= n` +* 1 <= n * k <= 106 +* `k` is odd. \ No newline at end of file diff --git a/src/main/kotlin/g3001_3100/s3079_find_the_sum_of_encrypted_integers/Solution.kt b/src/main/kotlin/g3001_3100/s3079_find_the_sum_of_encrypted_integers/Solution.kt new file mode 100644 index 00000000..9039a784 --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3079_find_the_sum_of_encrypted_integers/Solution.kt @@ -0,0 +1,31 @@ +package g3001_3100.s3079_find_the_sum_of_encrypted_integers + +// #Easy #Array #Math #2024_04_16_Time_172_ms_(80.60%)_Space_36.6_MB_(89.55%) + +import kotlin.math.max + +class Solution { + private fun encrypt(x: Int): Int { + var x = x + var nDigits = 0 + var max = 0 + while (x > 0) { + max = max(max, (x % 10)) + x /= 10 + nDigits++ + } + var ans = 0 + for (i in 0 until nDigits) { + ans = ans * 10 + max + } + return ans + } + + fun sumOfEncryptedInt(nums: IntArray): Int { + var ret = 0 + for (num in nums) { + ret += encrypt(num) + } + return ret + } +} diff --git a/src/main/kotlin/g3001_3100/s3079_find_the_sum_of_encrypted_integers/readme.md b/src/main/kotlin/g3001_3100/s3079_find_the_sum_of_encrypted_integers/readme.md new file mode 100644 index 00000000..369fcc98 --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3079_find_the_sum_of_encrypted_integers/readme.md @@ -0,0 +1,28 @@ +3079\. Find the Sum of Encrypted Integers + +Easy + +You are given an integer array `nums` containing **positive** integers. We define a function `encrypt` such that `encrypt(x)` replaces **every** digit in `x` with the **largest** digit in `x`. For example, `encrypt(523) = 555` and `encrypt(213) = 333`. + +Return _the **sum** of encrypted elements_. + +**Example 1:** + +**Input:** nums = [1,2,3] + +**Output:** 6 + +**Explanation:** The encrypted elements are `[1,2,3]`. The sum of encrypted elements is `1 + 2 + 3 == 6`. + +**Example 2:** + +**Input:** nums = [10,21,31] + +**Output:** 66 + +**Explanation:** The encrypted elements are `[11,22,33]`. The sum of encrypted elements is `11 + 22 + 33 == 66`. + +**Constraints:** + +* `1 <= nums.length <= 50` +* `1 <= nums[i] <= 1000` \ No newline at end of file diff --git a/src/main/kotlin/g3001_3100/s3080_mark_elements_on_array_by_performing_queries/Solution.kt b/src/main/kotlin/g3001_3100/s3080_mark_elements_on_array_by_performing_queries/Solution.kt new file mode 100644 index 00000000..50207274 --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3080_mark_elements_on_array_by_performing_queries/Solution.kt @@ -0,0 +1,84 @@ +package g3001_3100.s3080_mark_elements_on_array_by_performing_queries + +// #Medium #Array #Hash_Table #Sorting #Heap_Priority_Queue #Simulation +// #2024_04_16_Time_937_ms_(97.78%)_Space_90.4_MB_(55.56%) + +class Solution { + fun unmarkedSumArray(nums: IntArray, queries: Array): LongArray { + val l = nums.size + val orig = IntArray(l) + for (i in 0 until l) { + orig[i] = i + } + var x = 1 + while (x < l) { + val temp = IntArray(l) + val teor = IntArray(l) + var y = 0 + while (y < l) { + var s1 = 0 + var s2 = 0 + while (s1 + s2 < 2 * x && y + s1 + s2 < l) { + if (s2 >= x || y + x + s2 >= l) { + temp[y + s1 + s2] = nums[y + s1] + teor[y + s1 + s2] = orig[y + s1] + s1++ + } else if (s1 >= x) { + temp[y + s1 + s2] = nums[y + x + s2] + teor[y + s1 + s2] = orig[y + x + s2] + s2++ + } else if (nums[y + s1] <= nums[y + x + s2]) { + temp[y + s1 + s2] = nums[y + s1] + teor[y + s1 + s2] = orig[y + s1] + s1++ + } else { + temp[y + s1 + s2] = nums[y + x + s2] + teor[y + s1 + s2] = orig[y + x + s2] + s2++ + } + } + y += 2 * x + } + for (i in 0 until l) { + nums[i] = temp[i] + orig[i] = teor[i] + } + x *= 2 + } + val change = IntArray(l) + for (i in 0 until l) { + change[orig[i]] = i + } + val mark = BooleanArray(l) + val m = queries.size + var st = 0 + var sum: Long = 0 + for (num in nums) { + sum += num.toLong() + } + val out = LongArray(m) + for (i in 0 until m) { + val a = queries[i][0] + if (!mark[change[a]]) { + mark[change[a]] = true + sum -= nums[change[a]].toLong() + } + val b = queries[i][1] + var many = 0 + while (many < b) { + if (st == l) { + out[i] = sum + break + } + if (!mark[st]) { + mark[st] = true + sum -= nums[st].toLong() + many++ + } + st++ + } + out[i] = sum + } + return out + } +} diff --git a/src/main/kotlin/g3001_3100/s3080_mark_elements_on_array_by_performing_queries/readme.md b/src/main/kotlin/g3001_3100/s3080_mark_elements_on_array_by_performing_queries/readme.md new file mode 100644 index 00000000..839656b3 --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3080_mark_elements_on_array_by_performing_queries/readme.md @@ -0,0 +1,47 @@ +3080\. Mark Elements on Array by Performing Queries + +Medium + +You are given a **0-indexed** array `nums` of size `n` consisting of positive integers. + +You are also given a 2D array `queries` of size `m` where queries[i] = [indexi, ki]. + +Initially all elements of the array are **unmarked**. + +You need to apply `m` queries on the array in order, where on the ith query you do the following: + +* Mark the element at index indexi if it is not already marked. +* Then mark ki unmarked elements in the array with the **smallest** values. If multiple such elements exist, mark the ones with the smallest indices. And if less than ki unmarked elements exist, then mark all of them. + +Return _an array answer of size_ `m` _where_ `answer[i]` _is the **sum** of unmarked elements in the array after the_ ith _query_. + +**Example 1:** + +**Input:** nums = [1,2,2,1,2,3,1], queries = [[1,2],[3,3],[4,2]] + +**Output:** [8,3,0] + +**Explanation:** + +We do the following queries on the array: + +* Mark the element at index `1`, and `2` of the smallest unmarked elements with the smallest indices if they exist, the marked elements now are nums = [**1**,**2**,2,**1**,2,3,1]. The sum of unmarked elements is `2 + 2 + 3 + 1 = 8`. +* Mark the element at index `3`, since it is already marked we skip it. Then we mark `3` of the smallest unmarked elements with the smallest indices, the marked elements now are nums = [**1**,**2**,**2**,**1**,**2**,3,**1**]. The sum of unmarked elements is `3`. +* Mark the element at index `4`, since it is already marked we skip it. Then we mark `2` of the smallest unmarked elements with the smallest indices if they exist, the marked elements now are nums = [**1**,**2**,**2**,**1**,**2**,**3**,**1**]. The sum of unmarked elements is `0`. + +**Example 2:** + +**Input:** nums = [1,4,2,3], queries = [[0,1]] + +**Output:** [7] + +**Explanation:** We do one query which is mark the element at index `0` and mark the smallest element among unmarked elements. The marked elements will be nums = [**1**,4,**2**,3], and the sum of unmarked elements is `4 + 3 = 7`. + +**Constraints:** + +* `n == nums.length` +* `m == queries.length` +* 1 <= m <= n <= 105 +* 1 <= nums[i] <= 105 +* `queries[i].length == 2` +* 0 <= indexi, ki <= n - 1 \ No newline at end of file diff --git a/src/main/kotlin/g3001_3100/s3081_replace_question_marks_in_string_to_minimize_its_value/Solution.kt b/src/main/kotlin/g3001_3100/s3081_replace_question_marks_in_string_to_minimize_its_value/Solution.kt new file mode 100644 index 00000000..d4d534fe --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3081_replace_question_marks_in_string_to_minimize_its_value/Solution.kt @@ -0,0 +1,51 @@ +package g3001_3100.s3081_replace_question_marks_in_string_to_minimize_its_value + +// #Medium #String #Hash_Table #Sorting #Greedy #Heap_Priority_Queue #Counting +// #2024_04_16_Time_249_ms_(100.00%)_Space_40.6_MB_(100.00%) + +import kotlin.math.min + +class Solution { + fun minimizeStringValue(s: String): String { + val n = s.length + var time = 0 + val count = IntArray(26) + val res = IntArray(26) + val str = s.toCharArray() + for (c in str) { + if (c != '?') { + count[c.code - 'a'.code]++ + } else { + time++ + } + } + var minTime = Int.MAX_VALUE + for (i in 0..25) { + minTime = min(minTime, count[i]) + } + while (time > 0) { + for (j in 0..25) { + if (count[j] == minTime) { + res[j]++ + count[j]++ + time-- + } + if (time == 0) { + break + } + } + minTime++ + } + var start = 0 + for (i in 0 until n) { + if (str[i] == '?') { + while (res[start] == 0) { + start++ + } + str[i] = ('a'.code + start).toChar() + res[start]-- + } + } + return String(str) + } +} diff --git a/src/main/kotlin/g3001_3100/s3081_replace_question_marks_in_string_to_minimize_its_value/readme.md b/src/main/kotlin/g3001_3100/s3081_replace_question_marks_in_string_to_minimize_its_value/readme.md new file mode 100644 index 00000000..1bf54f2a --- /dev/null +++ b/src/main/kotlin/g3001_3100/s3081_replace_question_marks_in_string_to_minimize_its_value/readme.md @@ -0,0 +1,53 @@ +3081\. Replace Question Marks in String to Minimize Its Value + +Medium + +You are given a string `s`. `s[i]` is either a lowercase English letter or `'?'`. + +For a string `t` having length `m` containing **only** lowercase English letters, we define the function `cost(i)` for an index `i` as the number of characters **equal** to `t[i]` that appeared before it, i.e. in the range `[0, i - 1]`. + +The **value** of `t` is the **sum** of `cost(i)` for all indices `i`. + +For example, for the string `t = "aab"`: + +* `cost(0) = 0` +* `cost(1) = 1` +* `cost(2) = 0` +* Hence, the value of `"aab"` is `0 + 1 + 0 = 1`. + +Your task is to **replace all** occurrences of `'?'` in `s` with any lowercase English letter so that the **value** of `s` is **minimized**. + +Return _a string denoting the modified string with replaced occurrences of_ `'?'`_. If there are multiple strings resulting in the **minimum value**, return the lexicographically smallest one._ + +**Example 1:** + +**Input:** s = "???" + +**Output:** "abc" + +**Explanation:** In this example, we can replace the occurrences of `'?'` to make `s` equal to `"abc"`. + +For `"abc"`, `cost(0) = 0`, `cost(1) = 0`, and `cost(2) = 0`. + +The value of `"abc"` is `0`. + +Some other modifications of `s` that have a value of `0` are `"cba"`, `"abz"`, and, `"hey"`. + +Among all of them, we choose the lexicographically smallest. + +**Example 2:** + +**Input:** s = "a?a?" + +**Output:** "abac" + +**Explanation:** In this example, the occurrences of `'?'` can be replaced to make `s` equal to `"abac"`. + +For `"abac"`, `cost(0) = 0`, `cost(1) = 0`, `cost(2) = 1`, and `cost(3) = 0`. + +The value of `"abac"` is `1`. + +**Constraints:** + +* 1 <= s.length <= 105 +* `s[i]` is either a lowercase English letter or `'?'`. \ No newline at end of file diff --git a/src/test/kotlin/g3001_3100/s3070_count_submatrices_with_top_left_element_and_sum_less_than_k/SolutionTest.kt b/src/test/kotlin/g3001_3100/s3070_count_submatrices_with_top_left_element_and_sum_less_than_k/SolutionTest.kt new file mode 100644 index 00000000..196dbaff --- /dev/null +++ b/src/test/kotlin/g3001_3100/s3070_count_submatrices_with_top_left_element_and_sum_less_than_k/SolutionTest.kt @@ -0,0 +1,36 @@ +package g3001_3100.s3070_count_submatrices_with_top_left_element_and_sum_less_than_k + +import com_github_leetcode.CommonUtils +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun countSubmatrices() { + assertThat( + Solution() + .countSubmatrices( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[7,6,3],[6,6,1]" + ), + 18 + ), + equalTo(4) + ) + } + + @Test + fun countSubmatrices2() { + assertThat( + Solution() + .countSubmatrices( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[7,2,9],[1,5,0],[2,6,6]" + ), + 20 + ), + equalTo(6) + ) + } +} diff --git a/src/test/kotlin/g3001_3100/s3071_minimum_operations_to_write_the_letter_y_on_a_grid/SolutionTest.kt b/src/test/kotlin/g3001_3100/s3071_minimum_operations_to_write_the_letter_y_on_a_grid/SolutionTest.kt new file mode 100644 index 00000000..16124e41 --- /dev/null +++ b/src/test/kotlin/g3001_3100/s3071_minimum_operations_to_write_the_letter_y_on_a_grid/SolutionTest.kt @@ -0,0 +1,34 @@ +package g3001_3100.s3071_minimum_operations_to_write_the_letter_y_on_a_grid + +import com_github_leetcode.CommonUtils +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minimumOperationsToWriteY() { + assertThat( + Solution() + .minimumOperationsToWriteY( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,2,2],[1,1,0],[0,1,0]" + ) + ), + equalTo(3) + ) + } + + @Test + fun minimumOperationsToWriteY2() { + assertThat( + Solution() + .minimumOperationsToWriteY( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[0,1,0,1,0],[2,1,0,1,2],[2,2,2,0,1],[2,2,2,2,2],[2,1,2,2,2]" + ) + ), + equalTo(12) + ) + } +} diff --git a/src/test/kotlin/g3001_3100/s3072_distribute_elements_into_two_arrays_ii/SolutionTest.kt b/src/test/kotlin/g3001_3100/s3072_distribute_elements_into_two_arrays_ii/SolutionTest.kt new file mode 100644 index 00000000..f3659638 --- /dev/null +++ b/src/test/kotlin/g3001_3100/s3072_distribute_elements_into_two_arrays_ii/SolutionTest.kt @@ -0,0 +1,31 @@ +package g3001_3100.s3072_distribute_elements_into_two_arrays_ii + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun resultArray() { + assertThat( + Solution().resultArray(intArrayOf(2, 1, 3, 3)), + equalTo(intArrayOf(2, 3, 1, 3)) + ) + } + + @Test + fun resultArray2() { + assertThat( + Solution().resultArray(intArrayOf(5, 14, 3, 1, 2)), + equalTo(intArrayOf(5, 3, 2, 14, 1)) + ) + } + + @Test + fun resultArray3() { + assertThat( + Solution().resultArray(intArrayOf(3, 3, 3, 3)), + equalTo(intArrayOf(3, 3, 3, 3)) + ) + } +} diff --git a/src/test/kotlin/g3001_3100/s3074_apple_redistribution_into_boxes/SolutionTest.kt b/src/test/kotlin/g3001_3100/s3074_apple_redistribution_into_boxes/SolutionTest.kt new file mode 100644 index 00000000..24dde01d --- /dev/null +++ b/src/test/kotlin/g3001_3100/s3074_apple_redistribution_into_boxes/SolutionTest.kt @@ -0,0 +1,23 @@ +package g3001_3100.s3074_apple_redistribution_into_boxes + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minimumBoxes() { + assertThat( + Solution().minimumBoxes(intArrayOf(1, 3, 2), intArrayOf(4, 3, 1, 5, 2)), + equalTo(2) + ) + } + + @Test + fun minimumBoxes2() { + assertThat( + Solution().minimumBoxes(intArrayOf(5, 5, 5), intArrayOf(2, 4, 2, 7)), + equalTo(4) + ) + } +} diff --git a/src/test/kotlin/g3001_3100/s3075_maximize_happiness_of_selected_children/SolutionTest.kt b/src/test/kotlin/g3001_3100/s3075_maximize_happiness_of_selected_children/SolutionTest.kt new file mode 100644 index 00000000..66118d48 --- /dev/null +++ b/src/test/kotlin/g3001_3100/s3075_maximize_happiness_of_selected_children/SolutionTest.kt @@ -0,0 +1,22 @@ +package g3001_3100.s3075_maximize_happiness_of_selected_children + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maximumHappinessSum() { + assertThat(Solution().maximumHappinessSum(intArrayOf(1, 2, 3), 2), equalTo(4L)) + } + + @Test + fun maximumHappinessSum2() { + assertThat(Solution().maximumHappinessSum(intArrayOf(1, 1, 1, 1), 2), equalTo(1L)) + } + + @Test + fun maximumHappinessSum3() { + assertThat(Solution().maximumHappinessSum(intArrayOf(2, 3, 4, 5), 1), equalTo(5L)) + } +} diff --git a/src/test/kotlin/g3001_3100/s3076_shortest_uncommon_substring_in_an_array/SolutionTest.kt b/src/test/kotlin/g3001_3100/s3076_shortest_uncommon_substring_in_an_array/SolutionTest.kt new file mode 100644 index 00000000..0804f178 --- /dev/null +++ b/src/test/kotlin/g3001_3100/s3076_shortest_uncommon_substring_in_an_array/SolutionTest.kt @@ -0,0 +1,23 @@ +package g3001_3100.s3076_shortest_uncommon_substring_in_an_array + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun shortestSubstrings() { + assertThat( + Solution().shortestSubstrings(arrayOf("cab", "ad", "bad", "c")), + equalTo(arrayOf("ab", "", "ba", "")) + ) + } + + @Test + fun shortestSubstrings2() { + assertThat( + Solution().shortestSubstrings(arrayOf("abc", "bcd", "abcd")), + equalTo(arrayOf("", "", "abcd")) + ) + } +} diff --git a/src/test/kotlin/g3001_3100/s3077_maximum_strength_of_k_disjoint_subarrays/SolutionTest.kt b/src/test/kotlin/g3001_3100/s3077_maximum_strength_of_k_disjoint_subarrays/SolutionTest.kt new file mode 100644 index 00000000..e9600b3f --- /dev/null +++ b/src/test/kotlin/g3001_3100/s3077_maximum_strength_of_k_disjoint_subarrays/SolutionTest.kt @@ -0,0 +1,25 @@ +package g3001_3100.s3077_maximum_strength_of_k_disjoint_subarrays + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maximumStrength() { + assertThat(Solution().maximumStrength(intArrayOf(1, 2, 3, -1, 2), 3), equalTo(22L)) + } + + @Test + fun maximumStrength2() { + assertThat( + Solution().maximumStrength(intArrayOf(12, -2, -2, -2, -2), 5), + equalTo(64L) + ) + } + + @Test + fun maximumStrength3() { + assertThat(Solution().maximumStrength(intArrayOf(-1, -2, -3), 1), equalTo(-1L)) + } +} diff --git a/src/test/kotlin/g3001_3100/s3079_find_the_sum_of_encrypted_integers/SolutionTest.kt b/src/test/kotlin/g3001_3100/s3079_find_the_sum_of_encrypted_integers/SolutionTest.kt new file mode 100644 index 00000000..63dbbc63 --- /dev/null +++ b/src/test/kotlin/g3001_3100/s3079_find_the_sum_of_encrypted_integers/SolutionTest.kt @@ -0,0 +1,17 @@ +package g3001_3100.s3079_find_the_sum_of_encrypted_integers + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun sumOfEncryptedInt() { + assertThat(Solution().sumOfEncryptedInt(intArrayOf(1, 2, 3)), equalTo(6)) + } + + @Test + fun sumOfEncryptedInt2() { + assertThat(Solution().sumOfEncryptedInt(intArrayOf(10, 21, 31)), equalTo(66)) + } +} diff --git a/src/test/kotlin/g3001_3100/s3080_mark_elements_on_array_by_performing_queries/SolutionTest.kt b/src/test/kotlin/g3001_3100/s3080_mark_elements_on_array_by_performing_queries/SolutionTest.kt new file mode 100644 index 00000000..9e37adbe --- /dev/null +++ b/src/test/kotlin/g3001_3100/s3080_mark_elements_on_array_by_performing_queries/SolutionTest.kt @@ -0,0 +1,30 @@ +package g3001_3100.s3080_mark_elements_on_array_by_performing_queries + +import com_github_leetcode.CommonUtils +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun unmarkedSumArray() { + assertThat( + Solution() + .unmarkedSumArray( + intArrayOf(1, 2, 2, 1, 2, 3, 1), + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,2],[3,3],[4,2]" + ) + ), + equalTo(longArrayOf(8, 3, 0)) + ) + } + + @Test + fun unmarkedSumArray2() { + assertThat( + Solution().unmarkedSumArray(intArrayOf(1, 4, 2, 3), arrayOf(intArrayOf(0, 1))), + equalTo(longArrayOf(7)) + ) + } +} diff --git a/src/test/kotlin/g3001_3100/s3081_replace_question_marks_in_string_to_minimize_its_value/SolutionTest.kt b/src/test/kotlin/g3001_3100/s3081_replace_question_marks_in_string_to_minimize_its_value/SolutionTest.kt new file mode 100644 index 00000000..c97a4dec --- /dev/null +++ b/src/test/kotlin/g3001_3100/s3081_replace_question_marks_in_string_to_minimize_its_value/SolutionTest.kt @@ -0,0 +1,17 @@ +package g3001_3100.s3081_replace_question_marks_in_string_to_minimize_its_value + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minimizeStringValue() { + assertThat(Solution().minimizeStringValue("???"), equalTo("abc")) + } + + @Test + fun minimizeStringValue2() { + assertThat(Solution().minimizeStringValue("a?a?"), equalTo("abac")) + } +}