Skip to content

Added tasks 3461-3464 #762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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..<k) {
val a = ch[i].code - 48
val b = ch[i + 1].code - 48
val d = (a + b) % 10
val c = (d + '0'.code).toChar()
ch[i] = c
}
k--
}
return ch[0] == ch[1]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
3461\. Check If Digits Are Equal in String After Operations I

Easy

You are given a string `s` consisting of digits. Perform the following operation repeatedly until the string has **exactly** two digits:

* For each pair of consecutive digits in `s`, starting from the first digit, calculate a new digit as the sum of the two digits **modulo** 10.
* Replace `s` with the sequence of newly calculated digits, _maintaining the order_ in which they are computed.

Return `true` if the final two digits in `s` are the **same**; otherwise, return `false`.

**Example 1:**

**Input:** s = "3902"

**Output:** true

**Explanation:**

* Initially, `s = "3902"`
* First operation:
* `(s[0] + s[1]) % 10 = (3 + 9) % 10 = 2`
* `(s[1] + s[2]) % 10 = (9 + 0) % 10 = 9`
* `(s[2] + s[3]) % 10 = (0 + 2) % 10 = 2`
* `s` becomes `"292"`
* Second operation:
* `(s[0] + s[1]) % 10 = (2 + 9) % 10 = 1`
* `(s[1] + s[2]) % 10 = (9 + 2) % 10 = 1`
* `s` becomes `"11"`
* Since the digits in `"11"` are the same, the output is `true`.

**Example 2:**

**Input:** s = "34789"

**Output:** false

**Explanation:**

* Initially, `s = "34789"`.
* After the first operation, `s = "7157"`.
* After the second operation, `s = "862"`.
* After the third operation, `s = "48"`.
* Since `'4' != '8'`, the output is `false`.

**Constraints:**

* `3 <= s.length <= 100`
* `s` consists of only digits.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package g3401_3500.s3462_maximum_sum_with_at_most_k_elements

// #Medium #Array #Sorting #Greedy #Matrix #Heap_(Priority_Queue)
// #2025_02_25_Time_139_ms_(100.00%)_Space_88.84_MB_(79.31%)

class Solution {
fun maxSum(grid: Array<IntArray>, 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..<grid[i].size) {
dp[a] = grid[i][j]
a++
}
}
dp.sort()
var sum = 0L
for (i in l - 1 downTo l - k) {
sum += dp[i].toLong()
}
return sum
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
3462\. Maximum Sum With at Most K Elements

Medium

You are given a 2D integer matrix `grid` of size `n x m`, an integer array `limits` of length `n`, and an integer `k`. The task is to find the **maximum sum** of **at most** `k` elements from the matrix `grid` such that:

* The number of elements taken from the <code>i<sup>th</sup></code> 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`
* <code>0 <= grid[i][j] <= 10<sup>5</sup></code>
* `0 <= limits[i] <= m`
* `0 <= k <= min(n * m, sum(limits))`
Original file line number Diff line number Diff line change
@@ -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..<n) {
arr[i] = s[i].code - '0'.code
}
var num1 = 0
var num2 = 0
for (i in 0..<n - 1) {
num1 = (num1 + xs[i] * arr[i]) % 10
num2 = (num2 + xs[i] * arr[i + 1]) % 10
}
return num1 == num2
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
3463\. Check If Digits Are Equal in String After Operations II

Hard

You are given a string `s` consisting of digits. Perform the following operation repeatedly until the string has **exactly** two digits:

* For each pair of consecutive digits in `s`, starting from the first digit, calculate a new digit as the sum of the two digits **modulo** 10.
* Replace `s` with the sequence of newly calculated digits, _maintaining the order_ in which they are computed.

Return `true` if the final two digits in `s` are the **same**; otherwise, return `false`.

**Example 1:**

**Input:** s = "3902"

**Output:** true

**Explanation:**

* Initially, `s = "3902"`
* First operation:
* `(s[0] + s[1]) % 10 = (3 + 9) % 10 = 2`
* `(s[1] + s[2]) % 10 = (9 + 0) % 10 = 9`
* `(s[2] + s[3]) % 10 = (0 + 2) % 10 = 2`
* `s` becomes `"292"`
* Second operation:
* `(s[0] + s[1]) % 10 = (2 + 9) % 10 = 1`
* `(s[1] + s[2]) % 10 = (9 + 2) % 10 = 1`
* `s` becomes `"11"`
* Since the digits in `"11"` are the same, the output is `true`.

**Example 2:**

**Input:** s = "34789"

**Output:** false

**Explanation:**

* Initially, `s = "34789"`.
* After the first operation, `s = "7157"`.
* After the second operation, `s = "862"`.
* After the third operation, `s = "48"`.
* Since `'4' != '8'`, the output is `false`.

**Constraints:**

* <code>3 <= s.length <= 10<sup>5</sup></code>
* `s` consists of only digits.
Original file line number Diff line number Diff line change
@@ -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<IntArray>, k: Int): Int {
val n = points.size
val p = LongArray(n)
for (i in 0..<n) {
val x = points[i][0]
val y = points[i][1]
val c: Long
if (y == 0) {
c = x.toLong()
} else if (x == side) {
c = side + y.toLong()
} else if (y == side) {
c = 2L * side + (side - x)
} else {
c = 3L * side + (side - y)
}
p[i] = c
}
p.sort()
val c = 4L * side
val tot = 2 * n
val dArr = LongArray(tot)
for (i in 0..<n) {
dArr[i] = p[i]
dArr[i + n] = p[i] + c
}
var lo = 0
var hi = 2 * side
var ans = 0
while (lo <= hi) {
val mid = (lo + hi) ushr 1
if (check(mid, dArr, n, k, c)) {
ans = mid
lo = mid + 1
} else {
hi = mid - 1
}
}
return ans
}

private fun check(d: Int, dArr: LongArray, n: Int, k: Int, c: Long): Boolean {
val len = dArr.size
val nxt = IntArray(len)
var j = 0
for (i in 0..<len) {
if (j < i + 1) {
j = i + 1
}
while (j < len && dArr[j] < dArr[i] + d) {
j++
}
nxt[i] = if (j < len) j else -1
}
for (i in 0..<n) {
var cnt = 1
var cur = i
while (cnt < k) {
val nx = nxt[cur]
if (nx == -1 || nx >= i + n) {
break
}
cur = nx
cnt++
}
if (cnt == k && (dArr[i] + c - dArr[cur]) >= d) {
return true
}
}
return false
}
}
Loading