diff --git a/src/main/kotlin/g3101_3200/s3142_check_if_grid_satisfies_conditions/Solution.kt b/src/main/kotlin/g3101_3200/s3142_check_if_grid_satisfies_conditions/Solution.kt new file mode 100644 index 000000000..4121763e1 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3142_check_if_grid_satisfies_conditions/Solution.kt @@ -0,0 +1,29 @@ +package g3101_3200.s3142_check_if_grid_satisfies_conditions + +// #Easy #Array #Matrix #2024_05_15_Time_170_ms_(91.84%)_Space_37.9_MB_(93.88%) + +class Solution { + fun satisfiesConditions(grid: Array): Boolean { + val m = grid.size + val n = grid[0].size + for (i in 0 until m - 1) { + if (n > 1) { + for (j in 0 until n - 1) { + if ((grid[i][j] != grid[i + 1][j]) || (grid[i][j] == grid[i][j + 1])) { + return false + } + } + } else { + if (grid[i][0] != grid[i + 1][0]) { + return false + } + } + } + for (j in 0 until n - 1) { + if (grid[m - 1][j] == grid[m - 1][j + 1]) { + return false + } + } + return true + } +} diff --git a/src/main/kotlin/g3101_3200/s3142_check_if_grid_satisfies_conditions/readme.md b/src/main/kotlin/g3101_3200/s3142_check_if_grid_satisfies_conditions/readme.md new file mode 100644 index 000000000..cafc24506 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3142_check_if_grid_satisfies_conditions/readme.md @@ -0,0 +1,51 @@ +3142\. Check if Grid Satisfies Conditions + +Easy + +You are given a 2D matrix `grid` of size `m x n`. You need to check if each cell `grid[i][j]` is: + +* Equal to the cell below it, i.e. `grid[i][j] == grid[i + 1][j]` (if it exists). +* Different from the cell to its right, i.e. `grid[i][j] != grid[i][j + 1]` (if it exists). + +Return `true` if **all** the cells satisfy these conditions, otherwise, return `false`. + +**Example 1:** + +**Input:** grid = [[1,0,2],[1,0,2]] + +**Output:** true + +**Explanation:** + +**![](https://assets.leetcode.com/uploads/2024/04/15/examplechanged.png)** + +All the cells in the grid satisfy the conditions. + +**Example 2:** + +**Input:** grid = [[1,1,1],[0,0,0]] + +**Output:** false + +**Explanation:** + +**![](https://assets.leetcode.com/uploads/2024/03/27/example21.png)** + +All cells in the first row are equal. + +**Example 3:** + +**Input:** grid = [[1],[2],[3]] + +**Output:** false + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/03/31/changed.png) + +Cells in the first column have different values. + +**Constraints:** + +* `1 <= n, m <= 10` +* `0 <= grid[i][j] <= 9` \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3143_maximum_points_inside_the_square/Solution.kt b/src/main/kotlin/g3101_3200/s3143_maximum_points_inside_the_square/Solution.kt new file mode 100644 index 000000000..d3c7b0286 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3143_maximum_points_inside_the_square/Solution.kt @@ -0,0 +1,35 @@ +package g3101_3200.s3143_maximum_points_inside_the_square + +// #Medium #Array #String #Hash_Table #Sorting #Binary_Search +// #2024_05_15_Time_650_ms_(59.52%)_Space_93.5_MB_(54.76%) + +import kotlin.math.abs +import kotlin.math.max +import kotlin.math.min + +class Solution { + fun maxPointsInsideSquare(points: Array, s: String): Int { + val tags = IntArray(26) + tags.fill(Int.MAX_VALUE) + var secondMin = Int.MAX_VALUE + for (i in s.indices) { + val dist = max(abs(points[i][0]), abs(points[i][1])) + val c = s[i] + if (tags[c.code - 'a'.code] == Int.MAX_VALUE) { + tags[c.code - 'a'.code] = dist + } else if (dist < tags[c.code - 'a'.code]) { + secondMin = min(secondMin, tags[c.code - 'a'.code]) + tags[c.code - 'a'.code] = dist + } else { + secondMin = min(secondMin, dist) + } + } + var count = 0 + for (dist in tags) { + if (dist < secondMin) { + count++ + } + } + return count + } +} diff --git a/src/main/kotlin/g3101_3200/s3143_maximum_points_inside_the_square/readme.md b/src/main/kotlin/g3101_3200/s3143_maximum_points_inside_the_square/readme.md new file mode 100644 index 000000000..621f127dd --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3143_maximum_points_inside_the_square/readme.md @@ -0,0 +1,57 @@ +3143\. Maximum Points Inside the Square + +Medium + +You are given a 2D array `points` and a string `s` where, `points[i]` represents the coordinates of point `i`, and `s[i]` represents the **tag** of point `i`. + +A **valid** square is a square centered at the origin `(0, 0)`, has edges parallel to the axes, and **does not** contain two points with the same tag. + +Return the **maximum** number of points contained in a **valid** square. + +Note: + +* A point is considered to be inside the square if it lies on or within the square's boundaries. +* The side length of the square can be zero. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2024/03/29/3708-tc1.png) + +**Input:** points = [[2,2],[-1,-2],[-4,4],[-3,1],[3,-3]], s = "abdca" + +**Output:** 2 + +**Explanation:** + +The square of side length 4 covers two points `points[0]` and `points[1]`. + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2024/03/29/3708-tc2.png) + +**Input:** points = [[1,1],[-2,-2],[-2,2]], s = "abb" + +**Output:** 1 + +**Explanation:** + +The square of side length 2 covers one point, which is `points[0]`. + +**Example 3:** + +**Input:** points = [[1,1],[-1,-1],[2,-2]], s = "ccd" + +**Output:** 0 + +**Explanation:** + +It's impossible to make any valid squares centered at the origin such that it covers only one point among `points[0]` and `points[1]`. + +**Constraints:** + +* 1 <= s.length, points.length <= 105 +* `points[i].length == 2` +* -109 <= points[i][0], points[i][1] <= 109 +* `s.length == points.length` +* `points` consists of distinct coordinates. +* `s` consists only of lowercase English letters. \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3144_minimum_substring_partition_of_equal_character_frequency/Solution.kt b/src/main/kotlin/g3101_3200/s3144_minimum_substring_partition_of_equal_character_frequency/Solution.kt new file mode 100644 index 000000000..d950f39d7 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3144_minimum_substring_partition_of_equal_character_frequency/Solution.kt @@ -0,0 +1,34 @@ +package g3101_3200.s3144_minimum_substring_partition_of_equal_character_frequency + +// #Medium #String #Hash_Table #Dynamic_Programming #Counting +// #2024_05_15_Time_232_ms_(88.00%)_Space_38.9_MB_(48.00%) + +import kotlin.math.min + +class Solution { + fun minimumSubstringsInPartition(s: String): Int { + val cs = s.toCharArray() + val n = cs.size + val dp = IntArray(n + 1) + dp.fill(n) + dp[0] = 0 + for (i in 1..n) { + val count = IntArray(26) + var distinct = 0 + var maxCount = 0 + for (j in i - 1 downTo 0) { + val index = cs[j].code - 'a'.code + if (++count[index] == 1) { + distinct++ + } + if (count[index] > maxCount) { + maxCount = count[index] + } + if (maxCount * distinct == i - j) { + dp[i] = min(dp[i], (dp[j] + 1)) + } + } + } + return dp[n] + } +} diff --git a/src/main/kotlin/g3101_3200/s3144_minimum_substring_partition_of_equal_character_frequency/readme.md b/src/main/kotlin/g3101_3200/s3144_minimum_substring_partition_of_equal_character_frequency/readme.md new file mode 100644 index 000000000..c8e5a8c73 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3144_minimum_substring_partition_of_equal_character_frequency/readme.md @@ -0,0 +1,34 @@ +3144\. Minimum Substring Partition of Equal Character Frequency + +Medium + +Given a string `s`, you need to partition it into one or more **balanced** substrings. For example, if `s == "ababcc"` then `("abab", "c", "c")`, `("ab", "abc", "c")`, and `("ababcc")` are all valid partitions, but ("a", **"bab"**, "cc"), (**"aba"**, "bc", "c"), and ("ab", **"abcc"**) are not. The unbalanced substrings are bolded. + +Return the **minimum** number of substrings that you can partition `s` into. + +**Note:** A **balanced** string is a string where each character in the string occurs the same number of times. + +**Example 1:** + +**Input:** s = "fabccddg" + +**Output:** 3 + +**Explanation:** + +We can partition the string `s` into 3 substrings in one of the following ways: `("fab, "ccdd", "g")`, or `("fabc", "cd", "dg")`. + +**Example 2:** + +**Input:** s = "abababaccddb" + +**Output:** 2 + +**Explanation:** + +We can partition the string `s` into 2 substrings like so: `("abab", "abaccddb")`. + +**Constraints:** + +* `1 <= s.length <= 1000` +* `s` consists only of English lowercase letters. \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3145_find_products_of_elements_of_big_array/Solution.kt b/src/main/kotlin/g3101_3200/s3145_find_products_of_elements_of_big_array/Solution.kt new file mode 100644 index 000000000..c1e4a71e7 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3145_find_products_of_elements_of_big_array/Solution.kt @@ -0,0 +1,59 @@ +package g3101_3200.s3145_find_products_of_elements_of_big_array + +// #Hard #Array #Binary_Search #Bit_Manipulation +// #2024_05_15_Time_210_ms_(100.00%)_Space_40_MB_(100.00%) + +class Solution { + fun findProductsOfElements(queries: Array): IntArray { + val ans = IntArray(queries.size) + for (i in queries.indices) { + val q = queries[i] + val er = sumE(q[1] + 1) + val el = sumE(q[0]) + ans[i] = pow(2, er - el, q[2]) + } + return ans + } + + private fun sumE(k: Long): Long { + var k = k + var res: Long = 0 + var n: Long = 0 + var cnt1: Long = 0 + var sumI: Long = 0 + for (i in 63L - java.lang.Long.numberOfLeadingZeros(k + 1) downTo 1) { + val c = (cnt1 shl i.toInt()) + (i shl (i - 1).toInt()) + if (c <= k) { + k -= c + res += (sumI shl i.toInt()) + ((i * (i - 1) / 2) shl (i - 1).toInt()) + sumI += i + cnt1++ + n = n or (1L shl i.toInt()) + } + } + if (cnt1 <= k) { + k -= cnt1 + res += sumI + n++ + } + while (k-- > 0) { + res += java.lang.Long.numberOfTrailingZeros(n).toLong() + n = n and n - 1 + } + return res + } + + private fun pow(x: Long, n: Long, mod: Long): Int { + var x = x + var n = n + var res = 1 % mod + while (n > 0) { + if (n % 2 == 1L) { + res = (res * x) % mod + } + x = (x * x) % mod + n /= 2 + } + return res.toInt() + } +} diff --git a/src/main/kotlin/g3101_3200/s3145_find_products_of_elements_of_big_array/readme.md b/src/main/kotlin/g3101_3200/s3145_find_products_of_elements_of_big_array/readme.md new file mode 100644 index 000000000..eaaf97bad --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3145_find_products_of_elements_of_big_array/readme.md @@ -0,0 +1,44 @@ +3145\. Find Products of Elements of Big Array + +Hard + +A **powerful array** for an integer `x` is the shortest sorted array of powers of two that sum up to `x`. For example, the powerful array for 11 is `[1, 2, 8]`. + +The array `big_nums` is created by concatenating the **powerful** arrays for every positive integer `i` in ascending order: 1, 2, 3, and so forth. Thus, `big_nums` starts as [1, 2, 1, 2, 4, 1, 4, 2, 4, 1, 2, 4, 8, ...]. + +You are given a 2D integer matrix `queries`, where for queries[i] = [fromi, toi, modi] you should calculate (big_nums[fromi] * big_nums[fromi + 1] * ... * big_nums[toi]) % modi. + +Return an integer array `answer` such that `answer[i]` is the answer to the ith query. + +**Example 1:** + +**Input:** queries = [[1,3,7]] + +**Output:** [4] + +**Explanation:** + +There is one query. + +`big_nums[1..3] = [2,1,2]`. The product of them is 4. The remainder of 4 under 7 is 4. + +**Example 2:** + +**Input:** queries = [[2,5,3],[7,7,4]] + +**Output:** [2,2] + +**Explanation:** + +There are two queries. + +First query: `big_nums[2..5] = [1,2,4,1]`. The product of them is 8. The remainder of 8 under 3 is 2. + +Second query: `big_nums[7] = 2`. The remainder of 2 under 4 is 2. + +**Constraints:** + +* `1 <= queries.length <= 500` +* `queries[i].length == 3` +* 0 <= queries[i][0] <= queries[i][1] <= 1015 +* 1 <= queries[i][2] <= 105 \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3146_permutation_difference_between_two_strings/Solution.kt b/src/main/kotlin/g3101_3200/s3146_permutation_difference_between_two_strings/Solution.kt new file mode 100644 index 000000000..4f6865958 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3146_permutation_difference_between_two_strings/Solution.kt @@ -0,0 +1,20 @@ +package g3101_3200.s3146_permutation_difference_between_two_strings + +// #Easy #String #Hash_Table #2024_05_15_Time_177_ms_(58.21%)_Space_38.1_MB_(7.46%) + +import kotlin.math.abs + +class Solution { + fun findPermutationDifference(s: String, t: String): Int { + val res = IntArray(26) + res.fill(-1) + var sum = 0 + for (i in s.indices) { + res[s[i].code - 'a'.code] = i + } + for (i in t.indices) { + sum += abs((res[t[i].code - 'a'.code] - i)) + } + return sum + } +} diff --git a/src/main/kotlin/g3101_3200/s3146_permutation_difference_between_two_strings/readme.md b/src/main/kotlin/g3101_3200/s3146_permutation_difference_between_two_strings/readme.md new file mode 100644 index 000000000..fbda75338 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3146_permutation_difference_between_two_strings/readme.md @@ -0,0 +1,40 @@ +3146\. Permutation Difference between Two Strings + +Easy + +You are given two strings `s` and `t` such that every character occurs at most once in `s` and `t` is a permutation of `s`. + +The **permutation difference** between `s` and `t` is defined as the **sum** of the absolute difference between the index of the occurrence of each character in `s` and the index of the occurrence of the same character in `t`. + +Return the **permutation difference** between `s` and `t`. + +**Example 1:** + +**Input:** s = "abc", t = "bac" + +**Output:** 2 + +**Explanation:** + +For `s = "abc"` and `t = "bac"`, the permutation difference of `s` and `t` is equal to the sum of: + +* The absolute difference between the index of the occurrence of `"a"` in `s` and the index of the occurrence of `"a"` in `t`. +* The absolute difference between the index of the occurrence of `"b"` in `s` and the index of the occurrence of `"b"` in `t`. +* The absolute difference between the index of the occurrence of `"c"` in `s` and the index of the occurrence of `"c"` in `t`. + +That is, the permutation difference between `s` and `t` is equal to `|0 - 1| + |2 - 2| + |1 - 0| = 2`. + +**Example 2:** + +**Input:** s = "abcde", t = "edbac" + +**Output:** 12 + +**Explanation:** The permutation difference between `s` and `t` is equal to `|0 - 3| + |1 - 2| + |2 - 4| + |3 - 1| + |4 - 0| = 12`. + +**Constraints:** + +* `1 <= s.length <= 26` +* Each character occurs at most once in `s`. +* `t` is a permutation of `s`. +* `s` consists only of lowercase English letters. \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3147_taking_maximum_energy_from_the_mystic_dungeon/Solution.kt b/src/main/kotlin/g3101_3200/s3147_taking_maximum_energy_from_the_mystic_dungeon/Solution.kt new file mode 100644 index 000000000..0c330a1df --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3147_taking_maximum_energy_from_the_mystic_dungeon/Solution.kt @@ -0,0 +1,22 @@ +package g3101_3200.s3147_taking_maximum_energy_from_the_mystic_dungeon + +// #Medium #Array #Prefix_Sum #2024_05_15_Time_671_ms_(79.17%)_Space_60.9_MB_(97.92%) + +import kotlin.math.max + +class Solution { + fun maximumEnergy(energy: IntArray, k: Int): Int { + var max = Int.MIN_VALUE + val n = energy.size + for (i in n - 1 downTo n - k) { + var en = 0 + var j = i + while (j >= 0) { + en += energy[j] + max = max(en, max) + j -= k + } + } + return max + } +} diff --git a/src/main/kotlin/g3101_3200/s3147_taking_maximum_energy_from_the_mystic_dungeon/readme.md b/src/main/kotlin/g3101_3200/s3147_taking_maximum_energy_from_the_mystic_dungeon/readme.md new file mode 100644 index 000000000..ff311bf56 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3147_taking_maximum_energy_from_the_mystic_dungeon/readme.md @@ -0,0 +1,33 @@ +3147\. Taking Maximum Energy From the Mystic Dungeon + +Medium + +In a mystic dungeon, `n` magicians are standing in a line. Each magician has an attribute that gives you energy. Some magicians can give you negative energy, which means taking energy from you. + +You have been cursed in such a way that after absorbing energy from magician `i`, you will be instantly transported to magician `(i + k)`. This process will be repeated until you reach the magician where `(i + k)` does not exist. + +In other words, you will choose a starting point and then teleport with `k` jumps until you reach the end of the magicians' sequence, **absorbing all the energy** during the journey. + +You are given an array `energy` and an integer `k`. Return the **maximum** possible energy you can gain. + +**Example 1:** + +**Input:** energy = [5,2,-10,-5,1], k = 3 + +**Output:** 3 + +**Explanation:** We can gain a total energy of 3 by starting from magician 1 absorbing 2 + 1 = 3. + +**Example 2:** + +**Input:** energy = [-2,-3,-1], k = 2 + +**Output:** -1 + +**Explanation:** We can gain a total energy of -1 by starting from magician 2. + +**Constraints:** + +* 1 <= energy.length <= 105 +* `-1000 <= energy[i] <= 1000` +* `1 <= k <= energy.length - 1` \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3148_maximum_difference_score_in_a_grid/Solution.kt b/src/main/kotlin/g3101_3200/s3148_maximum_difference_score_in_a_grid/Solution.kt new file mode 100644 index 000000000..59f1e46f7 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3148_maximum_difference_score_in_a_grid/Solution.kt @@ -0,0 +1,36 @@ +package g3101_3200.s3148_maximum_difference_score_in_a_grid + +// #Medium #Array #Dynamic_Programming #Matrix +// #2024_05_15_Time_777_ms_(84.62%)_Space_73.3_MB_(79.49%) + +import kotlin.math.max + +class Solution { + fun maxScore(grid: List>): Int { + val m = grid.size - 1 + var row = grid[m] + var n = row.size + val maxRB = IntArray(n--) + maxRB[n] = row[n] + var mx = maxRB[n] + var result = Int.MIN_VALUE + for (i in n - 1 downTo 0) { + val x = row[i] + result = max(result, (mx - x)) + mx = max(mx, x) + maxRB[i] = mx + } + for (i in m - 1 downTo 0) { + row = grid[i] + mx = 0 + for (j in n downTo 0) { + mx = max(mx, maxRB[j]) + val x = row[j] + result = max(result, (mx - x)) + mx = max(mx, x) + maxRB[j] = mx + } + } + return result + } +} diff --git a/src/main/kotlin/g3101_3200/s3148_maximum_difference_score_in_a_grid/readme.md b/src/main/kotlin/g3101_3200/s3148_maximum_difference_score_in_a_grid/readme.md new file mode 100644 index 000000000..c2fe847e5 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3148_maximum_difference_score_in_a_grid/readme.md @@ -0,0 +1,43 @@ +3148\. Maximum Difference Score in a Grid + +Medium + +You are given an `m x n` matrix `grid` consisting of **positive** integers. You can move from a cell in the matrix to **any** other cell that is either to the bottom or to the right (not necessarily adjacent). The score of a move from a cell with the value `c1` to a cell with the value `c2` is `c2 - c1`. + +You can start at **any** cell, and you have to make **at least** one move. + +Return the **maximum** total score you can achieve. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2024/03/14/grid1.png) + +**Input:** grid = [[9,5,7,3],[8,9,6,1],[6,7,14,3],[2,5,3,1]] + +**Output:** 9 + +**Explanation:** We start at the cell `(0, 1)`, and we perform the following moves: + +- Move from the cell `(0, 1)` to `(2, 1)` with a score of `7 - 5 = 2`. + +- Move from the cell `(2, 1)` to `(2, 2)` with a score of `14 - 7 = 7`. + +The total score is `2 + 7 = 9`. + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2024/04/08/moregridsdrawio-1.png) + +**Input:** grid = [[4,3,2],[3,2,1]] + +**Output:** \-1 + +**Explanation:** We start at the cell `(0, 0)`, and we perform one move: `(0, 0)` to `(0, 1)`. The score is `3 - 4 = -1`. + +**Constraints:** + +* `m == grid.length` +* `n == grid[i].length` +* `2 <= m, n <= 1000` +* 4 <= m * n <= 105 +* 1 <= grid[i][j] <= 105 \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3149_find_the_minimum_cost_array_permutation/Solution.kt b/src/main/kotlin/g3101_3200/s3149_find_the_minimum_cost_array_permutation/Solution.kt new file mode 100644 index 000000000..6cd49da85 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3149_find_the_minimum_cost_array_permutation/Solution.kt @@ -0,0 +1,69 @@ +package g3101_3200.s3149_find_the_minimum_cost_array_permutation + +// #Hard #Array #Dynamic_Programming #Bit_Manipulation #Bitmask +// #2024_05_15_Time_329_ms_(100.00%)_Space_40.9_MB_(66.67%) + +import kotlin.math.abs +import kotlin.math.min + +class Solution { + private fun findMinScore(mask: Int, prevNum: Int, nums: IntArray, dp: Array): Int { + val n = nums.size + if (Integer.bitCount(mask) == n) { + dp[mask][prevNum] = abs((prevNum - nums[0])) + return dp[mask][prevNum] + } + if (dp[mask][prevNum] != -1) { + return dp[mask][prevNum] + } + var minScore = Int.MAX_VALUE + for (currNum in 0 until n) { + if ((mask shr currNum and 1 xor 1) == 1) { + val currScore = ( + abs((prevNum - nums[currNum])) + findMinScore( + mask or (1 shl currNum), + currNum, + nums, + dp + ) + ) + minScore = min(minScore, currScore) + } + } + return minScore.also { dp[mask][prevNum] = it } + } + + private fun constructMinScorePermutation(n: Int, nums: IntArray, dp: Array): IntArray { + val permutation = IntArray(n) + var i = 0 + permutation[i++] = 0 + var prevNum = 0 + var mask = 1 + while (i < n) { + for (currNum in 0 until n) { + if ((mask shr currNum and 1 xor 1) == 1) { + val currScore = + (abs((prevNum - nums[currNum])) + dp[mask or (1 shl currNum)][currNum]) + val minScore = dp[mask][prevNum] + if (currScore == minScore) { + permutation[i++] = currNum + prevNum = currNum + break + } + } + } + mask = mask or (1 shl prevNum) + } + return permutation + } + + fun findPermutation(nums: IntArray): IntArray { + val n = nums.size + val dp = Array(1 shl n) { IntArray(n) } + for (row in dp) { + row.fill(-1) + } + findMinScore(1, 0, nums, dp) + return constructMinScorePermutation(n, nums, dp) + } +} diff --git a/src/main/kotlin/g3101_3200/s3149_find_the_minimum_cost_array_permutation/readme.md b/src/main/kotlin/g3101_3200/s3149_find_the_minimum_cost_array_permutation/readme.md new file mode 100644 index 000000000..0a07e6091 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3149_find_the_minimum_cost_array_permutation/readme.md @@ -0,0 +1,38 @@ +3149\. Find the Minimum Cost Array Permutation + +Hard + +You are given an array `nums` which is a permutation of `[0, 1, 2, ..., n - 1]`. The **score** of any permutation of `[0, 1, 2, ..., n - 1]` named `perm` is defined as: + +`score(perm) = |perm[0] - nums[perm[1]]| + |perm[1] - nums[perm[2]]| + ... + |perm[n - 1] - nums[perm[0]]|` + +Return the permutation `perm` which has the **minimum** possible score. If _multiple_ permutations exist with this score, return the one that is lexicographically smallest among them. + +**Example 1:** + +**Input:** nums = [1,0,2] + +**Output:** [0,1,2] + +**Explanation:** + +**![](https://assets.leetcode.com/uploads/2024/04/04/example0gif.gif)** + +The lexicographically smallest permutation with minimum cost is `[0,1,2]`. The cost of this permutation is `|0 - 0| + |1 - 2| + |2 - 1| = 2`. + +**Example 2:** + +**Input:** nums = [0,2,1] + +**Output:** [0,2,1] + +**Explanation:** + +**![](https://assets.leetcode.com/uploads/2024/04/04/example1gif.gif)** + +The lexicographically smallest permutation with minimum cost is `[0,2,1]`. The cost of this permutation is `|0 - 1| + |2 - 2| + |1 - 0| = 2`. + +**Constraints:** + +* `2 <= n == nums.length <= 14` +* `nums` is a permutation of `[0, 1, 2, ..., n - 1]`. \ No newline at end of file diff --git a/src/test/kotlin/g3101_3200/s3142_check_if_grid_satisfies_conditions/SolutionTest.kt b/src/test/kotlin/g3101_3200/s3142_check_if_grid_satisfies_conditions/SolutionTest.kt new file mode 100644 index 000000000..48a3cf034 --- /dev/null +++ b/src/test/kotlin/g3101_3200/s3142_check_if_grid_satisfies_conditions/SolutionTest.kt @@ -0,0 +1,23 @@ +package g3101_3200.s3142_check_if_grid_satisfies_conditions + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun satisfiesConditions() { + assertThat( + Solution().satisfiesConditions(arrayOf(intArrayOf(1, 0, 2), intArrayOf(1, 0, 2))), + equalTo(true) + ) + } + + @Test + fun satisfiesConditions2() { + assertThat( + Solution().satisfiesConditions(arrayOf(intArrayOf(1, 1, 1), intArrayOf(0, 0, 0))), + equalTo(false) + ) + } +} diff --git a/src/test/kotlin/g3101_3200/s3143_maximum_points_inside_the_square/SolutionTest.kt b/src/test/kotlin/g3101_3200/s3143_maximum_points_inside_the_square/SolutionTest.kt new file mode 100644 index 000000000..74d161a62 --- /dev/null +++ b/src/test/kotlin/g3101_3200/s3143_maximum_points_inside_the_square/SolutionTest.kt @@ -0,0 +1,34 @@ +package g3101_3200.s3143_maximum_points_inside_the_square + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maxPointsInsideSquare() { + assertThat( + Solution() + .maxPointsInsideSquare( + arrayOf( + intArrayOf(2, 2), + intArrayOf(-1, -2), + intArrayOf(-4, 4), + intArrayOf(-3, 1), + intArrayOf(3, -3) + ), + "abdca" + ), + equalTo(2) + ) + } + + @Test + fun maxPointsInsideSquare2() { + assertThat( + Solution() + .maxPointsInsideSquare(arrayOf(intArrayOf(1, 1), intArrayOf(-2, -2), intArrayOf(-2, 2)), "abb"), + equalTo(1) + ) + } +} diff --git a/src/test/kotlin/g3101_3200/s3144_minimum_substring_partition_of_equal_character_frequency/SolutionTest.kt b/src/test/kotlin/g3101_3200/s3144_minimum_substring_partition_of_equal_character_frequency/SolutionTest.kt new file mode 100644 index 000000000..702e270b8 --- /dev/null +++ b/src/test/kotlin/g3101_3200/s3144_minimum_substring_partition_of_equal_character_frequency/SolutionTest.kt @@ -0,0 +1,17 @@ +package g3101_3200.s3144_minimum_substring_partition_of_equal_character_frequency + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minimumSubstringsInPartition() { + assertThat(Solution().minimumSubstringsInPartition("fabccddg"), equalTo(3)) + } + + @Test + fun minimumSubstringsInPartition2() { + assertThat(Solution().minimumSubstringsInPartition("abababaccddb"), equalTo(2)) + } +} diff --git a/src/test/kotlin/g3101_3200/s3145_find_products_of_elements_of_big_array/SolutionTest.kt b/src/test/kotlin/g3101_3200/s3145_find_products_of_elements_of_big_array/SolutionTest.kt new file mode 100644 index 000000000..ca7397de3 --- /dev/null +++ b/src/test/kotlin/g3101_3200/s3145_find_products_of_elements_of_big_array/SolutionTest.kt @@ -0,0 +1,23 @@ +package g3101_3200.s3145_find_products_of_elements_of_big_array + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun findProductsOfElements() { + assertThat( + Solution().findProductsOfElements(arrayOf(longArrayOf(1, 3, 7))), + equalTo(intArrayOf(4)) + ) + } + + @Test + fun findProductsOfElements2() { + assertThat( + Solution().findProductsOfElements(arrayOf(longArrayOf(2, 5, 3), longArrayOf(7, 7, 4))), + equalTo(intArrayOf(2, 2)) + ) + } +} diff --git a/src/test/kotlin/g3101_3200/s3146_permutation_difference_between_two_strings/SolutionTest.kt b/src/test/kotlin/g3101_3200/s3146_permutation_difference_between_two_strings/SolutionTest.kt new file mode 100644 index 000000000..396cea335 --- /dev/null +++ b/src/test/kotlin/g3101_3200/s3146_permutation_difference_between_two_strings/SolutionTest.kt @@ -0,0 +1,17 @@ +package g3101_3200.s3146_permutation_difference_between_two_strings + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun findPermutationDifference() { + assertThat(Solution().findPermutationDifference("abc", "bac"), equalTo(2)) + } + + @Test + fun findPermutationDifference2() { + assertThat(Solution().findPermutationDifference("abcde", "edbac"), equalTo(12)) + } +} diff --git a/src/test/kotlin/g3101_3200/s3147_taking_maximum_energy_from_the_mystic_dungeon/SolutionTest.kt b/src/test/kotlin/g3101_3200/s3147_taking_maximum_energy_from_the_mystic_dungeon/SolutionTest.kt new file mode 100644 index 000000000..b85fa59e2 --- /dev/null +++ b/src/test/kotlin/g3101_3200/s3147_taking_maximum_energy_from_the_mystic_dungeon/SolutionTest.kt @@ -0,0 +1,17 @@ +package g3101_3200.s3147_taking_maximum_energy_from_the_mystic_dungeon + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maximumEnergy() { + assertThat(Solution().maximumEnergy(intArrayOf(5, 2, -10, -5, 1), 3), equalTo(3)) + } + + @Test + fun maximumEnergy2() { + assertThat(Solution().maximumEnergy(intArrayOf(-2, -3, -1), 2), equalTo(-1)) + } +} diff --git a/src/test/kotlin/g3101_3200/s3148_maximum_difference_score_in_a_grid/SolutionTest.kt b/src/test/kotlin/g3101_3200/s3148_maximum_difference_score_in_a_grid/SolutionTest.kt new file mode 100644 index 000000000..b4487194c --- /dev/null +++ b/src/test/kotlin/g3101_3200/s3148_maximum_difference_score_in_a_grid/SolutionTest.kt @@ -0,0 +1,34 @@ +package g3101_3200.s3148_maximum_difference_score_in_a_grid + +import com_github_leetcode.ArrayUtils.getLists +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maxScore() { + assertThat( + Solution() + .maxScore( + getLists( + arrayOf( + intArrayOf(9, 5, 7, 3), + intArrayOf(8, 9, 6, 1), + intArrayOf(6, 7, 14, 3), + intArrayOf(2, 5, 3, 1) + ) + ) + ), + equalTo(9) + ) + } + + @Test + fun maxScore2() { + assertThat( + Solution().maxScore(getLists(arrayOf(intArrayOf(4, 3, 2), intArrayOf(3, 2, 1)))), + equalTo(-1) + ) + } +} diff --git a/src/test/kotlin/g3101_3200/s3149_find_the_minimum_cost_array_permutation/SolutionTest.kt b/src/test/kotlin/g3101_3200/s3149_find_the_minimum_cost_array_permutation/SolutionTest.kt new file mode 100644 index 000000000..b398e02f1 --- /dev/null +++ b/src/test/kotlin/g3101_3200/s3149_find_the_minimum_cost_array_permutation/SolutionTest.kt @@ -0,0 +1,21 @@ +package g3101_3200.s3149_find_the_minimum_cost_array_permutation + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun findPermutation() { + assertThat( + Solution().findPermutation(intArrayOf(1, 0, 2)), equalTo(intArrayOf(0, 1, 2)) + ) + } + + @Test + fun findPermutation2() { + assertThat( + Solution().findPermutation(intArrayOf(0, 2, 1)), equalTo(intArrayOf(0, 2, 1)) + ) + } +}