Skip to content

Added tasks 3536-3539 #811

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 6, 2025
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,28 @@
package g3501_3600.s3536_maximum_product_of_two_digits

// #Easy #Math #Sorting #2025_05_04_Time_1_ms_(100.00%)_Space_40.93_MB_(100.00%)

class Solution {
fun maxProduct(n: Int): Int {
var n = n
var m1 = n % 10
n /= 10
var m2 = n % 10
n /= 10
while (n > 0) {
val a = n % 10
if (a > m1) {
if (m1 > m2) {
m2 = m1
}
m1 = a
} else {
if (a > m2) {
m2 = a
}
}
n /= 10
}
return m1 * m2
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
3536\. Maximum Product of Two Digits

Easy

You are given a positive integer `n`.

Return the **maximum** product of any two digits in `n`.

**Note:** You may use the **same** digit twice if it appears more than once in `n`.

**Example 1:**

**Input:** n = 31

**Output:** 3

**Explanation:**

* The digits of `n` are `[3, 1]`.
* The possible products of any two digits are: `3 * 1 = 3`.
* The maximum product is 3.

**Example 2:**

**Input:** n = 22

**Output:** 4

**Explanation:**

* The digits of `n` are `[2, 2]`.
* The possible products of any two digits are: `2 * 2 = 4`.
* The maximum product is 4.

**Example 3:**

**Input:** n = 124

**Output:** 8

**Explanation:**

* The digits of `n` are `[1, 2, 4]`.
* The possible products of any two digits are: `1 * 2 = 2`, `1 * 4 = 4`, `2 * 4 = 8`.
* The maximum product is 8.

**Constraints:**

* <code>10 <= n <= 10<sup>9</sup></code>
34 changes: 34 additions & 0 deletions src/main/kotlin/g3501_3600/s3537_fill_a_special_grid/Solution.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package g3501_3600.s3537_fill_a_special_grid

// #Medium #Array #Matrix #Divide_and_Conquer
// #2025_05_04_Time_2_ms_(100.00%)_Space_88.71_MB_(61.54%)

import kotlin.math.pow

class Solution {
fun specialGrid(n: Int): Array<IntArray> {
if (n == 0) {
return arrayOf<IntArray>(intArrayOf(0))
}
val len = 2.0.pow(n.toDouble()).toInt()
val ans = Array<IntArray>(len) { IntArray(len) }
val num = intArrayOf(2.0.pow(2.0 * n).toInt() - 1)
backtrack(ans, len, len, 0, 0, num)
return ans
}

private fun backtrack(ans: Array<IntArray>, m: Int, n: Int, x: Int, y: Int, num: IntArray) {
if (m == 2 && n == 2) {
ans[x][y] = num[0]
ans[x + 1][y] = num[0] - 1
ans[x + 1][y + 1] = num[0] - 2
ans[x][y + 1] = num[0] - 3
num[0] -= 4
return
}
backtrack(ans, m / 2, n / 2, x, y, num)
backtrack(ans, m / 2, n / 2, x + m / 2, y, num)
backtrack(ans, m / 2, n / 2, x + m / 2, y + n / 2, num)
backtrack(ans, m / 2, n / 2, x, y + n / 2, num)
}
}
67 changes: 67 additions & 0 deletions src/main/kotlin/g3501_3600/s3537_fill_a_special_grid/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
3537\. Fill a Special Grid

Medium

You are given a non-negative integer `n` representing a <code>2<sup>n</sup> x 2<sup>n</sup></code> grid. You must fill the grid with integers from 0 to <code>2<sup>2n</sup> - 1</code> to make it **special**. A grid is **special** if it satisfies **all** the following conditions:

* All numbers in the top-right quadrant are smaller than those in the bottom-right quadrant.
* All numbers in the bottom-right quadrant are smaller than those in the bottom-left quadrant.
* All numbers in the bottom-left quadrant are smaller than those in the top-left quadrant.
* Each of its quadrants is also a special grid.

Return the **special** <code>2<sup>n</sup> x 2<sup>n</sup></code> grid.

**Note**: Any 1x1 grid is special.

**Example 1:**

**Input:** n = 0

**Output:** [[0]]

**Explanation:**

The only number that can be placed is 0, and there is only one possible position in the grid.

**Example 2:**

**Input:** n = 1

**Output:** [[3,0],[2,1]]

**Explanation:**

The numbers in each quadrant are:

* Top-right: 0
* Bottom-right: 1
* Bottom-left: 2
* Top-left: 3

Since `0 < 1 < 2 < 3`, this satisfies the given constraints.

**Example 3:**

**Input:** n = 2

**Output:** [[15,12,3,0],[14,13,2,1],[11,8,7,4],[10,9,6,5]]

**Explanation:**

![](https://assets.leetcode.com/uploads/2025/03/05/4123example3p1drawio.png)

The numbers in each quadrant are:

* Top-right: 3, 0, 2, 1
* Bottom-right: 7, 4, 6, 5
* Bottom-left: 11, 8, 10, 9
* Top-left: 15, 12, 14, 13
* `max(3, 0, 2, 1) < min(7, 4, 6, 5)`
* `max(7, 4, 6, 5) < min(11, 8, 10, 9)`
* `max(11, 8, 10, 9) < min(15, 12, 14, 13)`

This satisfies the first three requirements. Additionally, each quadrant is also a special grid. Thus, this is a special grid.

**Constraints:**

* `0 <= n <= 10`
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package g3501_3600.s3538_merge_operations_for_minimum_travel_time

// #Hard #Array #Dynamic_Programming #Prefix_Sum
// #2025_05_04_Time_10_ms_(100.00%)_Space_46.96_MB_(100.00%)

import kotlin.math.min

@Suppress("unused")
class Solution {
fun minTravelTime(l: Int, n: Int, k: Int, position: IntArray, time: IntArray): Int {
val dp = Array<Array<IntArray>>(n) { Array<IntArray>(k + 1) { IntArray(k + 1) } }
for (i in 0..<n) {
for (j in 0..k) {
for (m in 0..k) {
dp[i][j][m] = Int.Companion.MAX_VALUE
}
}
}
dp[0][0][0] = 0
for (i in 0..<n - 1) {
var currTime = 0
var curr = 0
while (curr <= k && curr <= i) {
currTime += time[i - curr]
for (used in 0..k) {
if (dp[i][curr][used] == Int.Companion.MAX_VALUE) {
continue
}
var next = 0
while (next <= k - used && next <= n - i - 2) {
val nextI = i + next + 1
dp[nextI][next][next + used] = min(
dp[nextI][next][next + used],
dp[i][curr][used] +
(position[nextI] - position[i]) * currTime,
)
next++
}
}
curr++
}
}
var ans = Int.Companion.MAX_VALUE
for (curr in 0..k) {
ans = min(ans, dp[n - 1][curr][k])
}
return ans
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
3538\. Merge Operations for Minimum Travel Time

Hard

You are given a straight road of length `l` km, an integer `n`, an integer `k`**,** and **two** integer arrays, `position` and `time`, each of length `n`.

The array `position` lists the positions (in km) of signs in **strictly** increasing order (with `position[0] = 0` and `position[n - 1] = l`).

Each `time[i]` represents the time (in minutes) required to travel 1 km between `position[i]` and `position[i + 1]`.

You **must** perform **exactly** `k` merge operations. In one merge, you can choose any **two** adjacent signs at indices `i` and `i + 1` (with `i > 0` and `i + 1 < n`) and:

* Update the sign at index `i + 1` so that its time becomes `time[i] + time[i + 1]`.
* Remove the sign at index `i`.

Return the **minimum** **total** **travel time** (in minutes) to travel from 0 to `l` after **exactly** `k` merges.

**Example 1:**

**Input:** l = 10, n = 4, k = 1, position = [0,3,8,10], time = [5,8,3,6]

**Output:** 62

**Explanation:**

* Merge the signs at indices 1 and 2. Remove the sign at index 1, and change the time at index 2 to `8 + 3 = 11`.

* After the merge:
* `position` array: `[0, 8, 10]`
* `time` array: `[5, 11, 6]`

| Segment | Distance (km) | Time per km (min) | Segment Travel Time (min) |
|-----------|---------------|-------------------|----------------------------|
| 0 → 8 | 8 | 5 | 8 × 5 = 40 |
| 8 → 10 | 2 | 11 | 2 × 11 = 22 |


* Total Travel Time: `40 + 22 = 62`, which is the minimum possible time after exactly 1 merge.

**Example 2:**

**Input:** l = 5, n = 5, k = 1, position = [0,1,2,3,5], time = [8,3,9,3,3]

**Output:** 34

**Explanation:**

* Merge the signs at indices 1 and 2. Remove the sign at index 1, and change the time at index 2 to `3 + 9 = 12`.
* After the merge:
* `position` array: `[0, 2, 3, 5]`
* `time` array: `[8, 12, 3, 3]`

| Segment | Distance (km) | Time per km (min) | Segment Travel Time (min) |
|-----------|---------------|-------------------|----------------------------|
| 0 → 2 | 2 | 8 | 2 × 8 = 16 |
| 2 → 3 | 1 | 12 | 1 × 12 = 12 |
| 3 → 5 | 2 | 3 | 2 × 3 = 6 |

* Total Travel Time: `16 + 12 + 6 = 34`**,** which is the minimum possible time after exactly 1 merge.

**Constraints:**

* <code>1 <= l <= 10<sup>5</sup></code>
* `2 <= n <= min(l + 1, 50)`
* `0 <= k <= min(n - 2, 10)`
* `position.length == n`
* `position[0] = 0` and `position[n - 1] = l`
* `position` is sorted in strictly increasing order.
* `time.length == n`
* `1 <= time[i] <= 100`
* `1 <= sum(time) <= 100`
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package g3501_3600.s3539_find_sum_of_array_product_of_magical_sequences

// #Hard #Array #Dynamic_Programming #Math #Bit_Manipulation #Bitmask #Combinatorics
// #2025_05_06_Time_60_ms_(100.00%)_Space_48.98_MB_(100.00%)

class Solution {
fun magicalSum(m: Int, k: Int, nums: IntArray): Int {
val n = nums.size
val pow = Array<LongArray>(n) { LongArray(m + 1) }
for (j in 0..<n) {
pow[j][0] = 1L
for (c in 1..m) {
pow[j][c] = pow[j][c - 1] * nums[j] % MOD
}
}
var dp = Array<Array<LongArray>>(m + 1) { Array<LongArray>(k + 1) { LongArray(m + 1) } }
var next = Array<Array<LongArray>>(m + 1) { Array<LongArray>(k + 1) { LongArray(m + 1) } }
dp[0][0][0] = 1L
for (i in 0..<n) {
for (t in 0..m) {
for (o in 0..k) {
next[t][o].fill(0L)
}
}
for (t in 0..m) {
for (o in 0..k) {
for (c in 0..m) {
if (dp[t][o][c] == 0L) {
continue
}
for (cc in 0..m - t) {
val total = c + cc
if (o + (total and 1) > k) {
continue
}
next[t + cc][o + (total and 1)][total ushr 1] =
(
(
next[t + cc][o + (total and 1)][total ushr 1] +
dp[t][o][c] *
C[m - t][cc] %
MOD
* pow[i][cc] %
MOD
) %
MOD
)
}
}
}
}
val tmp = dp
dp = next
next = tmp
}
var res: Long = 0
for (o in 0..k) {
for (c in 0..m) {
if (o + P[c] == k) {
res = (res + dp[m][o][c]) % MOD
}
}
}
return res.toInt()
}

companion object {
private const val MOD = 1000000007
private val C: Array<IntArray> = precomputeBinom(31)
private val P: IntArray = precomputePop(31)

private fun precomputeBinom(max: Int): Array<IntArray> {
val res = Array<IntArray>(max) { IntArray(max) }
for (i in 0..<max) {
res[i][i] = 1
res[i][0] = res[i][i]
for (j in 1..<i) {
res[i][j] = (res[i - 1][j - 1] + res[i - 1][j]) % MOD
}
}
return res
}

private fun precomputePop(max: Int): IntArray {
val res = IntArray(max)
for (i in 1..<max) {
res[i] = res[i shr 1] + (i and 1)
}
return res
}
}
}
Loading