diff --git a/src/main/kotlin/g3401_3500/s3436_find_valid_emails/script.sql b/src/main/kotlin/g3401_3500/s3436_find_valid_emails/script.sql index 200e7bd50..26f7d4f8d 100644 --- a/src/main/kotlin/g3401_3500/s3436_find_valid_emails/script.sql +++ b/src/main/kotlin/g3401_3500/s3436_find_valid_emails/script.sql @@ -1,5 +1,5 @@ # Write your MySQL query statement below -# #Easy #2025_02_04_Time_451_ms_(70.84%)_Space_0.0_MB_(100.00%) +# #Easy #Database #2025_02_04_Time_451_ms_(70.84%)_Space_0.0_MB_(100.00%) select user_id, email from users where email regexp '^[A-Za-z0-9_]+@[A-Za-z][A-Za-z0-9_]*\.com$' order by user_id diff --git a/src/main/kotlin/g3401_3500/s3461_check_if_digits_are_equal_in_string_after_operations_i/Solution.kt b/src/main/kotlin/g3401_3500/s3461_check_if_digits_are_equal_in_string_after_operations_i/Solution.kt new file mode 100644 index 000000000..271369539 --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3461_check_if_digits_are_equal_in_string_after_operations_i/Solution.kt @@ -0,0 +1,22 @@ +package g3401_3500.s3461_check_if_digits_are_equal_in_string_after_operations_i + +// #Easy #String #Math #Simulation #Number_Theory #Combinatorics +// #2025_02_25_Time_3_ms_(100.00%)_Space_35.54_MB_(100.00%) + +class Solution { + fun hasSameDigits(s: String): Boolean { + val ch = s.toCharArray() + var k = ch.size - 1 + while (k != 1) { + for (i in 0.., limits: IntArray, k: Int): Long { + var l = 0 + for (i in limits.indices) { + l += limits[i] + } + val dp = IntArray(l) + var a = 0 + for (i in grid.indices) { + val lim = limits[i] + grid[i].sort() + for (j in grid[i].size - lim..ith row of `grid` does not exceed `limits[i]`. + + +Return the **maximum sum**. + +**Example 1:** + +**Input:** grid = [[1,2],[3,4]], limits = [1,2], k = 2 + +**Output:** 7 + +**Explanation:** + +* From the second row, we can take at most 2 elements. The elements taken are 4 and 3. +* The maximum possible sum of at most 2 selected elements is `4 + 3 = 7`. + +**Example 2:** + +**Input:** grid = [[5,3,7],[8,2,6]], limits = [2,2], k = 3 + +**Output:** 21 + +**Explanation:** + +* From the first row, we can take at most 2 elements. The element taken is 7. +* From the second row, we can take at most 2 elements. The elements taken are 8 and 6. +* The maximum possible sum of at most 3 selected elements is `7 + 8 + 6 = 21`. + +**Constraints:** + +* `n == grid.length == limits.length` +* `m == grid[i].length` +* `1 <= n, m <= 500` +* 0 <= grid[i][j] <= 105 +* `0 <= limits[i] <= m` +* `0 <= k <= min(n * m, sum(limits))` \ No newline at end of file diff --git a/src/main/kotlin/g3401_3500/s3463_check_if_digits_are_equal_in_string_after_operations_ii/Solution.kt b/src/main/kotlin/g3401_3500/s3463_check_if_digits_are_equal_in_string_after_operations_ii/Solution.kt new file mode 100644 index 000000000..a6dfde70a --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3463_check_if_digits_are_equal_in_string_after_operations_ii/Solution.kt @@ -0,0 +1,76 @@ +package g3401_3500.s3463_check_if_digits_are_equal_in_string_after_operations_ii + +// #Hard #String #Math #Number_Theory #Combinatorics +// #2025_02_25_Time_38_ms_(100.00%)_Space_45.90_MB_(11.11%) + +class Solution { + private fun powMod10(a: Int, n: Int): Int { + var a = a + var n = n + var x = 1 + while (n >= 1) { + if (n % 2 == 1) { + x = (x * a) % 10 + } + a = (a * a) % 10 + n /= 2 + } + return x + } + + private fun f(n: Int): IntArray { + val ns = IntArray(n + 1) + val n2 = IntArray(n + 1) + val n5 = IntArray(n + 1) + ns[0] = 1 + for (i in 1..n) { + var m = i + n2[i] = n2[i - 1] + n5[i] = n5[i - 1] + while (m % 2 == 0) { + m /= 2 + n2[i]++ + } + while (m % 5 == 0) { + m /= 5 + n5[i]++ + } + ns[i] = (ns[i - 1] * m) % 10 + } + val inv = IntArray(10) + for (i in 1..9) { + for (j in 0..9) { + if (i * j % 10 == 1) { + inv[i] = j + } + } + } + val xs = IntArray(n + 1) + for (k in 0..n) { + var a = 0 + val s2 = n2[n] - n2[n - k] - n2[k] + val s5 = n5[n] - n5[n - k] - n5[k] + if (s2 == 0 || s5 == 0) { + a = (ns[n] * inv[ns[n - k]] * inv[ns[k]] * powMod10(2, s2) * powMod10(5, s5)) % 10 + } + xs[k] = a + } + return xs + } + + fun hasSameDigits(s: String): Boolean { + val n = s.length + val xs = f(n - 2) + val arr = IntArray(n) + for (i in 0..3 <= s.length <= 105 +* `s` consists of only digits. \ No newline at end of file diff --git a/src/main/kotlin/g3401_3500/s3464_maximize_the_distance_between_points_on_a_square/Solution.kt b/src/main/kotlin/g3401_3500/s3464_maximize_the_distance_between_points_on_a_square/Solution.kt new file mode 100644 index 000000000..8eca2b34b --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3464_maximize_the_distance_between_points_on_a_square/Solution.kt @@ -0,0 +1,77 @@ +package g3401_3500.s3464_maximize_the_distance_between_points_on_a_square + +// #Hard #Array #Greedy #Binary_Search #2025_02_25_Time_18_ms_(98.51%)_Space_49.78_MB_(46.27%) + +class Solution { + fun maxDistance(side: Int, points: Array, k: Int): Int { + val n = points.size + val p = LongArray(n) + for (i in 0..= i + n) { + break + } + cur = nx + cnt++ + } + if (cnt == k && (dArr[i] + c - dArr[cur]) >= d) { + return true + } + } + return false + } +} diff --git a/src/main/kotlin/g3401_3500/s3464_maximize_the_distance_between_points_on_a_square/readme.md b/src/main/kotlin/g3401_3500/s3464_maximize_the_distance_between_points_on_a_square/readme.md new file mode 100644 index 000000000..2fc14a006 --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3464_maximize_the_distance_between_points_on_a_square/readme.md @@ -0,0 +1,59 @@ +3464\. Maximize the Distance Between Points on a Square + +Hard + +You are given an integer `side`, representing the edge length of a square with corners at `(0, 0)`, `(0, side)`, `(side, 0)`, and `(side, side)` on a Cartesian plane. + +You are also given a **positive** integer `k` and a 2D integer array `points`, where points[i] = [xi, yi] represents the coordinate of a point lying on the **boundary** of the square. + +You need to select `k` elements among `points` such that the **minimum** Manhattan distance between any two points is **maximized**. + +Return the **maximum** possible **minimum** Manhattan distance between the selected `k` points. + +The Manhattan Distance between two cells (xi, yi) and (xj, yj) is |xi - xj| + |yi - yj|. + +**Example 1:** + +**Input:** side = 2, points = [[0,2],[2,0],[2,2],[0,0]], k = 4 + +**Output:** 2 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2025/01/28/4080_example0_revised.png) + +Select all four points. + +**Example 2:** + +**Input:** side = 2, points = [[0,0],[1,2],[2,0],[2,2],[2,1]], k = 4 + +**Output:** 1 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2025/01/28/4080_example1_revised.png) + +Select the points `(0, 0)`, `(2, 0)`, `(2, 2)`, and `(2, 1)`. + +**Example 3:** + +**Input:** side = 2, points = [[0,0],[0,1],[0,2],[1,2],[2,0],[2,2],[2,1]], k = 5 + +**Output:** 1 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2025/01/28/4080_example2_revised.png) + +Select the points `(0, 0)`, `(0, 1)`, `(0, 2)`, `(1, 2)`, and `(2, 2)`. + +**Constraints:** + +* 1 <= side <= 109 +* 4 <= points.length <= min(4 * side, 15 * 103) +* `points[i] == [xi, yi]` +* The input is generated such that: + * `points[i]` lies on the boundary of the square. + * All `points[i]` are **unique**. +* `4 <= k <= min(25, points.length)` \ No newline at end of file diff --git a/src/test/kotlin/g3401_3500/s3461_check_if_digits_are_equal_in_string_after_operations_i/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3461_check_if_digits_are_equal_in_string_after_operations_i/SolutionTest.kt new file mode 100644 index 000000000..f5472b652 --- /dev/null +++ b/src/test/kotlin/g3401_3500/s3461_check_if_digits_are_equal_in_string_after_operations_i/SolutionTest.kt @@ -0,0 +1,17 @@ +package g3401_3500.s3461_check_if_digits_are_equal_in_string_after_operations_i + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun hasSameDigits() { + assertThat(Solution().hasSameDigits("3902"), equalTo(true)) + } + + @Test + fun hasSameDigits2() { + assertThat(Solution().hasSameDigits("34789"), equalTo(false)) + } +} diff --git a/src/test/kotlin/g3401_3500/s3462_maximum_sum_with_at_most_k_elements/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3462_maximum_sum_with_at_most_k_elements/SolutionTest.kt new file mode 100644 index 000000000..9f2ede2b3 --- /dev/null +++ b/src/test/kotlin/g3401_3500/s3462_maximum_sum_with_at_most_k_elements/SolutionTest.kt @@ -0,0 +1,31 @@ +package g3401_3500.s3462_maximum_sum_with_at_most_k_elements + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maxSum() { + assertThat( + Solution().maxSum(arrayOf(intArrayOf(1, 2), intArrayOf(3, 4)), intArrayOf(1, 2), 2), + equalTo(7L), + ) + } + + @Test + fun maxSum2() { + assertThat( + Solution().maxSum(arrayOf(intArrayOf(5, 3, 7), intArrayOf(8, 2, 6)), intArrayOf(2, 2), 3), + equalTo(21L), + ) + } + + @Test + fun maxSum3() { + assertThat( + Solution().maxSum(arrayOf(), intArrayOf(2, 2), 3), + equalTo(0L), + ) + } +} diff --git a/src/test/kotlin/g3401_3500/s3463_check_if_digits_are_equal_in_string_after_operations_ii/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3463_check_if_digits_are_equal_in_string_after_operations_ii/SolutionTest.kt new file mode 100644 index 000000000..885d9373d --- /dev/null +++ b/src/test/kotlin/g3401_3500/s3463_check_if_digits_are_equal_in_string_after_operations_ii/SolutionTest.kt @@ -0,0 +1,22 @@ +package g3401_3500.s3463_check_if_digits_are_equal_in_string_after_operations_ii + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun hasSameDigits() { + assertThat(Solution().hasSameDigits("3902"), equalTo(true)) + } + + @Test + fun hasSameDigits2() { + assertThat(Solution().hasSameDigits("34789"), equalTo(false)) + } + + @Test + fun hasSameDigits3() { + assertThat(Solution().hasSameDigits("3506677"), equalTo(false)) + } +} diff --git a/src/test/kotlin/g3401_3500/s3464_maximize_the_distance_between_points_on_a_square/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3464_maximize_the_distance_between_points_on_a_square/SolutionTest.kt new file mode 100644 index 000000000..a1ab74e13 --- /dev/null +++ b/src/test/kotlin/g3401_3500/s3464_maximize_the_distance_between_points_on_a_square/SolutionTest.kt @@ -0,0 +1,59 @@ +package g3401_3500.s3464_maximize_the_distance_between_points_on_a_square + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maxDistance() { + assertThat( + Solution().maxDistance( + 2, + arrayOf(intArrayOf(0, 2), intArrayOf(2, 0), intArrayOf(2, 2), intArrayOf(0, 0)), + 4, + ), + equalTo(2), + ) + } + + @Test + fun maxDistance2() { + assertThat( + Solution() + .maxDistance( + 2, + arrayOf( + intArrayOf(0, 0), + intArrayOf(1, 2), + intArrayOf(2, 0), + intArrayOf(2, 2), + intArrayOf(2, 1), + ), + 4, + ), + equalTo(1), + ) + } + + @Test + fun maxDistance3() { + assertThat( + Solution() + .maxDistance( + 2, + arrayOf( + intArrayOf(0, 0), + intArrayOf(0, 1), + intArrayOf(0, 2), + intArrayOf(1, 2), + intArrayOf(2, 0), + intArrayOf(2, 2), + intArrayOf(2, 1), + ), + 5, + ), + equalTo(1), + ) + } +}