diff --git a/src/main/kotlin/g3201_3300/s3270_find_the_key_of_the_numbers/Solution.kt b/src/main/kotlin/g3201_3300/s3270_find_the_key_of_the_numbers/Solution.kt new file mode 100644 index 000000000..7af6062d2 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3270_find_the_key_of_the_numbers/Solution.kt @@ -0,0 +1,43 @@ +package g3201_3300.s3270_find_the_key_of_the_numbers + +// #Easy #Math #2024_09_04_Time_122_ms_(97.83%)_Space_34_MB_(65.22%) + +import kotlin.math.min + +class Solution { + fun generateKey(num1: Int, num2: Int, num3: Int): Int { + val s1 = ( + min( + num1 / 1000 % 10, + min( + num2 / 1000 % 10, + num3 / 1000 % 10 + ) + ) * 1000 + ) + val s2 = ( + min( + num1 / 100 % 10, + min( + num2 / 100 % 10, + num3 / 100 % 10 + ) + ) * 100 + ) + val s3 = + ( + min( + num1 / 10 % 10, + min( + num2 / 10 % 10, + num3 / 10 % 10 + ) + ) * 10 + ) + val s4 = min( + num1 % 10, + min(num2 % 10, num3 % 10) + ) + return s1 + s2 + s3 + s4 + } +} diff --git a/src/main/kotlin/g3201_3300/s3270_find_the_key_of_the_numbers/readme.md b/src/main/kotlin/g3201_3300/s3270_find_the_key_of_the_numbers/readme.md new file mode 100644 index 000000000..3b3f6fed1 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3270_find_the_key_of_the_numbers/readme.md @@ -0,0 +1,45 @@ +3270\. Find the Key of the Numbers + +Easy + +You are given three **positive** integers `num1`, `num2`, and `num3`. + +The `key` of `num1`, `num2`, and `num3` is defined as a four-digit number such that: + +* Initially, if any number has **less than** four digits, it is padded with **leading zeros**. +* The ith digit (`1 <= i <= 4`) of the `key` is generated by taking the **smallest** digit among the ith digits of `num1`, `num2`, and `num3`. + +Return the `key` of the three numbers **without** leading zeros (_if any_). + +**Example 1:** + +**Input:** num1 = 1, num2 = 10, num3 = 1000 + +**Output:** 0 + +**Explanation:** + +On padding, `num1` becomes `"0001"`, `num2` becomes `"0010"`, and `num3` remains `"1000"`. + +* The 1st digit of the `key` is `min(0, 0, 1)`. +* The 2nd digit of the `key` is `min(0, 0, 0)`. +* The 3rd digit of the `key` is `min(0, 1, 0)`. +* The 4th digit of the `key` is `min(1, 0, 0)`. + +Hence, the `key` is `"0000"`, i.e. 0. + +**Example 2:** + +**Input:** num1 = 987, num2 = 879, num3 = 798 + +**Output:** 777 + +**Example 3:** + +**Input:** num1 = 1, num2 = 2, num3 = 3 + +**Output:** 1 + +**Constraints:** + +* `1 <= num1, num2, num3 <= 9999` \ No newline at end of file diff --git a/src/main/kotlin/g3201_3300/s3271_hash_divided_string/Solution.kt b/src/main/kotlin/g3201_3300/s3271_hash_divided_string/Solution.kt new file mode 100644 index 000000000..afd7a8305 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3271_hash_divided_string/Solution.kt @@ -0,0 +1,20 @@ +package g3201_3300.s3271_hash_divided_string + +// #Medium #String #Simulation #2024_09_04_Time_178_ms_(100.00%)_Space_36.9_MB_(97.50%) + +class Solution { + fun stringHash(s: String, k: Int): String { + val result = StringBuilder() + var i = 0 + var sum = 0 + while (i < s.length) { + sum += s[i].code - 'a'.code + if ((i + 1) % k == 0) { + result.append(('a'.code + sum % 26).toChar()) + sum = 0 + } + i++ + } + return result.toString() + } +} diff --git a/src/main/kotlin/g3201_3300/s3271_hash_divided_string/readme.md b/src/main/kotlin/g3201_3300/s3271_hash_divided_string/readme.md new file mode 100644 index 000000000..2eca01647 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3271_hash_divided_string/readme.md @@ -0,0 +1,46 @@ +3271\. Hash Divided String + +Medium + +You are given a string `s` of length `n` and an integer `k`, where `n` is a **multiple** of `k`. Your task is to hash the string `s` into a new string called `result`, which has a length of `n / k`. + +First, divide `s` into `n / k` **substrings**, each with a length of `k`. Then, initialize `result` as an **empty** string. + +For each **substring** in order from the beginning: + +* The **hash value** of a character is the index of that character in the **English alphabet** (e.g., `'a' → 0`, `'b' → 1`, ..., `'z' → 25`). +* Calculate the _sum_ of all the **hash values** of the characters in the substring. +* Find the remainder of this sum when divided by 26, which is called `hashedChar`. +* Identify the character in the English lowercase alphabet that corresponds to `hashedChar`. +* Append that character to the end of `result`. + +Return `result`. + +**Example 1:** + +**Input:** s = "abcd", k = 2 + +**Output:** "bf" + +**Explanation:** + +First substring: `"ab"`, `0 + 1 = 1`, `1 % 26 = 1`, `result[0] = 'b'`. + +Second substring: `"cd"`, `2 + 3 = 5`, `5 % 26 = 5`, `result[1] = 'f'`. + +**Example 2:** + +**Input:** s = "mxz", k = 3 + +**Output:** "i" + +**Explanation:** + +The only substring: `"mxz"`, `12 + 23 + 25 = 60`, `60 % 26 = 8`, `result[0] = 'i'`. + +**Constraints:** + +* `1 <= k <= 100` +* `k <= s.length <= 1000` +* `s.length` is divisible by `k`. +* `s` consists only of lowercase English letters. \ No newline at end of file diff --git a/src/main/kotlin/g3201_3300/s3272_find_the_count_of_good_integers/Solution.kt b/src/main/kotlin/g3201_3300/s3272_find_the_count_of_good_integers/Solution.kt new file mode 100644 index 000000000..6756fe251 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3272_find_the_count_of_good_integers/Solution.kt @@ -0,0 +1,103 @@ +package g3201_3300.s3272_find_the_count_of_good_integers + +// #Hard #Hash_Table #Math #Enumeration #Combinatorics +// #2024_09_04_Time_452_ms_(80.00%)_Space_53.5_MB_(60.00%) + +import kotlin.math.max + +class Solution { + private val palindromes: MutableList = ArrayList() + + private fun factorial(n: Int): Long { + var res: Long = 1 + for (i in 2..n) { + res *= i.toLong() + } + return res + } + + private fun countDigits(s: String): MutableMap { + val freq: MutableMap = HashMap() + for (c in s.toCharArray()) { + freq[c] = freq.getOrDefault(c, 0) + 1 + } + return freq + } + + private fun calculatePermutations(freq: Map, length: Int): Long { + var totalPermutations = factorial(length) + for (count in freq.values) { + totalPermutations /= factorial(count) + } + return totalPermutations + } + + private fun calculateValidPermutations(s: String): Long { + val freq = countDigits(s) + val n = s.length + var totalPermutations = calculatePermutations(freq, n) + if (freq.getOrDefault('0', 0) > 0) { + freq['0'] = freq['0']!! - 1 + val invalidPermutations = calculatePermutations(freq, n - 1) + totalPermutations -= invalidPermutations + } + return totalPermutations + } + + private fun generatePalindromes( + f: Int, + r: Int, + k: Int, + lb: Int, + sum: Int, + ans: StringBuilder, + rem: IntArray + ) { + if (f > r) { + if (sum == 0) { + palindromes.add(ans.toString()) + } + return + } + for (i in lb..9) { + ans.setCharAt(f, ('0'.code + i).toChar()) + ans.setCharAt(r, ('0'.code + i).toChar()) + var chk = sum + chk = (chk + rem[f] * i) % k + if (f != r) { + chk = (chk + rem[r] * i) % k + } + generatePalindromes(f + 1, r - 1, k, 0, chk, ans, rem) + } + } + + private fun allKPalindromes(n: Int, k: Int): List { + val ans = StringBuilder(n) + ans.append("0".repeat(max(0.0, n.toDouble()).toInt())) + val rem = IntArray(n) + rem[0] = 1 + for (i in 1 until n) { + rem[i] = (rem[i - 1] * 10) % k + } + palindromes.clear() + generatePalindromes(0, n - 1, k, 1, 0, ans, rem) + return palindromes + } + + fun countGoodIntegers(n: Int, k: Int): Long { + val ans = allKPalindromes(n, k) + val st: MutableSet = HashSet() + for (str in ans) { + val arr = str.toCharArray() + arr.sort() + st.add(String(arr)) + } + val v: List = ArrayList(st) + var chk: Long = 0 + for (str in v) { + val cc = calculateValidPermutations(str) + chk += cc + } + return chk + } +} diff --git a/src/main/kotlin/g3201_3300/s3272_find_the_count_of_good_integers/readme.md b/src/main/kotlin/g3201_3300/s3272_find_the_count_of_good_integers/readme.md new file mode 100644 index 000000000..6c28972f4 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3272_find_the_count_of_good_integers/readme.md @@ -0,0 +1,50 @@ +3272\. Find the Count of Good Integers + +Hard + +You are given two **positive** integers `n` and `k`. + +An integer `x` is called **k-palindromic** if: + +* `x` is a palindrome. +* `x` is divisible by `k`. + +An integer is called **good** if its digits can be _rearranged_ to form a **k-palindromic** integer. For example, for `k = 2`, 2020 can be rearranged to form the _k-palindromic_ integer 2002, whereas 1010 cannot be rearranged to form a _k-palindromic_ integer. + +Return the count of **good** integers containing `n` digits. + +**Note** that _any_ integer must **not** have leading zeros, **neither** before **nor** after rearrangement. For example, 1010 _cannot_ be rearranged to form 101. + +**Example 1:** + +**Input:** n = 3, k = 5 + +**Output:** 27 + +**Explanation:** + +_Some_ of the good integers are: + +* 551 because it can be rearranged to form 515. +* 525 because it is already k-palindromic. + +**Example 2:** + +**Input:** n = 1, k = 4 + +**Output:** 2 + +**Explanation:** + +The two good integers are 4 and 8. + +**Example 3:** + +**Input:** n = 5, k = 6 + +**Output:** 2468 + +**Constraints:** + +* `1 <= n <= 10` +* `1 <= k <= 9` \ No newline at end of file diff --git a/src/main/kotlin/g3201_3300/s3273_minimum_amount_of_damage_dealt_to_bob/Solution.kt b/src/main/kotlin/g3201_3300/s3273_minimum_amount_of_damage_dealt_to_bob/Solution.kt new file mode 100644 index 000000000..808784943 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3273_minimum_amount_of_damage_dealt_to_bob/Solution.kt @@ -0,0 +1,29 @@ +package g3201_3300.s3273_minimum_amount_of_damage_dealt_to_bob + +// #Hard #Array #Sorting #Greedy #2024_09_04_Time_793_ms_(90.00%)_Space_67.1_MB_(55.00%) + +class Solution { + fun minDamage(pw: Int, damage: IntArray, health: IntArray): Long { + var res: Long = 0 + var sum: Long = 0 + for (e in damage) { + sum += e.toLong() + } + val pairs = arrayOfNulls(damage.size) + for (e in damage.indices) { + pairs[e] = Pair(damage[e], (health[e] + pw - 1) / pw) + } + pairs.sort() + for (pr in pairs) { + res += pr!!.`val` * sum + sum -= pr.key.toLong() + } + return res + } + + internal class Pair(var key: Int, var `val`: Int) : Comparable { + override fun compareTo(p: Pair): Int { + return `val` * p.key - key * p.`val` + } + } +} diff --git a/src/main/kotlin/g3201_3300/s3273_minimum_amount_of_damage_dealt_to_bob/readme.md b/src/main/kotlin/g3201_3300/s3273_minimum_amount_of_damage_dealt_to_bob/readme.md new file mode 100644 index 000000000..289046f26 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3273_minimum_amount_of_damage_dealt_to_bob/readme.md @@ -0,0 +1,49 @@ +3273\. Minimum Amount of Damage Dealt to Bob + +Hard + +You are given an integer `power` and two integer arrays `damage` and `health`, both having length `n`. + +Bob has `n` enemies, where enemy `i` will deal Bob `damage[i]` **points** of damage per second while they are _alive_ (i.e. `health[i] > 0`). + +Every second, **after** the enemies deal damage to Bob, he chooses **one** of the enemies that is still _alive_ and deals `power` points of damage to them. + +Determine the **minimum** total amount of damage points that will be dealt to Bob before **all** `n` enemies are _dead_. + +**Example 1:** + +**Input:** power = 4, damage = [1,2,3,4], health = [4,5,6,8] + +**Output:** 39 + +**Explanation:** + +* Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is `10 + 10 = 20` points. +* Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is `6 + 6 = 12` points. +* Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is `3` points. +* Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is `2 + 2 = 4` points. + +**Example 2:** + +**Input:** power = 1, damage = [1,1,1,1], health = [1,2,3,4] + +**Output:** 20 + +**Explanation:** + +* Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is `4` points. +* Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is `3 + 3 = 6` points. +* Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is `2 + 2 + 2 = 6` points. +* Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is `1 + 1 + 1 + 1 = 4` points. + +**Example 3:** + +**Input:** power = 8, damage = [40], health = [59] + +**Output:** 320 + +**Constraints:** + +* 1 <= power <= 104 +* 1 <= n == damage.length == health.length <= 105 +* 1 <= damage[i], health[i] <= 104 \ No newline at end of file diff --git a/src/main/kotlin/g3201_3300/s3274_check_if_two_chessboard_squares_have_the_same_color/Solution.kt b/src/main/kotlin/g3201_3300/s3274_check_if_two_chessboard_squares_have_the_same_color/Solution.kt new file mode 100644 index 000000000..a3f014e21 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3274_check_if_two_chessboard_squares_have_the_same_color/Solution.kt @@ -0,0 +1,11 @@ +package g3201_3300.s3274_check_if_two_chessboard_squares_have_the_same_color + +// #Easy #String #Math #2024_09_04_Time_164_ms_(38.64%)_Space_34.3_MB_(81.82%) + +class Solution { + fun checkTwoChessboards(coordinate1: String, coordinate2: String): Boolean { + val s1 = (coordinate1[0].code - 'a'.code) + (coordinate1[1].code - '0'.code) + val s2 = (coordinate2[0].code - 'a'.code) + (coordinate2[1].code - '0'.code) + return s1 % 2 == s2 % 2 + } +} diff --git a/src/main/kotlin/g3201_3300/s3274_check_if_two_chessboard_squares_have_the_same_color/readme.md b/src/main/kotlin/g3201_3300/s3274_check_if_two_chessboard_squares_have_the_same_color/readme.md new file mode 100644 index 000000000..99ca3599f --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3274_check_if_two_chessboard_squares_have_the_same_color/readme.md @@ -0,0 +1,39 @@ +3274\. Check if Two Chessboard Squares Have the Same Color + +Easy + +You are given two strings, `coordinate1` and `coordinate2`, representing the coordinates of a square on an `8 x 8` chessboard. + +Below is the chessboard for reference. + +![](https://assets.leetcode.com/uploads/2024/07/17/screenshot-2021-02-20-at-22159-pm.png) + +Return `true` if these two squares have the same color and `false` otherwise. + +The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first (indicating its column), and the number second (indicating its row). + +**Example 1:** + +**Input:** coordinate1 = "a1", coordinate2 = "c3" + +**Output:** true + +**Explanation:** + +Both squares are black. + +**Example 2:** + +**Input:** coordinate1 = "a1", coordinate2 = "h3" + +**Output:** false + +**Explanation:** + +Square `"a1"` is black and `"h3"` is white. + +**Constraints:** + +* `coordinate1.length == coordinate2.length == 2` +* `'a' <= coordinate1[0], coordinate2[0] <= 'h'` +* `'1' <= coordinate1[1], coordinate2[1] <= '8'` \ No newline at end of file diff --git a/src/main/kotlin/g3201_3300/s3275_k_th_nearest_obstacle_queries/Solution.kt b/src/main/kotlin/g3201_3300/s3275_k_th_nearest_obstacle_queries/Solution.kt new file mode 100644 index 000000000..b0bde43b9 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3275_k_th_nearest_obstacle_queries/Solution.kt @@ -0,0 +1,57 @@ +package g3201_3300.s3275_k_th_nearest_obstacle_queries + +// #Medium #Array #Heap_Priority_Queue #2024_09_04_Time_1277_ms_(100.00%)_Space_147.2_MB_(61.11%) + +import kotlin.math.abs + +class Solution { + fun resultsArray(queries: Array, k: Int): IntArray { + val len = queries.size + val results = IntArray(len) + val heap = IntArray(k) + run { + var i = 0 + while (i < k && i < len) { + val query = queries[i] + heap[i] = (abs(query[0]) + abs(query[1])) + results[i] = -1 + i++ + } + } + if (k <= len) { + buildMaxHeap(heap, k) + results[k - 1] = heap[0] + } + for (i in k until len) { + val query = queries[i] + val dist = (abs(query[0]) + abs(query[1])) + if (dist < heap[0]) { + heap[0] = dist + heapify(heap, 0, k) + } + results[i] = heap[0] + } + return results + } + + private fun buildMaxHeap(heap: IntArray, size: Int) { + for (i in size / 2 - 1 downTo 0) { + heapify(heap, i, size) + } + } + + private fun heapify(heap: IntArray, index: Int, size: Int) { + val root = heap[index] + val left = 2 * index + 1 + val right = 2 * index + 2 + if (right < size && root < heap[right] && heap[left] < heap[right]) { + heap[index] = heap[right] + heap[right] = root + heapify(heap, right, size) + } else if (left < size && root < heap[left]) { + heap[index] = heap[left] + heap[left] = root + heapify(heap, left, size) + } + } +} diff --git a/src/main/kotlin/g3201_3300/s3275_k_th_nearest_obstacle_queries/readme.md b/src/main/kotlin/g3201_3300/s3275_k_th_nearest_obstacle_queries/readme.md new file mode 100644 index 000000000..7842a92a9 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3275_k_th_nearest_obstacle_queries/readme.md @@ -0,0 +1,50 @@ +3275\. K-th Nearest Obstacle Queries + +Medium + +There is an infinite 2D plane. + +You are given a positive integer `k`. You are also given a 2D array `queries`, which contains the following queries: + +* `queries[i] = [x, y]`: Build an obstacle at coordinate `(x, y)` in the plane. It is guaranteed that there is **no** obstacle at this coordinate when this query is made. + +After each query, you need to find the **distance** of the kth **nearest** obstacle from the origin. + +Return an integer array `results` where `results[i]` denotes the kth nearest obstacle after query `i`, or `results[i] == -1` if there are less than `k` obstacles. + +**Note** that initially there are **no** obstacles anywhere. + +The **distance** of an obstacle at coordinate `(x, y)` from the origin is given by `|x| + |y|`. + +**Example 1:** + +**Input:** queries = [[1,2],[3,4],[2,3],[-3,0]], k = 2 + +**Output:** [-1,7,5,3] + +**Explanation:** + +* Initially, there are 0 obstacles. +* After `queries[0]`, there are less than 2 obstacles. +* After `queries[1]`, there are obstacles at distances 3 and 7. +* After `queries[2]`, there are obstacles at distances 3, 5, and 7. +* After `queries[3]`, there are obstacles at distances 3, 3, 5, and 7. + +**Example 2:** + +**Input:** queries = [[5,5],[4,4],[3,3]], k = 1 + +**Output:** [10,8,6] + +**Explanation:** + +* After `queries[0]`, there is an obstacle at distance 10. +* After `queries[1]`, there are obstacles at distances 8 and 10. +* After `queries[2]`, there are obstacles at distances 6, 8, and 10. + +**Constraints:** + +* 1 <= queries.length <= 2 * 105 +* All `queries[i]` are unique. +* -109 <= queries[i][0], queries[i][1] <= 109 +* 1 <= k <= 105 \ No newline at end of file diff --git a/src/main/kotlin/g3201_3300/s3276_select_cells_in_grid_with_maximum_score/Solution.kt b/src/main/kotlin/g3201_3300/s3276_select_cells_in_grid_with_maximum_score/Solution.kt new file mode 100644 index 000000000..af2689686 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3276_select_cells_in_grid_with_maximum_score/Solution.kt @@ -0,0 +1,48 @@ +package g3201_3300.s3276_select_cells_in_grid_with_maximum_score + +// #Hard #Array #Dynamic_Programming #Matrix #Bit_Manipulation #Bitmask +// #2024_09_04_Time_213_ms_(92.31%)_Space_39.8_MB_(84.62%) + +import kotlin.math.max + +class Solution { + fun maxScore(grid: List>): Int { + val n = grid.size + val m = grid[0].size + val arr = Array(n * m) { IntArray(2) } + for (i in 0 until n) { + val l = grid[i] + for (j in l.indices) { + arr[i * m + j][0] = l[j] + arr[i * m + j][1] = i + } + } + arr.sortWith { a: IntArray, b: IntArray -> b[0] - a[0] } + var dp = IntArray(1 shl n) + var i = 0 + while (i < arr.size) { + val seen = BooleanArray(n) + seen[arr[i][1]] = true + val v = arr[i][0] + i++ + while (i < arr.size && arr[i][0] == v) { + seen[arr[i][1]] = true + i++ + } + val next = dp.copyOf(dp.size) + for (j in 0 until n) { + if (seen[j]) { + val and = ((1 shl n) - 1) xor (1 shl j) + var k = and + while (k > 0) { + next[k or (1 shl j)] = max(next[k or (1 shl j)], (dp[k] + v)) + k = (k - 1) and and + } + next[1 shl j] = max(next[1 shl j], v) + } + } + dp = next + } + return dp[dp.size - 1] + } +} diff --git a/src/main/kotlin/g3201_3300/s3276_select_cells_in_grid_with_maximum_score/readme.md b/src/main/kotlin/g3201_3300/s3276_select_cells_in_grid_with_maximum_score/readme.md new file mode 100644 index 000000000..3310e3858 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3276_select_cells_in_grid_with_maximum_score/readme.md @@ -0,0 +1,43 @@ +3276\. Select Cells in Grid With Maximum Score + +Hard + +You are given a 2D matrix `grid` consisting of positive integers. + +You have to select _one or more_ cells from the matrix such that the following conditions are satisfied: + +* No two selected cells are in the **same** row of the matrix. +* The values in the set of selected cells are **unique**. + +Your score will be the **sum** of the values of the selected cells. + +Return the **maximum** score you can achieve. + +**Example 1:** + +**Input:** grid = [[1,2,3],[4,3,2],[1,1,1]] + +**Output:** 8 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/07/29/grid1drawio.png) + +We can select the cells with values 1, 3, and 4 that are colored above. + +**Example 2:** + +**Input:** grid = [[8,7,6],[8,3,2]] + +**Output:** 15 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/07/29/grid8_8drawio.png) + +We can select the cells with values 7 and 8 that are colored above. + +**Constraints:** + +* `1 <= grid.length, grid[i].length <= 10` +* `1 <= grid[i][j] <= 100` \ No newline at end of file diff --git a/src/main/kotlin/g3201_3300/s3277_maximum_xor_score_subarray_queries/Solution.kt b/src/main/kotlin/g3201_3300/s3277_maximum_xor_score_subarray_queries/Solution.kt new file mode 100644 index 000000000..a956da61d --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3277_maximum_xor_score_subarray_queries/Solution.kt @@ -0,0 +1,32 @@ +package g3201_3300.s3277_maximum_xor_score_subarray_queries + +// #Hard #Array #Dynamic_Programming #2024_09_04_Time_1269_ms_(100.00%)_Space_116.7_MB_(75.00%) + +import kotlin.math.max + +class Solution { + fun maximumSubarrayXor(nums: IntArray, queries: Array): IntArray { + val n = nums.size + val dp = Array(n) { IntArray(n) } + for (i in 0 until n) { + dp[i][i] = nums[i] + } + for (i in n - 2 downTo 0) { + for (j in i + 1 until n) { + dp[i][j] = dp[i][j - 1] xor dp[i + 1][j] + } + } + for (i in n - 2 downTo 0) { + for (j in i + 1 until n) { + dp[i][j] = max(dp[i][j], max(dp[i][j - 1], dp[i + 1][j])) + } + } + val q = queries.size + val ans = IntArray(q) + var time = 0 + for (query in queries) { + ans[time++] = dp[query[0]][query[1]] + } + return ans + } +} diff --git a/src/main/kotlin/g3201_3300/s3277_maximum_xor_score_subarray_queries/readme.md b/src/main/kotlin/g3201_3300/s3277_maximum_xor_score_subarray_queries/readme.md new file mode 100644 index 000000000..3da27c01e --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3277_maximum_xor_score_subarray_queries/readme.md @@ -0,0 +1,53 @@ +3277\. Maximum XOR Score Subarray Queries + +Hard + +You are given an array `nums` of `n` integers, and a 2D integer array `queries` of size `q`, where queries[i] = [li, ri]. + +For each query, you must find the **maximum XOR score** of any subarray of nums[li..ri]. + +The **XOR score** of an array `a` is found by repeatedly applying the following operations on `a` so that only one element remains, that is the **score**: + +* Simultaneously replace `a[i]` with `a[i] XOR a[i + 1]` for all indices `i` except the last one. +* Remove the last element of `a`. + +Return an array `answer` of size `q` where `answer[i]` is the answer to query `i`. + +**Example 1:** + +**Input:** nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]] + +**Output:** [12,60,60] + +**Explanation:** + +In the first query, `nums[0..2]` has 6 subarrays `[2]`, `[8]`, `[4]`, `[2, 8]`, `[8, 4]`, and `[2, 8, 4]` each with a respective XOR score of 2, 8, 4, 10, 12, and 6. The answer for the query is 12, the largest of all XOR scores. + +In the second query, the subarray of `nums[1..4]` with the largest XOR score is `nums[1..4]` with a score of 60. + +In the third query, the subarray of `nums[0..5]` with the largest XOR score is `nums[1..4]` with a score of 60. + +**Example 2:** + +**Input:** nums = [0,7,3,2,8,5,1], queries = [[0,3],[1,5],[2,4],[2,6],[5,6]] + +**Output:** [7,14,11,14,5] + +**Explanation:** + +| Index | nums[li..ri] | Maximum XOR Score Subarray | Maximum Subarray XOR Score | +|-------|-----------------------------------|----------------------------|-----------------------------| +| 0 | [0, 7, 3, 2] | [7] | 7 | +| 1 | [7, 3, 2, 8, 5] | [7, 3, 2, 8] | 14 | +| 2 | [3, 2, 8] | [3, 2, 8] | 11 | +| 3 | [3, 2, 8, 5, 1] | [2, 8, 5, 1] | 14 | +| 4 | [5, 1] | [5] | 5 | + +**Constraints:** + +* `1 <= n == nums.length <= 2000` +* 0 <= nums[i] <= 231 - 1 +* 1 <= q == queries.length <= 105 +* `queries[i].length == 2` +* queries[i] = [li, ri] +* 0 <= li <= ri <= n - 1 \ No newline at end of file diff --git a/src/test/kotlin/g3201_3300/s3270_find_the_key_of_the_numbers/SolutionTest.kt b/src/test/kotlin/g3201_3300/s3270_find_the_key_of_the_numbers/SolutionTest.kt new file mode 100644 index 000000000..f30dca038 --- /dev/null +++ b/src/test/kotlin/g3201_3300/s3270_find_the_key_of_the_numbers/SolutionTest.kt @@ -0,0 +1,22 @@ +package g3201_3300.s3270_find_the_key_of_the_numbers + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun generateKey() { + assertThat(Solution().generateKey(1, 10, 1000), equalTo(0)) + } + + @Test + fun generateKey2() { + assertThat(Solution().generateKey(987, 879, 798), equalTo(777)) + } + + @Test + fun generateKey3() { + assertThat(Solution().generateKey(1, 2, 3), equalTo(1)) + } +} diff --git a/src/test/kotlin/g3201_3300/s3271_hash_divided_string/SolutionTest.kt b/src/test/kotlin/g3201_3300/s3271_hash_divided_string/SolutionTest.kt new file mode 100644 index 000000000..983d0d03f --- /dev/null +++ b/src/test/kotlin/g3201_3300/s3271_hash_divided_string/SolutionTest.kt @@ -0,0 +1,17 @@ +package g3201_3300.s3271_hash_divided_string + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun stringHash() { + assertThat(Solution().stringHash("abcd", 2), equalTo("bf")) + } + + @Test + fun stringHash2() { + assertThat(Solution().stringHash("mxz", 3), equalTo("i")) + } +} diff --git a/src/test/kotlin/g3201_3300/s3272_find_the_count_of_good_integers/SolutionTest.kt b/src/test/kotlin/g3201_3300/s3272_find_the_count_of_good_integers/SolutionTest.kt new file mode 100644 index 000000000..8fee722cd --- /dev/null +++ b/src/test/kotlin/g3201_3300/s3272_find_the_count_of_good_integers/SolutionTest.kt @@ -0,0 +1,22 @@ +package g3201_3300.s3272_find_the_count_of_good_integers + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun countGoodIntegers() { + assertThat(Solution().countGoodIntegers(3, 5), equalTo(27L)) + } + + @Test + fun countGoodIntegers2() { + assertThat(Solution().countGoodIntegers(1, 4), equalTo(2L)) + } + + @Test + fun countGoodIntegers3() { + assertThat(Solution().countGoodIntegers(5, 6), equalTo(2468L)) + } +} diff --git a/src/test/kotlin/g3201_3300/s3273_minimum_amount_of_damage_dealt_to_bob/SolutionTest.kt b/src/test/kotlin/g3201_3300/s3273_minimum_amount_of_damage_dealt_to_bob/SolutionTest.kt new file mode 100644 index 000000000..568964cc8 --- /dev/null +++ b/src/test/kotlin/g3201_3300/s3273_minimum_amount_of_damage_dealt_to_bob/SolutionTest.kt @@ -0,0 +1,28 @@ +package g3201_3300.s3273_minimum_amount_of_damage_dealt_to_bob + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minDamage() { + assertThat( + Solution().minDamage(4, intArrayOf(1, 2, 3, 4), intArrayOf(4, 5, 6, 8)), + equalTo(39L) + ) + } + + @Test + fun minDamage2() { + assertThat( + Solution().minDamage(1, intArrayOf(1, 1, 1, 1), intArrayOf(1, 2, 3, 4)), + equalTo(20L) + ) + } + + @Test + fun minDamage3() { + assertThat(Solution().minDamage(8, intArrayOf(40), intArrayOf(59)), equalTo(320L)) + } +} diff --git a/src/test/kotlin/g3201_3300/s3274_check_if_two_chessboard_squares_have_the_same_color/SolutionTest.kt b/src/test/kotlin/g3201_3300/s3274_check_if_two_chessboard_squares_have_the_same_color/SolutionTest.kt new file mode 100644 index 000000000..f049351e2 --- /dev/null +++ b/src/test/kotlin/g3201_3300/s3274_check_if_two_chessboard_squares_have_the_same_color/SolutionTest.kt @@ -0,0 +1,17 @@ +package g3201_3300.s3274_check_if_two_chessboard_squares_have_the_same_color + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun checkTwoChessboards() { + assertThat(Solution().checkTwoChessboards("a1", "c3"), equalTo(true)) + } + + @Test + fun checkTwoChessboards2() { + assertThat(Solution().checkTwoChessboards("a1", "h3"), equalTo(false)) + } +} diff --git a/src/test/kotlin/g3201_3300/s3275_k_th_nearest_obstacle_queries/SolutionTest.kt b/src/test/kotlin/g3201_3300/s3275_k_th_nearest_obstacle_queries/SolutionTest.kt new file mode 100644 index 000000000..b1d482ebb --- /dev/null +++ b/src/test/kotlin/g3201_3300/s3275_k_th_nearest_obstacle_queries/SolutionTest.kt @@ -0,0 +1,26 @@ +package g3201_3300.s3275_k_th_nearest_obstacle_queries + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun resultsArray() { + assertThat( + Solution().resultsArray( + arrayOf(intArrayOf(1, 2), intArrayOf(3, 4), intArrayOf(2, 3), intArrayOf(-3, 0)), + 2 + ), + equalTo(intArrayOf(-1, 7, 5, 3)) + ) + } + + @Test + fun resultsArray2() { + assertThat( + Solution().resultsArray(arrayOf(intArrayOf(5, 5), intArrayOf(4, 4), intArrayOf(3, 3)), 1), + equalTo(intArrayOf(10, 8, 6)) + ) + } +} diff --git a/src/test/kotlin/g3201_3300/s3276_select_cells_in_grid_with_maximum_score/SolutionTest.kt b/src/test/kotlin/g3201_3300/s3276_select_cells_in_grid_with_maximum_score/SolutionTest.kt new file mode 100644 index 000000000..cec38b8e4 --- /dev/null +++ b/src/test/kotlin/g3201_3300/s3276_select_cells_in_grid_with_maximum_score/SolutionTest.kt @@ -0,0 +1,27 @@ +package g3201_3300.s3276_select_cells_in_grid_with_maximum_score + +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(1, 2, 3), intArrayOf(4, 3, 2), intArrayOf(1, 1, 1))) + ), + equalTo(8) + ) + } + + @Test + fun maxScore2() { + assertThat( + Solution().maxScore(getLists(arrayOf(intArrayOf(8, 7, 6), intArrayOf(8, 3, 2)))), + equalTo(15) + ) + } +} diff --git a/src/test/kotlin/g3201_3300/s3277_maximum_xor_score_subarray_queries/SolutionTest.kt b/src/test/kotlin/g3201_3300/s3277_maximum_xor_score_subarray_queries/SolutionTest.kt new file mode 100644 index 000000000..f6b59c4fc --- /dev/null +++ b/src/test/kotlin/g3201_3300/s3277_maximum_xor_score_subarray_queries/SolutionTest.kt @@ -0,0 +1,31 @@ +package g3201_3300.s3277_maximum_xor_score_subarray_queries + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maximumSubarrayXor() { + assertThat( + Solution() + .maximumSubarrayXor( + intArrayOf(2, 8, 4, 32, 16, 1), + arrayOf(intArrayOf(0, 2), intArrayOf(1, 4), intArrayOf(0, 5)) + ), + equalTo(intArrayOf(12, 60, 60)) + ) + } + + @Test + fun maximumSubarrayXor2() { + assertThat( + Solution() + .maximumSubarrayXor( + intArrayOf(0, 7, 3, 2, 8, 5, 1), + arrayOf(intArrayOf(0, 3), intArrayOf(1, 5), intArrayOf(2, 4), intArrayOf(2, 6), intArrayOf(5, 6)) + ), + equalTo(intArrayOf(7, 14, 11, 14, 5)) + ) + } +}