Skip to content

Commit f99d0cd

Browse files
authored
Added tasks 3461-3464
1 parent 791d518 commit f99d0cd

File tree

13 files changed

+533
-1
lines changed

13 files changed

+533
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Write your MySQL query statement below
2-
# #Easy #2025_02_04_Time_451_ms_(70.84%)_Space_0.0_MB_(100.00%)
2+
# #Easy #Database #2025_02_04_Time_451_ms_(70.84%)_Space_0.0_MB_(100.00%)
33
select user_id, email from users
44
where email regexp '^[A-Za-z0-9_]+@[A-Za-z][A-Za-z0-9_]*\.com$'
55
order by user_id
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g3401_3500.s3461_check_if_digits_are_equal_in_string_after_operations_i
2+
3+
// #Easy #String #Math #Simulation #Number_Theory #Combinatorics
4+
// #2025_02_25_Time_3_ms_(100.00%)_Space_35.54_MB_(100.00%)
5+
6+
class Solution {
7+
fun hasSameDigits(s: String): Boolean {
8+
val ch = s.toCharArray()
9+
var k = ch.size - 1
10+
while (k != 1) {
11+
for (i in 0..<k) {
12+
val a = ch[i].code - 48
13+
val b = ch[i + 1].code - 48
14+
val d = (a + b) % 10
15+
val c = (d + '0'.code).toChar()
16+
ch[i] = c
17+
}
18+
k--
19+
}
20+
return ch[0] == ch[1]
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
3461\. Check If Digits Are Equal in String After Operations I
2+
3+
Easy
4+
5+
You are given a string `s` consisting of digits. Perform the following operation repeatedly until the string has **exactly** two digits:
6+
7+
* 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.
8+
* Replace `s` with the sequence of newly calculated digits, _maintaining the order_ in which they are computed.
9+
10+
Return `true` if the final two digits in `s` are the **same**; otherwise, return `false`.
11+
12+
**Example 1:**
13+
14+
**Input:** s = "3902"
15+
16+
**Output:** true
17+
18+
**Explanation:**
19+
20+
* Initially, `s = "3902"`
21+
* First operation:
22+
* `(s[0] + s[1]) % 10 = (3 + 9) % 10 = 2`
23+
* `(s[1] + s[2]) % 10 = (9 + 0) % 10 = 9`
24+
* `(s[2] + s[3]) % 10 = (0 + 2) % 10 = 2`
25+
* `s` becomes `"292"`
26+
* Second operation:
27+
* `(s[0] + s[1]) % 10 = (2 + 9) % 10 = 1`
28+
* `(s[1] + s[2]) % 10 = (9 + 2) % 10 = 1`
29+
* `s` becomes `"11"`
30+
* Since the digits in `"11"` are the same, the output is `true`.
31+
32+
**Example 2:**
33+
34+
**Input:** s = "34789"
35+
36+
**Output:** false
37+
38+
**Explanation:**
39+
40+
* Initially, `s = "34789"`.
41+
* After the first operation, `s = "7157"`.
42+
* After the second operation, `s = "862"`.
43+
* After the third operation, `s = "48"`.
44+
* Since `'4' != '8'`, the output is `false`.
45+
46+
**Constraints:**
47+
48+
* `3 <= s.length <= 100`
49+
* `s` consists of only digits.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g3401_3500.s3462_maximum_sum_with_at_most_k_elements
2+
3+
// #Medium #Array #Sorting #Greedy #Matrix #Heap_(Priority_Queue)
4+
// #2025_02_25_Time_139_ms_(100.00%)_Space_88.84_MB_(79.31%)
5+
6+
class Solution {
7+
fun maxSum(grid: Array<IntArray>, limits: IntArray, k: Int): Long {
8+
var l = 0
9+
for (i in limits.indices) {
10+
l += limits[i]
11+
}
12+
val dp = IntArray(l)
13+
var a = 0
14+
for (i in grid.indices) {
15+
val lim = limits[i]
16+
grid[i].sort()
17+
for (j in grid[i].size - lim..<grid[i].size) {
18+
dp[a] = grid[i][j]
19+
a++
20+
}
21+
}
22+
dp.sort()
23+
var sum = 0L
24+
for (i in l - 1 downTo l - k) {
25+
sum += dp[i].toLong()
26+
}
27+
return sum
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
3462\. Maximum Sum With at Most K Elements
2+
3+
Medium
4+
5+
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:
6+
7+
* The number of elements taken from the <code>i<sup>th</sup></code> row of `grid` does not exceed `limits[i]`.
8+
9+
10+
Return the **maximum sum**.
11+
12+
**Example 1:**
13+
14+
**Input:** grid = [[1,2],[3,4]], limits = [1,2], k = 2
15+
16+
**Output:** 7
17+
18+
**Explanation:**
19+
20+
* From the second row, we can take at most 2 elements. The elements taken are 4 and 3.
21+
* The maximum possible sum of at most 2 selected elements is `4 + 3 = 7`.
22+
23+
**Example 2:**
24+
25+
**Input:** grid = [[5,3,7],[8,2,6]], limits = [2,2], k = 3
26+
27+
**Output:** 21
28+
29+
**Explanation:**
30+
31+
* From the first row, we can take at most 2 elements. The element taken is 7.
32+
* From the second row, we can take at most 2 elements. The elements taken are 8 and 6.
33+
* The maximum possible sum of at most 3 selected elements is `7 + 8 + 6 = 21`.
34+
35+
**Constraints:**
36+
37+
* `n == grid.length == limits.length`
38+
* `m == grid[i].length`
39+
* `1 <= n, m <= 500`
40+
* <code>0 <= grid[i][j] <= 10<sup>5</sup></code>
41+
* `0 <= limits[i] <= m`
42+
* `0 <= k <= min(n * m, sum(limits))`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package g3401_3500.s3463_check_if_digits_are_equal_in_string_after_operations_ii
2+
3+
// #Hard #String #Math #Number_Theory #Combinatorics
4+
// #2025_02_25_Time_38_ms_(100.00%)_Space_45.90_MB_(11.11%)
5+
6+
class Solution {
7+
private fun powMod10(a: Int, n: Int): Int {
8+
var a = a
9+
var n = n
10+
var x = 1
11+
while (n >= 1) {
12+
if (n % 2 == 1) {
13+
x = (x * a) % 10
14+
}
15+
a = (a * a) % 10
16+
n /= 2
17+
}
18+
return x
19+
}
20+
21+
private fun f(n: Int): IntArray {
22+
val ns = IntArray(n + 1)
23+
val n2 = IntArray(n + 1)
24+
val n5 = IntArray(n + 1)
25+
ns[0] = 1
26+
for (i in 1..n) {
27+
var m = i
28+
n2[i] = n2[i - 1]
29+
n5[i] = n5[i - 1]
30+
while (m % 2 == 0) {
31+
m /= 2
32+
n2[i]++
33+
}
34+
while (m % 5 == 0) {
35+
m /= 5
36+
n5[i]++
37+
}
38+
ns[i] = (ns[i - 1] * m) % 10
39+
}
40+
val inv = IntArray(10)
41+
for (i in 1..9) {
42+
for (j in 0..9) {
43+
if (i * j % 10 == 1) {
44+
inv[i] = j
45+
}
46+
}
47+
}
48+
val xs = IntArray(n + 1)
49+
for (k in 0..n) {
50+
var a = 0
51+
val s2 = n2[n] - n2[n - k] - n2[k]
52+
val s5 = n5[n] - n5[n - k] - n5[k]
53+
if (s2 == 0 || s5 == 0) {
54+
a = (ns[n] * inv[ns[n - k]] * inv[ns[k]] * powMod10(2, s2) * powMod10(5, s5)) % 10
55+
}
56+
xs[k] = a
57+
}
58+
return xs
59+
}
60+
61+
fun hasSameDigits(s: String): Boolean {
62+
val n = s.length
63+
val xs = f(n - 2)
64+
val arr = IntArray(n)
65+
for (i in 0..<n) {
66+
arr[i] = s[i].code - '0'.code
67+
}
68+
var num1 = 0
69+
var num2 = 0
70+
for (i in 0..<n - 1) {
71+
num1 = (num1 + xs[i] * arr[i]) % 10
72+
num2 = (num2 + xs[i] * arr[i + 1]) % 10
73+
}
74+
return num1 == num2
75+
}
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
3463\. Check If Digits Are Equal in String After Operations II
2+
3+
Hard
4+
5+
You are given a string `s` consisting of digits. Perform the following operation repeatedly until the string has **exactly** two digits:
6+
7+
* 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.
8+
* Replace `s` with the sequence of newly calculated digits, _maintaining the order_ in which they are computed.
9+
10+
Return `true` if the final two digits in `s` are the **same**; otherwise, return `false`.
11+
12+
**Example 1:**
13+
14+
**Input:** s = "3902"
15+
16+
**Output:** true
17+
18+
**Explanation:**
19+
20+
* Initially, `s = "3902"`
21+
* First operation:
22+
* `(s[0] + s[1]) % 10 = (3 + 9) % 10 = 2`
23+
* `(s[1] + s[2]) % 10 = (9 + 0) % 10 = 9`
24+
* `(s[2] + s[3]) % 10 = (0 + 2) % 10 = 2`
25+
* `s` becomes `"292"`
26+
* Second operation:
27+
* `(s[0] + s[1]) % 10 = (2 + 9) % 10 = 1`
28+
* `(s[1] + s[2]) % 10 = (9 + 2) % 10 = 1`
29+
* `s` becomes `"11"`
30+
* Since the digits in `"11"` are the same, the output is `true`.
31+
32+
**Example 2:**
33+
34+
**Input:** s = "34789"
35+
36+
**Output:** false
37+
38+
**Explanation:**
39+
40+
* Initially, `s = "34789"`.
41+
* After the first operation, `s = "7157"`.
42+
* After the second operation, `s = "862"`.
43+
* After the third operation, `s = "48"`.
44+
* Since `'4' != '8'`, the output is `false`.
45+
46+
**Constraints:**
47+
48+
* <code>3 <= s.length <= 10<sup>5</sup></code>
49+
* `s` consists of only digits.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package g3401_3500.s3464_maximize_the_distance_between_points_on_a_square
2+
3+
// #Hard #Array #Greedy #Binary_Search #2025_02_25_Time_18_ms_(98.51%)_Space_49.78_MB_(46.27%)
4+
5+
class Solution {
6+
fun maxDistance(side: Int, points: Array<IntArray>, k: Int): Int {
7+
val n = points.size
8+
val p = LongArray(n)
9+
for (i in 0..<n) {
10+
val x = points[i][0]
11+
val y = points[i][1]
12+
val c: Long
13+
if (y == 0) {
14+
c = x.toLong()
15+
} else if (x == side) {
16+
c = side + y.toLong()
17+
} else if (y == side) {
18+
c = 2L * side + (side - x)
19+
} else {
20+
c = 3L * side + (side - y)
21+
}
22+
p[i] = c
23+
}
24+
p.sort()
25+
val c = 4L * side
26+
val tot = 2 * n
27+
val dArr = LongArray(tot)
28+
for (i in 0..<n) {
29+
dArr[i] = p[i]
30+
dArr[i + n] = p[i] + c
31+
}
32+
var lo = 0
33+
var hi = 2 * side
34+
var ans = 0
35+
while (lo <= hi) {
36+
val mid = (lo + hi) ushr 1
37+
if (check(mid, dArr, n, k, c)) {
38+
ans = mid
39+
lo = mid + 1
40+
} else {
41+
hi = mid - 1
42+
}
43+
}
44+
return ans
45+
}
46+
47+
private fun check(d: Int, dArr: LongArray, n: Int, k: Int, c: Long): Boolean {
48+
val len = dArr.size
49+
val nxt = IntArray(len)
50+
var j = 0
51+
for (i in 0..<len) {
52+
if (j < i + 1) {
53+
j = i + 1
54+
}
55+
while (j < len && dArr[j] < dArr[i] + d) {
56+
j++
57+
}
58+
nxt[i] = if (j < len) j else -1
59+
}
60+
for (i in 0..<n) {
61+
var cnt = 1
62+
var cur = i
63+
while (cnt < k) {
64+
val nx = nxt[cur]
65+
if (nx == -1 || nx >= i + n) {
66+
break
67+
}
68+
cur = nx
69+
cnt++
70+
}
71+
if (cnt == k && (dArr[i] + c - dArr[cur]) >= d) {
72+
return true
73+
}
74+
}
75+
return false
76+
}
77+
}

0 commit comments

Comments
 (0)