Skip to content

Commit 7aa800a

Browse files
committed
Added tasks 3461-3464
1 parent 2674eaa commit 7aa800a

File tree

12 files changed

+547
-0
lines changed

12 files changed

+547
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g3401_3500.s3461_check_if_digits_are_equal_in_string_after_operations_i
2+
3+
// #Easy #2025_02_23_Time_8_ms_(100.00%)_Space_37.90_MB_(100.00%)
4+
5+
class Solution {
6+
fun hasSameDigits(s: String): Boolean {
7+
var s = s
8+
var n = s.length
9+
while (n > 2) {
10+
val nstr = StringBuilder()
11+
for (i in 1..<n) {
12+
val next = ((s[i].code - '0'.code) + (s[i - 1].code - '0'.code)) % 10
13+
nstr.append(next)
14+
}
15+
n--
16+
s = nstr.toString()
17+
}
18+
return s[0] == s[1]
19+
}
20+
}
Lines changed: 49 additions & 0 deletions
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.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g3401_3500.s3462_maximum_sum_with_at_most_k_elements
2+
3+
// #Medium #2025_02_23_Time_202_ms_(100.00%)_Space_107.52_MB_(100.00%)
4+
5+
import java.util.Collections
6+
import java.util.PriorityQueue
7+
8+
class Solution {
9+
fun maxSum(grid: Array<IntArray>, limits: IntArray, k: Int): Long {
10+
if (grid.isEmpty()) {
11+
return 0
12+
}
13+
val pq = PriorityQueue<Int>(Collections.reverseOrder<Int>())
14+
var temp: PriorityQueue<Int>?
15+
for (i in grid.indices) {
16+
temp = PriorityQueue<Int>(Collections.reverseOrder<Int>())
17+
for (j in grid[i].indices) {
18+
temp.add(grid[i][j])
19+
}
20+
var cnt = 0
21+
while (temp.isNotEmpty() && cnt < limits[i]) {
22+
pq.add(temp.poll())
23+
cnt += 1
24+
}
25+
}
26+
var result: Long = 0
27+
var count: Long = 0
28+
while (pq.isNotEmpty() && count < k) {
29+
result += pq.poll()!!.toLong()
30+
count += 1
31+
}
32+
return result
33+
}
34+
}
Lines changed: 42 additions & 0 deletions
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))`
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package g3401_3500.s3463_check_if_digits_are_equal_in_string_after_operations_ii
2+
3+
// #Hard #2025_02_23_Time_63_ms_(100.00%)_Space_44.31_MB_(100.00%)
4+
5+
class Solution {
6+
fun hasSameDigits(s: String): Boolean {
7+
val n = s.length
8+
val nMunus2 = n - 2
9+
var f0 = 0
10+
var f1 = 0
11+
for (j in 0..nMunus2) {
12+
val c = binomMod10(nMunus2, j)
13+
f0 = (f0 + c * (s[j].code - '0'.code)) % 10
14+
f1 = (f1 + c * (s[j + 1].code - '0'.code)) % 10
15+
}
16+
return f0 == f1
17+
}
18+
19+
private fun binomMod10(n: Int, k: Int): Int {
20+
val r2 = binomMod2(n, k)
21+
val r5 = binomMod5(n, k)
22+
for (x in 0..9) {
23+
if (x % 2 == r2 && x % 5 == r5) {
24+
return x
25+
}
26+
}
27+
return 0
28+
}
29+
30+
private fun binomMod2(n: Int, k: Int): Int {
31+
return if ((n and k) == k) 1 else 0
32+
}
33+
34+
private fun binomMod5(n: Int, k: Int): Int {
35+
var n = n
36+
var k = k
37+
val t = arrayOf<IntArray?>(
38+
intArrayOf(1),
39+
intArrayOf(1, 1),
40+
intArrayOf(1, 2, 1),
41+
intArrayOf(1, 3, 3, 1),
42+
intArrayOf(1, 4, 1, 4, 1),
43+
)
44+
var res = 1
45+
while (n > 0 || k > 0) {
46+
val nd = n % 5
47+
val kd = k % 5
48+
if (kd > nd) {
49+
return 0
50+
}
51+
res = (res * t[nd]!![kd]) % 5
52+
n /= 5
53+
k /= 5
54+
}
55+
return res
56+
}
57+
}
Lines changed: 49 additions & 0 deletions
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.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package g3401_3500.s3464_maximize_the_distance_between_points_on_a_square
2+
3+
// #Hard #2025_02_23_Time_159_ms_(100.00%)_Space_67.20_MB_(100.00%)
4+
5+
class Solution {
6+
fun maxDistance(sideLength: Int, points: Array<IntArray>, requiredPoints: Int): Int {
7+
val perimeter = 4L * sideLength
8+
val numPoints = points.size
9+
val mappedPositions = LongArray(numPoints)
10+
for (i in 0..<numPoints) {
11+
mappedPositions[i] = mapToPerimeter(sideLength, points[i][0], points[i][1])
12+
}
13+
mappedPositions.sort()
14+
var low: Long = 0
15+
var high = perimeter
16+
while (low < high) {
17+
val mid = (low + high + 1) / 2
18+
if (isValidDistance(mid, requiredPoints, mappedPositions, perimeter)) {
19+
low = mid
20+
} else {
21+
high = mid - 1
22+
}
23+
}
24+
return low.toInt()
25+
}
26+
27+
private fun mapToPerimeter(sideLength: Int, x: Int, y: Int): Long {
28+
if (y == sideLength) {
29+
return x.toLong()
30+
}
31+
if (x == sideLength) {
32+
return sideLength + (sideLength - y).toLong()
33+
}
34+
if (y == 0) {
35+
return 2L * sideLength + (sideLength - x)
36+
}
37+
return 3L * sideLength + y
38+
}
39+
40+
private fun isValidDistance(
41+
minDistance: Long,
42+
requiredPoints: Int,
43+
mappedPositions: LongArray,
44+
perimeter: Long,
45+
): Boolean {
46+
val numPoints = mappedPositions.size
47+
val extendedPositions = LongArray(2 * numPoints)
48+
for (i in 0..<numPoints) {
49+
extendedPositions[i] = mappedPositions[i]
50+
extendedPositions[i + numPoints] = mappedPositions[i] + perimeter
51+
}
52+
for (i in 0..<numPoints) {
53+
if (canSelectRequiredPoints(
54+
i,
55+
minDistance,
56+
requiredPoints,
57+
mappedPositions,
58+
extendedPositions,
59+
perimeter,
60+
)
61+
) {
62+
return true
63+
}
64+
}
65+
return false
66+
}
67+
68+
private fun canSelectRequiredPoints(
69+
startIndex: Int,
70+
minDistance: Long,
71+
requiredPoints: Int,
72+
mappedPositions: LongArray,
73+
extendedPositions: LongArray,
74+
perimeter: Long,
75+
): Boolean {
76+
var selectedCount = 1
77+
var previousPosition = mappedPositions[startIndex]
78+
var currentIndex = startIndex
79+
for (i in 1..<requiredPoints) {
80+
val targetPosition = previousPosition + minDistance
81+
val left = currentIndex + 1
82+
val right = startIndex + mappedPositions.size
83+
val nextIndex = lowerBound(extendedPositions, left, right, targetPosition)
84+
if (nextIndex >= right) {
85+
return false
86+
}
87+
selectedCount++
88+
previousPosition = extendedPositions[nextIndex]
89+
currentIndex = nextIndex
90+
}
91+
return selectedCount == requiredPoints &&
92+
(previousPosition - mappedPositions[startIndex] <= perimeter - minDistance)
93+
}
94+
95+
private fun lowerBound(arr: LongArray, left: Int, right: Int, target: Long): Int {
96+
var left = left
97+
var right = right
98+
while (left < right) {
99+
val mid = left + (right - left) / 2
100+
if (arr[mid] >= target) {
101+
right = mid
102+
} else {
103+
left = mid + 1
104+
}
105+
}
106+
return left
107+
}
108+
}

0 commit comments

Comments
 (0)