Skip to content

Commit c7b9201

Browse files
committed
Added tasks 3722-3729
1 parent b3a8167 commit c7b9201

File tree

24 files changed

+846
-0
lines changed

24 files changed

+846
-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_26_Time_8_ms_(100.00%)_Space_44.98_MB_(100.00%)
4+
5+
public class Solution {
6+
public String lexSmallest(String s) {
7+
int n = s.length();
8+
char[] arr = s.toCharArray();
9+
char[] best = arr.clone();
10+
// Check all reverse first k operations
11+
for (int k = 1; k <= n; k++) {
12+
if (isBetterReverseFirstK(arr, k, best)) {
13+
updateBestReverseFirstK(arr, k, best);
14+
}
15+
}
16+
// Check all reverse last k operations
17+
for (int k = 1; k <= n; k++) {
18+
if (isBetterReverseLastK(arr, k, best)) {
19+
updateBestReverseLastK(arr, k, best);
20+
}
21+
}
22+
return new String(best);
23+
}
24+
25+
private boolean isBetterReverseFirstK(char[] arr, int k, char[] best) {
26+
for (int i = 0; i < arr.length; i++) {
27+
char currentChar = (i < k) ? arr[k - 1 - i] : 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 boolean isBetterReverseLastK(char[] arr, int k, char[] best) {
39+
int n = arr.length;
40+
for (int i = 0; i < n; i++) {
41+
char currentChar = (i < n - k) ? arr[i] : 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 void updateBestReverseFirstK(char[] arr, int k, char[] best) {
53+
for (int i = 0; i < k; i++) {
54+
best[i] = arr[k - 1 - i];
55+
}
56+
if (arr.length - k >= 0) {
57+
System.arraycopy(arr, k, best, k, arr.length - k);
58+
}
59+
}
60+
61+
private void updateBestReverseLastK(char[] arr, int k, char[] best) {
62+
int n = arr.length;
63+
if (n - k >= 0) {
64+
System.arraycopy(arr, 0, best, 0, n - k);
65+
}
66+
for (int i = 0; i < k; i++) {
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_26_Time_8_ms_(99.81%)_Space_45.78_MB_(_%)
4+
5+
public class Solution {
6+
public String maxSumOfSquares(int places, int sum) {
7+
String ans = "";
8+
int nines = sum / 9;
9+
if (places < nines) {
10+
return ans;
11+
} else if (places == nines) {
12+
int remSum = sum - nines * 9;
13+
if (remSum > 0) {
14+
return ans;
15+
}
16+
ans = "9".repeat(nines);
17+
} else {
18+
int remSum = sum - nines * 9;
19+
ans = "9".repeat(nines) + remSum;
20+
int 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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g3701_3800.s3724_minimum_operations_to_transform_array;
2+
3+
// #Medium #Biweekly_Contest_168 #2025_10_26_Time_2_ms_(100.00%)_Space_61.61_MB_(100.00%)
4+
5+
public class Solution {
6+
public long minOperations(int[] nums1, int[] nums2) {
7+
int n = nums1.length;
8+
int last = nums2[n];
9+
long steps = 1;
10+
long minDiffFromLast = Long.MAX_VALUE;
11+
for (int i = 0; i < n; i++) {
12+
int min = Math.min(nums1[i], nums2[i]);
13+
int max = Math.max(nums1[i], nums2[i]);
14+
steps += Math.abs(max - min);
15+
if (minDiffFromLast > 0) {
16+
if (min <= last && last <= max) {
17+
minDiffFromLast = 0;
18+
} else {
19+
minDiffFromLast =
20+
Math.min(
21+
minDiffFromLast,
22+
Math.min(Math.abs(min - last), Math.abs(max - last)));
23+
}
24+
}
25+
}
26+
return steps + minDiffFromLast;
27+
}
28+
}
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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package g3701_3800.s3725_count_ways_to_choose_coprime_integers_from_rows;
2+
3+
// #Hard #Biweekly_Contest_168 #2025_10_26_Time_31_ms_(92.56%)_Space_49.21_MB_(_%)
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public class Solution {
9+
private static final int MOD = 1_000_000_007;
10+
11+
public int countCoprime(int[][] mat) {
12+
int m = mat.length;
13+
int n = mat[0].length;
14+
int maxVal = 0;
15+
for (int[] ints : mat) {
16+
for (int j = 0; j < n; j++) {
17+
maxVal = Math.max(maxVal, ints[j]);
18+
}
19+
}
20+
Map<Integer, Long> gcdWays = new HashMap<>();
21+
for (int g = maxVal; g >= 1; g--) {
22+
long ways = countWaysWithDivisor(mat, g, m, n);
23+
if (ways > 0) {
24+
for (int multiple = 2 * g; multiple <= maxVal; multiple += g) {
25+
if (gcdWays.containsKey(multiple)) {
26+
ways = (ways - gcdWays.get(multiple) + MOD) % MOD;
27+
}
28+
}
29+
gcdWays.put(g, ways);
30+
}
31+
}
32+
return gcdWays.getOrDefault(1, 0L).intValue();
33+
}
34+
35+
private long countWaysWithDivisor(int[][] matrix, int divisor, int rows, int cols) {
36+
long totalWays = 1;
37+
for (int row = 0; row < rows; row++) {
38+
int validChoices = 0;
39+
for (int col = 0; col < cols; col++) {
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+
}

0 commit comments

Comments
 (0)