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_28_Time_8_ms_(100.00%)_Space_45.74_MB_(100.00%)

class Solution {
fun lexSmallest(s: String): String {
val n = s.length
val arr = s.toCharArray()
val best = arr.clone()
// Check all reverse first k operations
for (k in 1..n) {
if (isBetterReverseFirstK(arr, k, best)) {
updateBestReverseFirstK(arr, k, best)
}
}
// Check all reverse last k operations
for (k in 1..n) {
if (isBetterReverseLastK(arr, k, best)) {
updateBestReverseLastK(arr, k, best)
}
}
return String(best)
}

private fun isBetterReverseFirstK(arr: CharArray, k: Int, best: CharArray): Boolean {
for (i in arr.indices) {
val currentChar = if (i < k) arr[k - 1 - i] else arr[i]
if (currentChar < best[i]) {
return true
}
if (currentChar > best[i]) {
return false
}
}
return false
}

private fun isBetterReverseLastK(arr: CharArray, k: Int, best: CharArray): Boolean {
val n = arr.size
for (i in 0..<n) {
val currentChar = if (i < n - k) arr[i] else arr[n - 1 - (i - (n - k))]
if (currentChar < best[i]) {
return true
}
if (currentChar > best[i]) {
return false
}
}
return false
}

private fun updateBestReverseFirstK(arr: CharArray, k: Int, best: CharArray) {
for (i in 0..<k) {
best[i] = arr[k - 1 - i]
}
if (arr.size - k >= 0) {
System.arraycopy(arr, k, best, k, arr.size - k)
}
}

private fun updateBestReverseLastK(arr: CharArray, k: Int, best: CharArray) {
val n = arr.size
if (n - k >= 0) {
System.arraycopy(arr, 0, best, 0, n - k)
}
for (i in 0..<k) {
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_28_Time_16_ms_(94.44%)_Space_47.62_MB_(61.11%)

class Solution {
fun maxSumOfSquares(places: Int, sum: Int): String {
var ans = ""
val nines = sum / 9
if (places < nines) {
return ans
} else if (places == nines) {
val remSum = sum - nines * 9
if (remSum > 0) {
return ans
}
ans = "9".repeat(nines)
} else {
val remSum = sum - nines * 9
ans = "9".repeat(nines) + remSum
val 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,33 @@
package g3701_3800.s3724_minimum_operations_to_transform_array

// #Medium #Array #Greedy #Biweekly_Contest_168
// #2025_10_28_Time_10_ms_(83.33%)_Space_66.99_MB_(100.00%)

import kotlin.math.abs
import kotlin.math.max
import kotlin.math.min

class Solution {
fun minOperations(nums1: IntArray, nums2: IntArray): Long {
val n = nums1.size
val last = nums2[n]
var steps: Long = 1
var minDiffFromLast = Long.MAX_VALUE
for (i in 0..<n) {
val min = min(nums1[i], nums2[i])
val max = max(nums1[i], nums2[i])
steps += abs(max - min).toLong()
if (minDiffFromLast > 0) {
if (min <= last && last <= max) {
minDiffFromLast = 0
} else {
minDiffFromLast = min(
minDiffFromLast,
min(abs(min - last), abs(max - last)).toLong(),
)
}
}
}
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,55 @@
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_28_Time_29_ms_(100.00%)_Space_51.80_MB_(100.00%)

import kotlin.math.max

class Solution {
fun countCoprime(mat: Array<IntArray>): Int {
val m = mat.size
val n = mat[0].size
var maxVal = 0
for (ints in mat) {
for (j in 0..<n) {
maxVal = max(maxVal, ints[j])
}
}
val gcdWays: MutableMap<Int, Long> = HashMap()
for (g in maxVal downTo 1) {
var ways = countWaysWithDivisor(mat, g, m, n)
if (ways > 0) {
var multiple = 2 * g
while (multiple <= maxVal) {
if (gcdWays.containsKey(multiple)) {
ways = (ways - gcdWays[multiple]!! + MOD) % MOD
}
multiple += g
}
gcdWays[g] = ways
}
}
return gcdWays.getOrDefault(1, 0L).toInt()
}

private fun countWaysWithDivisor(matrix: Array<IntArray>, divisor: Int, rows: Int, cols: Int): Long {
var totalWays: Long = 1
for (row in 0..<rows) {
var validChoices = 0
for (col in 0..<cols) {
if (matrix[row][col] % divisor == 0) {
validChoices++
}
}
if (validChoices == 0) {
return 0
}
totalWays = (totalWays * validChoices) % MOD
}
return totalWays
}

companion object {
private const val MOD = 1000000007
}
}
Loading