Skip to content

Commit af24ba5

Browse files
authored
Added tasks 3722-3729
1 parent c062347 commit af24ba5

File tree

24 files changed

+924
-0
lines changed

24 files changed

+924
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package g3701_3800.s3722_lexicographically_smallest_string_after_reverse
2+
3+
// #Medium #Binary_Search #Two_Pointers #Enumeration #Biweekly_Contest_168
4+
// #2025_10_28_Time_8_ms_(100.00%)_Space_45.74_MB_(100.00%)
5+
6+
class Solution {
7+
fun lexSmallest(s: String): String {
8+
val n = s.length
9+
val arr = s.toCharArray()
10+
val best = arr.clone()
11+
// Check all reverse first k operations
12+
for (k in 1..n) {
13+
if (isBetterReverseFirstK(arr, k, best)) {
14+
updateBestReverseFirstK(arr, k, best)
15+
}
16+
}
17+
// Check all reverse last k operations
18+
for (k in 1..n) {
19+
if (isBetterReverseLastK(arr, k, best)) {
20+
updateBestReverseLastK(arr, k, best)
21+
}
22+
}
23+
return String(best)
24+
}
25+
26+
private fun isBetterReverseFirstK(arr: CharArray, k: Int, best: CharArray): Boolean {
27+
for (i in arr.indices) {
28+
val currentChar = if (i < k) arr[k - 1 - i] else arr[i]
29+
if (currentChar < best[i]) {
30+
return true
31+
}
32+
if (currentChar > best[i]) {
33+
return false
34+
}
35+
}
36+
return false
37+
}
38+
39+
private fun isBetterReverseLastK(arr: CharArray, k: Int, best: CharArray): Boolean {
40+
val n = arr.size
41+
for (i in 0..<n) {
42+
val currentChar = if (i < n - k) arr[i] else arr[n - 1 - (i - (n - k))]
43+
if (currentChar < best[i]) {
44+
return true
45+
}
46+
if (currentChar > best[i]) {
47+
return false
48+
}
49+
}
50+
return false
51+
}
52+
53+
private fun updateBestReverseFirstK(arr: CharArray, k: Int, best: CharArray) {
54+
for (i in 0..<k) {
55+
best[i] = arr[k - 1 - i]
56+
}
57+
if (arr.size - k >= 0) {
58+
System.arraycopy(arr, k, best, k, arr.size - k)
59+
}
60+
}
61+
62+
private fun updateBestReverseLastK(arr: CharArray, k: Int, best: CharArray) {
63+
val n = arr.size
64+
if (n - k >= 0) {
65+
System.arraycopy(arr, 0, best, 0, n - k)
66+
}
67+
for (i in 0..<k) {
68+
best[n - k + i] = arr[n - 1 - i]
69+
}
70+
}
71+
}
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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g3701_3800.s3723_maximize_sum_of_squares_of_digits
2+
3+
// #Medium #Math #Greedy #Biweekly_Contest_168
4+
// #2025_10_28_Time_16_ms_(94.44%)_Space_47.62_MB_(61.11%)
5+
6+
class Solution {
7+
fun maxSumOfSquares(places: Int, sum: Int): String {
8+
var ans = ""
9+
val nines = sum / 9
10+
if (places < nines) {
11+
return ans
12+
} else if (places == nines) {
13+
val remSum = sum - nines * 9
14+
if (remSum > 0) {
15+
return ans
16+
}
17+
ans = "9".repeat(nines)
18+
} else {
19+
val remSum = sum - nines * 9
20+
ans = "9".repeat(nines) + remSum
21+
val extra = places - ans.length
22+
if (extra > 0) {
23+
ans = ans + ("0".repeat(extra))
24+
}
25+
}
26+
return ans
27+
}
28+
}
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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g3701_3800.s3724_minimum_operations_to_transform_array
2+
3+
// #Medium #Array #Greedy #Biweekly_Contest_168
4+
// #2025_10_28_Time_10_ms_(83.33%)_Space_66.99_MB_(100.00%)
5+
6+
import kotlin.math.abs
7+
import kotlin.math.max
8+
import kotlin.math.min
9+
10+
class Solution {
11+
fun minOperations(nums1: IntArray, nums2: IntArray): Long {
12+
val n = nums1.size
13+
val last = nums2[n]
14+
var steps: Long = 1
15+
var minDiffFromLast = Long.MAX_VALUE
16+
for (i in 0..<n) {
17+
val min = min(nums1[i], nums2[i])
18+
val max = max(nums1[i], nums2[i])
19+
steps += abs(max - min).toLong()
20+
if (minDiffFromLast > 0) {
21+
if (min <= last && last <= max) {
22+
minDiffFromLast = 0
23+
} else {
24+
minDiffFromLast = min(
25+
minDiffFromLast,
26+
min(abs(min - last), abs(max - last)).toLong(),
27+
)
28+
}
29+
}
30+
}
31+
return steps + minDiffFromLast
32+
}
33+
}
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: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package g3701_3800.s3725_count_ways_to_choose_coprime_integers_from_rows
2+
3+
// #Hard #Array #Dynamic_Programming #Math #Matrix #Number_Theory #Combinatorics
4+
// #Biweekly_Contest_168 #2025_10_28_Time_29_ms_(100.00%)_Space_51.80_MB_(100.00%)
5+
6+
import kotlin.math.max
7+
8+
class Solution {
9+
fun countCoprime(mat: Array<IntArray>): Int {
10+
val m = mat.size
11+
val n = mat[0].size
12+
var maxVal = 0
13+
for (ints in mat) {
14+
for (j in 0..<n) {
15+
maxVal = max(maxVal, ints[j])
16+
}
17+
}
18+
val gcdWays: MutableMap<Int, Long> = HashMap()
19+
for (g in maxVal downTo 1) {
20+
var ways = countWaysWithDivisor(mat, g, m, n)
21+
if (ways > 0) {
22+
var multiple = 2 * g
23+
while (multiple <= maxVal) {
24+
if (gcdWays.containsKey(multiple)) {
25+
ways = (ways - gcdWays[multiple]!! + MOD) % MOD
26+
}
27+
multiple += g
28+
}
29+
gcdWays[g] = ways
30+
}
31+
}
32+
return gcdWays.getOrDefault(1, 0L).toInt()
33+
}
34+
35+
private fun countWaysWithDivisor(matrix: Array<IntArray>, divisor: Int, rows: Int, cols: Int): Long {
36+
var totalWays: Long = 1
37+
for (row in 0..<rows) {
38+
var validChoices = 0
39+
for (col in 0..<cols) {
40+
if (matrix[row][col] % divisor == 0) {
41+
validChoices++
42+
}
43+
}
44+
if (validChoices == 0) {
45+
return 0
46+
}
47+
totalWays = (totalWays * validChoices) % MOD
48+
}
49+
return totalWays
50+
}
51+
52+
companion object {
53+
private const val MOD = 1000000007
54+
}
55+
}

0 commit comments

Comments
 (0)