Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package g3701_3800.s3722_lexicographically_smallest_string_after_reverse;

// #Medium #Binary_Search #Two_Pointers #Enumeration #Biweekly_Contest_168
// #2025_10_29_Time_7_ms_(100.00%)_Space_45.70_MB_(100.00%)

public class Solution {
public String lexSmallest(String s) {
int n = s.length();
char[] arr = s.toCharArray();
char[] best = arr.clone();
// Check all reverse first k operations
for (int k = 1; k <= n; k++) {
if (isBetterReverseFirstK(arr, k, best)) {
updateBestReverseFirstK(arr, k, best);
}
}
// Check all reverse last k operations
for (int k = 1; k <= n; k++) {
if (isBetterReverseLastK(arr, k, best)) {
updateBestReverseLastK(arr, k, best);
}
}
return new String(best);
}

private boolean isBetterReverseFirstK(char[] arr, int k, char[] best) {
for (int i = 0; i < arr.length; i++) {
char currentChar = (i < k) ? arr[k - 1 - i] : arr[i];
if (currentChar < best[i]) {
return true;
}
if (currentChar > best[i]) {
return false;
}
}
return false;
}

private boolean isBetterReverseLastK(char[] arr, int k, char[] best) {
int n = arr.length;
for (int i = 0; i < n; i++) {
char currentChar = (i < n - k) ? arr[i] : arr[n - 1 - (i - (n - k))];
if (currentChar < best[i]) {
return true;
}
if (currentChar > best[i]) {
return false;
}
}
return false;
}

private void updateBestReverseFirstK(char[] arr, int k, char[] best) {
for (int i = 0; i < k; i++) {
best[i] = arr[k - 1 - i];
}
if (arr.length - k >= 0) {
System.arraycopy(arr, k, best, k, arr.length - k);
}
}

private void updateBestReverseLastK(char[] arr, int k, char[] best) {
int n = arr.length;
if (n - k >= 0) {
System.arraycopy(arr, 0, best, 0, n - k);
}
for (int i = 0; i < k; i++) {
best[n - k + i] = arr[n - 1 - i];
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
3722\. Lexicographically Smallest String After Reverse

Medium

You are given a string `s` of length `n` consisting of lowercase English letters.

You must perform **exactly** one operation by choosing any integer `k` such that `1 <= k <= n` and either:

* reverse the **first** `k` characters of `s`, or
* reverse the **last** `k` characters of `s`.

Return the **lexicographically smallest** string that can be obtained after **exactly** one such operation.

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.

**Example 1:**

**Input:** s = "dcab"

**Output:** "acdb"

**Explanation:**

* Choose `k = 3`, reverse the first 3 characters.
* Reverse `"dca"` to `"acd"`, resulting string `s = "acdb"`, which is the lexicographically smallest string achievable.

**Example 2:**

**Input:** s = "abba"

**Output:** "aabb"

**Explanation:**

* Choose `k = 3`, reverse the last 3 characters.
* Reverse `"bba"` to `"abb"`, so the resulting string is `"aabb"`, which is the lexicographically smallest string achievable.

**Example 3:**

**Input:** s = "zxy"

**Output:** "xzy"

**Explanation:**

* Choose `k = 2`, reverse the first 2 characters.
* Reverse `"zx"` to `"xz"`, so the resulting string is `"xzy"`, which is the lexicographically smallest string achievable.

**Constraints:**

* `1 <= n == s.length <= 1000`
* `s` consists of lowercase English letters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package g3701_3800.s3723_maximize_sum_of_squares_of_digits;

// #Medium #Math #Greedy #Biweekly_Contest_168
// #2025_10_29_Time_14_ms_(98.69%)_Space_46.36_MB_(47.20%)

public class Solution {
public String maxSumOfSquares(int places, int sum) {
String ans = "";
int nines = sum / 9;
if (places < nines) {
return ans;
} else if (places == nines) {
int remSum = sum - nines * 9;
if (remSum > 0) {
return ans;
}
ans = "9".repeat(nines);
} else {
int remSum = sum - nines * 9;
ans = "9".repeat(nines) + remSum;
int extra = places - ans.length();
if (extra > 0) {
ans = ans + ("0".repeat(extra));
}
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
3723\. Maximize Sum of Squares of Digits

Medium

You are given two **positive** integers `num` and `sum`.

A positive integer `n` is **good** if it satisfies both of the following:

* The number of digits in `n` is **exactly** `num`.
* The sum of digits in `n` is **exactly** `sum`.

The **score** of a **good** integer `n` is the sum of the squares of digits in `n`.

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.

**Example 1:**

**Input:** num = 2, sum = 3

**Output:** "30"

**Explanation:**

There are 3 good integers: 12, 21, and 30.

* The score of 12 is <code>1<sup>2</sup> + 2<sup>2</sup> = 5</code>.
* The score of 21 is <code>2<sup>2</sup> + 1<sup>2</sup> = 5</code>.
* The score of 30 is <code>3<sup>2</sup> + 0<sup>2</sup> = 9</code>.

The maximum score is 9, which is achieved by the good integer 30. Therefore, the answer is `"30"`.

**Example 2:**

**Input:** num = 2, sum = 17

**Output:** "98"

**Explanation:**

There are 2 good integers: 89 and 98.

* The score of 89 is <code>8<sup>2</sup> + 9<sup>2</sup> = 145</code>.
* The score of 98 is <code>9<sup>2</sup> + 8<sup>2</sup> = 145</code>.

The maximum score is 145. The maximum good integer that achieves this score is 98. Therefore, the answer is `"98"`.

**Example 3:**

**Input:** num = 1, sum = 10

**Output:** ""

**Explanation:**

There are no integers that have exactly 1 digit and whose digits sum to 10. Therefore, the answer is `""`.

**Constraints:**

* <code>1 <= num <= 2 * 10<sup>5</sup></code>
* <code>1 <= sum <= 2 * 10<sup>6</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package g3701_3800.s3724_minimum_operations_to_transform_array;

// #Medium #Array #Greedy #Biweekly_Contest_168
// #2025_10_29_Time_2_ms_(100.00%)_Space_61.71_MB_(16.98%)

public class Solution {
public long minOperations(int[] nums1, int[] nums2) {
int n = nums1.length;
int last = nums2[n];
long steps = 1;
long minDiffFromLast = Long.MAX_VALUE;
for (int i = 0; i < n; i++) {
int min = Math.min(nums1[i], nums2[i]);
int max = Math.max(nums1[i], nums2[i]);
steps += Math.abs(max - min);
if (minDiffFromLast > 0) {
if (min <= last && last <= max) {
minDiffFromLast = 0;
} else {
minDiffFromLast =
Math.min(
minDiffFromLast,
Math.min(Math.abs(min - last), Math.abs(max - last)));
}
}
}
return steps + minDiffFromLast;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
3724\. Minimum Operations to Transform Array

Medium

You are given two integer arrays `nums1` of length `n` and `nums2` of length `n + 1`.

You want to transform `nums1` into `nums2` using the **minimum** number of operations.

You may perform the following operations **any** number of times, each time choosing an index `i`:

* **Increase** `nums1[i]` by 1.
* **Decrease** `nums1[i]` by 1.
* **Append** `nums1[i]` to the **end** of the array.

Return the **minimum** number of operations required to transform `nums1` into `nums2`.

**Example 1:**

**Input:** nums1 = [2,8], nums2 = [1,7,3]

**Output:** 4

**Explanation:**

| Step | `i` | Operation | `nums1[i]` | Updated `nums1` |
|------|------|------------|-------------|----------------|
| 1 | 0 | Append | - | [2, 8, 2] |
| 2 | 0 | Decrement | Decreases to 1 | [1, 8, 2] |
| 3 | 1 | Decrement | Decreases to 7 | [1, 7, 2] |
| 4 | 2 | Increment | Increases to 3 | [1, 7, 3] |

Thus, after 4 operations `nums1` is transformed into `nums2`.

**Example 2:**

**Input:** nums1 = [1,3,6], nums2 = [2,4,5,3]

**Output:** 4

**Explanation:**

| Step | `i` | Operation | `nums1[i]` | Updated `nums1` |
|------|------|------------|-------------|----------------|
| 1 | 1 | Append | - | [1, 3, 6, 3] |
| 2 | 0 | Increment | Increases to 2 | [2, 3, 6, 3] |
| 3 | 1 | Increment | Increases to 4 | [2, 4, 6, 3] |
| 4 | 2 | Decrement | Decreases to 5 | [2, 4, 5, 3] |

Thus, after 4 operations `nums1` is transformed into `nums2`.

**Example 3:**

**Input:** nums1 = [2], nums2 = [3,4]

**Output:** 3

**Explanation:**

| Step | `i` | Operation | `nums1[i]` | Updated `nums1` |
|------|------|------------|-------------|----------------|
| 1 | 0 | Increment | Increases to 3 | [3] |
| 2 | 0 | Append | - | [3, 3] |
| 3 | 1 | Increment | Increases to 4 | [3, 4] |

Thus, after 3 operations `nums1` is transformed into `nums2`.

**Constraints:**

* <code>1 <= n == nums1.length <= 10<sup>5</sup></code>
* `nums2.length == n + 1`
* <code>1 <= nums1[i], nums2[i] <= 10<sup>5</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package g3701_3800.s3725_count_ways_to_choose_coprime_integers_from_rows;

// #Hard #Array #Dynamic_Programming #Math #Matrix #Number_Theory #Combinatorics
// #Biweekly_Contest_168 #2025_10_29_Time_31_ms_(94.07%)_Space_47.74_MB_(49.21%)

import java.util.HashMap;
import java.util.Map;

public class Solution {
private static final int MOD = 1_000_000_007;

public int countCoprime(int[][] mat) {
int m = mat.length;
int n = mat[0].length;
int maxVal = 0;
for (int[] ints : mat) {
for (int j = 0; j < n; j++) {
maxVal = Math.max(maxVal, ints[j]);
}
}
Map<Integer, Long> gcdWays = new HashMap<>();
for (int g = maxVal; g >= 1; g--) {
long ways = countWaysWithDivisor(mat, g, m, n);
if (ways > 0) {
for (int multiple = 2 * g; multiple <= maxVal; multiple += g) {
if (gcdWays.containsKey(multiple)) {
ways = (ways - gcdWays.get(multiple) + MOD) % MOD;
}
}
gcdWays.put(g, ways);
}
}
return gcdWays.getOrDefault(1, 0L).intValue();
}

private long countWaysWithDivisor(int[][] matrix, int divisor, int rows, int cols) {
long totalWays = 1;
for (int row = 0; row < rows; row++) {
int validChoices = 0;
for (int col = 0; col < cols; col++) {
if (matrix[row][col] % divisor == 0) {
validChoices++;
}
}
if (validChoices == 0) {
return 0;
}
totalWays = (totalWays * validChoices) % MOD;
}
return totalWays;
}
}
Loading