Skip to content

Commit 48ab9f4

Browse files
committed
Added tasks 3722-3729
1 parent c062347 commit 48ab9f4

File tree

24 files changed

+920
-0
lines changed

24 files changed

+920
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package g3701_3800.s3722_lexicographically_smallest_string_after_reverse
2+
3+
// #Medium #Biweekly_Contest_168 #2025_10_28_Time_8_ms_(100.00%)_Space_45.74_MB_(100.00%)
4+
5+
class Solution {
6+
fun lexSmallest(s: String): String {
7+
val n = s.length
8+
val arr = s.toCharArray()
9+
val best = arr.clone()
10+
// Check all reverse first k operations
11+
for (k in 1..n) {
12+
if (isBetterReverseFirstK(arr, k, best)) {
13+
updateBestReverseFirstK(arr, k, best)
14+
}
15+
}
16+
// Check all reverse last k operations
17+
for (k in 1..n) {
18+
if (isBetterReverseLastK(arr, k, best)) {
19+
updateBestReverseLastK(arr, k, best)
20+
}
21+
}
22+
return String(best)
23+
}
24+
25+
private fun isBetterReverseFirstK(arr: CharArray, k: Int, best: CharArray): Boolean {
26+
for (i in arr.indices) {
27+
val currentChar = if (i < k) arr[k - 1 - i] else arr[i]
28+
if (currentChar < best[i]) {
29+
return true
30+
}
31+
if (currentChar > best[i]) {
32+
return false
33+
}
34+
}
35+
return false
36+
}
37+
38+
private fun isBetterReverseLastK(arr: CharArray, k: Int, best: CharArray): Boolean {
39+
val n = arr.size
40+
for (i in 0..<n) {
41+
val currentChar = if (i < n - k) arr[i] else arr[n - 1 - (i - (n - k))]
42+
if (currentChar < best[i]) {
43+
return true
44+
}
45+
if (currentChar > best[i]) {
46+
return false
47+
}
48+
}
49+
return false
50+
}
51+
52+
private fun updateBestReverseFirstK(arr: CharArray, k: Int, best: CharArray) {
53+
for (i in 0..<k) {
54+
best[i] = arr[k - 1 - i]
55+
}
56+
if (arr.size - k >= 0) {
57+
System.arraycopy(arr, k, best, k, arr.size - k)
58+
}
59+
}
60+
61+
private fun updateBestReverseLastK(arr: CharArray, k: Int, best: CharArray) {
62+
val n = arr.size
63+
if (n - k >= 0) {
64+
System.arraycopy(arr, 0, best, 0, n - k)
65+
}
66+
for (i in 0..<k) {
67+
best[n - k + i] = arr[n - 1 - i]
68+
}
69+
}
70+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
3722\. Lexicographically Smallest String After Reverse
2+
3+
Medium
4+
5+
You are given a string `s` of length `n` consisting of lowercase English letters.
6+
7+
You must perform **exactly** one operation by choosing any integer `k` such that `1 <= k <= n` and either:
8+
9+
* reverse the **first** `k` characters of `s`, or
10+
* reverse the **last** `k` characters of `s`.
11+
12+
Return the **lexicographically smallest** string that can be obtained after **exactly** one such operation.
13+
14+
A string `a` is **lexicographically smaller** than a string `b` if, at the first position where they differ, `a` has a letter that appears earlier in the alphabet than the corresponding letter in `b`. If the first `min(a.length, b.length)` characters are the same, then the shorter string is considered lexicographically smaller.
15+
16+
**Example 1:**
17+
18+
**Input:** s = "dcab"
19+
20+
**Output:** "acdb"
21+
22+
**Explanation:**
23+
24+
* Choose `k = 3`, reverse the first 3 characters.
25+
* Reverse `"dca"` to `"acd"`, resulting string `s = "acdb"`, which is the lexicographically smallest string achievable.
26+
27+
**Example 2:**
28+
29+
**Input:** s = "abba"
30+
31+
**Output:** "aabb"
32+
33+
**Explanation:**
34+
35+
* Choose `k = 3`, reverse the last 3 characters.
36+
* Reverse `"bba"` to `"abb"`, so the resulting string is `"aabb"`, which is the lexicographically smallest string achievable.
37+
38+
**Example 3:**
39+
40+
**Input:** s = "zxy"
41+
42+
**Output:** "xzy"
43+
44+
**Explanation:**
45+
46+
* Choose `k = 2`, reverse the first 2 characters.
47+
* Reverse `"zx"` to `"xz"`, so the resulting string is `"xzy"`, which is the lexicographically smallest string achievable.
48+
49+
**Constraints:**
50+
51+
* `1 <= n == s.length <= 1000`
52+
* `s` consists of lowercase English letters.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g3701_3800.s3723_maximize_sum_of_squares_of_digits
2+
3+
// #Medium #Biweekly_Contest_168 #2025_10_28_Time_16_ms_(94.44%)_Space_47.62_MB_(61.11%)
4+
5+
class Solution {
6+
fun maxSumOfSquares(places: Int, sum: Int): String {
7+
var ans = ""
8+
val nines = sum / 9
9+
if (places < nines) {
10+
return ans
11+
} else if (places == nines) {
12+
val remSum = sum - nines * 9
13+
if (remSum > 0) {
14+
return ans
15+
}
16+
ans = "9".repeat(nines)
17+
} else {
18+
val remSum = sum - nines * 9
19+
ans = "9".repeat(nines) + remSum
20+
val extra = places - ans.length
21+
if (extra > 0) {
22+
ans = ans + ("0".repeat(extra))
23+
}
24+
}
25+
return ans
26+
}
27+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
3723\. Maximize Sum of Squares of Digits
2+
3+
Medium
4+
5+
You are given two **positive** integers `num` and `sum`.
6+
7+
A positive integer `n` is **good** if it satisfies both of the following:
8+
9+
* The number of digits in `n` is **exactly** `num`.
10+
* The sum of digits in `n` is **exactly** `sum`.
11+
12+
The **score** of a **good** integer `n` is the sum of the squares of digits in `n`.
13+
14+
Return a **string** denoting the **good** integer `n` that achieves the **maximum** **score**. If there are multiple possible integers, return the **maximum** one. If no such integer exists, return an empty string.
15+
16+
**Example 1:**
17+
18+
**Input:** num = 2, sum = 3
19+
20+
**Output:** "30"
21+
22+
**Explanation:**
23+
24+
There are 3 good integers: 12, 21, and 30.
25+
26+
* The score of 12 is <code>1<sup>2</sup> + 2<sup>2</sup> = 5</code>.
27+
* The score of 21 is <code>2<sup>2</sup> + 1<sup>2</sup> = 5</code>.
28+
* The score of 30 is <code>3<sup>2</sup> + 0<sup>2</sup> = 9</code>.
29+
30+
The maximum score is 9, which is achieved by the good integer 30. Therefore, the answer is `"30"`.
31+
32+
**Example 2:**
33+
34+
**Input:** num = 2, sum = 17
35+
36+
**Output:** "98"
37+
38+
**Explanation:**
39+
40+
There are 2 good integers: 89 and 98.
41+
42+
* The score of 89 is <code>8<sup>2</sup> + 9<sup>2</sup> = 145</code>.
43+
* The score of 98 is <code>9<sup>2</sup> + 8<sup>2</sup> = 145</code>.
44+
45+
The maximum score is 145. The maximum good integer that achieves this score is 98. Therefore, the answer is `"98"`.
46+
47+
**Example 3:**
48+
49+
**Input:** num = 1, sum = 10
50+
51+
**Output:** ""
52+
53+
**Explanation:**
54+
55+
There are no integers that have exactly 1 digit and whose digits sum to 10. Therefore, the answer is `""`.
56+
57+
**Constraints:**
58+
59+
* <code>1 <= num <= 2 * 10<sup>5</sup></code>
60+
* <code>1 <= sum <= 2 * 10<sup>6</sup></code>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g3701_3800.s3724_minimum_operations_to_transform_array
2+
3+
// #Medium #Biweekly_Contest_168 #2025_10_28_Time_10_ms_(83.33%)_Space_66.99_MB_(100.00%)
4+
5+
import kotlin.math.abs
6+
import kotlin.math.max
7+
import kotlin.math.min
8+
9+
class Solution {
10+
fun minOperations(nums1: IntArray, nums2: IntArray): Long {
11+
val n = nums1.size
12+
val last = nums2[n]
13+
var steps: Long = 1
14+
var minDiffFromLast = Long.MAX_VALUE
15+
for (i in 0..<n) {
16+
val min = min(nums1[i], nums2[i])
17+
val max = max(nums1[i], nums2[i])
18+
steps += abs(max - min).toLong()
19+
if (minDiffFromLast > 0) {
20+
if (min <= last && last <= max) {
21+
minDiffFromLast = 0
22+
} else {
23+
minDiffFromLast = min(
24+
minDiffFromLast,
25+
min(abs(min - last), abs(max - last)).toLong(),
26+
)
27+
}
28+
}
29+
}
30+
return steps + minDiffFromLast
31+
}
32+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
3724\. Minimum Operations to Transform Array
2+
3+
Medium
4+
5+
You are given two integer arrays `nums1` of length `n` and `nums2` of length `n + 1`.
6+
7+
You want to transform `nums1` into `nums2` using the **minimum** number of operations.
8+
9+
You may perform the following operations **any** number of times, each time choosing an index `i`:
10+
11+
* **Increase** `nums1[i]` by 1.
12+
* **Decrease** `nums1[i]` by 1.
13+
* **Append** `nums1[i]` to the **end** of the array.
14+
15+
Return the **minimum** number of operations required to transform `nums1` into `nums2`.
16+
17+
**Example 1:**
18+
19+
**Input:** nums1 = [2,8], nums2 = [1,7,3]
20+
21+
**Output:** 4
22+
23+
**Explanation:**
24+
25+
| Step | `i` | Operation | `nums1[i]` | Updated `nums1` |
26+
|------|------|------------|-------------|----------------|
27+
| 1 | 0 | Append | - | [2, 8, 2] |
28+
| 2 | 0 | Decrement | Decreases to 1 | [1, 8, 2] |
29+
| 3 | 1 | Decrement | Decreases to 7 | [1, 7, 2] |
30+
| 4 | 2 | Increment | Increases to 3 | [1, 7, 3] |
31+
32+
Thus, after 4 operations `nums1` is transformed into `nums2`.
33+
34+
**Example 2:**
35+
36+
**Input:** nums1 = [1,3,6], nums2 = [2,4,5,3]
37+
38+
**Output:** 4
39+
40+
**Explanation:**
41+
42+
| Step | `i` | Operation | `nums1[i]` | Updated `nums1` |
43+
|------|------|------------|-------------|----------------|
44+
| 1 | 1 | Append | - | [1, 3, 6, 3] |
45+
| 2 | 0 | Increment | Increases to 2 | [2, 3, 6, 3] |
46+
| 3 | 1 | Increment | Increases to 4 | [2, 4, 6, 3] |
47+
| 4 | 2 | Decrement | Decreases to 5 | [2, 4, 5, 3] |
48+
49+
Thus, after 4 operations `nums1` is transformed into `nums2`.
50+
51+
**Example 3:**
52+
53+
**Input:** nums1 = [2], nums2 = [3,4]
54+
55+
**Output:** 3
56+
57+
**Explanation:**
58+
59+
| Step | `i` | Operation | `nums1[i]` | Updated `nums1` |
60+
|------|------|------------|-------------|----------------|
61+
| 1 | 0 | Increment | Increases to 3 | [3] |
62+
| 2 | 0 | Append | - | [3, 3] |
63+
| 3 | 1 | Increment | Increases to 4 | [3, 4] |
64+
65+
Thus, after 3 operations `nums1` is transformed into `nums2`.
66+
67+
**Constraints:**
68+
69+
* <code>1 <= n == nums1.length <= 10<sup>5</sup></code>
70+
* `nums2.length == n + 1`
71+
* <code>1 <= nums1[i], nums2[i] <= 10<sup>5</sup></code>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package g3701_3800.s3725_count_ways_to_choose_coprime_integers_from_rows
2+
3+
// #Hard #Biweekly_Contest_168 #2025_10_28_Time_29_ms_(100.00%)_Space_51.80_MB_(100.00%)
4+
5+
import kotlin.math.max
6+
7+
class Solution {
8+
fun countCoprime(mat: Array<IntArray>): Int {
9+
val m = mat.size
10+
val n = mat[0].size
11+
var maxVal = 0
12+
for (ints in mat) {
13+
for (j in 0..<n) {
14+
maxVal = max(maxVal, ints[j])
15+
}
16+
}
17+
val gcdWays: MutableMap<Int, Long> = HashMap()
18+
for (g in maxVal downTo 1) {
19+
var ways = countWaysWithDivisor(mat, g, m, n)
20+
if (ways > 0) {
21+
var multiple = 2 * g
22+
while (multiple <= maxVal) {
23+
if (gcdWays.containsKey(multiple)) {
24+
ways = (ways - gcdWays[multiple]!! + MOD) % MOD
25+
}
26+
multiple += g
27+
}
28+
gcdWays[g] = ways
29+
}
30+
}
31+
return gcdWays.getOrDefault(1, 0L).toInt()
32+
}
33+
34+
private fun countWaysWithDivisor(matrix: Array<IntArray>, divisor: Int, rows: Int, cols: Int): Long {
35+
var totalWays: Long = 1
36+
for (row in 0..<rows) {
37+
var validChoices = 0
38+
for (col in 0..<cols) {
39+
if (matrix[row][col] % divisor == 0) {
40+
validChoices++
41+
}
42+
}
43+
if (validChoices == 0) {
44+
return 0
45+
}
46+
totalWays = (totalWays * validChoices) % MOD
47+
}
48+
return totalWays
49+
}
50+
51+
companion object {
52+
private const val MOD = 1000000007
53+
}
54+
}

0 commit comments

Comments
 (0)