Skip to content

Commit 6a7942a

Browse files
committed
Added tasks 3536-3539
1 parent bce104a commit 6a7942a

File tree

12 files changed

+561
-0
lines changed

12 files changed

+561
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g3501_3600.s3536_maximum_product_of_two_digits
2+
3+
// #Easy #2025_05_04_Time_1_ms_(100.00%)_Space_40.93_MB_(100.00%)
4+
5+
class Solution {
6+
fun maxProduct(n: Int): Int {
7+
var n = n
8+
var m1 = n % 10
9+
n /= 10
10+
var m2 = n % 10
11+
n /= 10
12+
while (n > 0) {
13+
val a = n % 10
14+
if (a > m1) {
15+
if (m1 > m2) {
16+
m2 = m1
17+
}
18+
m1 = a
19+
} else {
20+
if (a > m2) {
21+
m2 = a
22+
}
23+
}
24+
n /= 10
25+
}
26+
return m1 * m2
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
3536\. Maximum Product of Two Digits
2+
3+
Easy
4+
5+
You are given a positive integer `n`.
6+
7+
Return the **maximum** product of any two digits in `n`.
8+
9+
**Note:** You may use the **same** digit twice if it appears more than once in `n`.
10+
11+
**Example 1:**
12+
13+
**Input:** n = 31
14+
15+
**Output:** 3
16+
17+
**Explanation:**
18+
19+
* The digits of `n` are `[3, 1]`.
20+
* The possible products of any two digits are: `3 * 1 = 3`.
21+
* The maximum product is 3.
22+
23+
**Example 2:**
24+
25+
**Input:** n = 22
26+
27+
**Output:** 4
28+
29+
**Explanation:**
30+
31+
* The digits of `n` are `[2, 2]`.
32+
* The possible products of any two digits are: `2 * 2 = 4`.
33+
* The maximum product is 4.
34+
35+
**Example 3:**
36+
37+
**Input:** n = 124
38+
39+
**Output:** 8
40+
41+
**Explanation:**
42+
43+
* The digits of `n` are `[1, 2, 4]`.
44+
* The possible products of any two digits are: `1 * 2 = 2`, `1 * 4 = 4`, `2 * 4 = 8`.
45+
* The maximum product is 8.
46+
47+
**Constraints:**
48+
49+
* <code>10 <= n <= 10<sup>9</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g3501_3600.s3537_fill_a_special_grid
2+
3+
// #Medium #2025_05_04_Time_2_ms_(100.00%)_Space_88.71_MB_(61.54%)
4+
5+
import kotlin.math.pow
6+
7+
class Solution {
8+
fun specialGrid(n: Int): Array<IntArray> {
9+
if (n == 0) {
10+
return arrayOf<IntArray>(intArrayOf(0))
11+
}
12+
val len = 2.0.pow(n.toDouble()).toInt()
13+
val ans = Array<IntArray>(len) { IntArray(len) }
14+
val num = intArrayOf(2.0.pow(2.0 * n).toInt() - 1)
15+
backtrack(ans, len, len, 0, 0, num)
16+
return ans
17+
}
18+
19+
private fun backtrack(ans: Array<IntArray>, m: Int, n: Int, x: Int, y: Int, num: IntArray) {
20+
if (m == 2 && n == 2) {
21+
ans[x][y] = num[0]
22+
ans[x + 1][y] = num[0] - 1
23+
ans[x + 1][y + 1] = num[0] - 2
24+
ans[x][y + 1] = num[0] - 3
25+
num[0] -= 4
26+
return
27+
}
28+
backtrack(ans, m / 2, n / 2, x, y, num)
29+
backtrack(ans, m / 2, n / 2, x + m / 2, y, num)
30+
backtrack(ans, m / 2, n / 2, x + m / 2, y + n / 2, num)
31+
backtrack(ans, m / 2, n / 2, x, y + n / 2, num)
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
3537\. Fill a Special Grid
2+
3+
Medium
4+
5+
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:
6+
7+
* All numbers in the top-right quadrant are smaller than those in the bottom-right quadrant.
8+
* All numbers in the bottom-right quadrant are smaller than those in the bottom-left quadrant.
9+
* All numbers in the bottom-left quadrant are smaller than those in the top-left quadrant.
10+
* Each of its quadrants is also a special grid.
11+
12+
Return the **special** <code>2<sup>n</sup> x 2<sup>n</sup></code> grid.
13+
14+
**Note**: Any 1x1 grid is special.
15+
16+
**Example 1:**
17+
18+
**Input:** n = 0
19+
20+
**Output:** [[0]]
21+
22+
**Explanation:**
23+
24+
The only number that can be placed is 0, and there is only one possible position in the grid.
25+
26+
**Example 2:**
27+
28+
**Input:** n = 1
29+
30+
**Output:** [[3,0],[2,1]]
31+
32+
**Explanation:**
33+
34+
The numbers in each quadrant are:
35+
36+
* Top-right: 0
37+
* Bottom-right: 1
38+
* Bottom-left: 2
39+
* Top-left: 3
40+
41+
Since `0 < 1 < 2 < 3`, this satisfies the given constraints.
42+
43+
**Example 3:**
44+
45+
**Input:** n = 2
46+
47+
**Output:** [[15,12,3,0],[14,13,2,1],[11,8,7,4],[10,9,6,5]]
48+
49+
**Explanation:**
50+
51+
![](https://assets.leetcode.com/uploads/2025/03/05/4123example3p1drawio.png)
52+
53+
The numbers in each quadrant are:
54+
55+
* Top-right: 3, 0, 2, 1
56+
* Bottom-right: 7, 4, 6, 5
57+
* Bottom-left: 11, 8, 10, 9
58+
* Top-left: 15, 12, 14, 13
59+
* `max(3, 0, 2, 1) < min(7, 4, 6, 5)`
60+
* `max(7, 4, 6, 5) < min(11, 8, 10, 9)`
61+
* `max(11, 8, 10, 9) < min(15, 12, 14, 13)`
62+
63+
This satisfies the first three requirements. Additionally, each quadrant is also a special grid. Thus, this is a special grid.
64+
65+
**Constraints:**
66+
67+
* `0 <= n <= 10`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package g3501_3600.s3538_merge_operations_for_minimum_travel_time
2+
3+
// #Hard #2025_05_04_Time_10_ms_(100.00%)_Space_46.96_MB_(100.00%)
4+
5+
import kotlin.math.min
6+
7+
@Suppress("unused")
8+
class Solution {
9+
fun minTravelTime(l: Int, n: Int, k: Int, position: IntArray, time: IntArray): Int {
10+
val dp = Array<Array<IntArray>>(n) { Array<IntArray>(k + 1) { IntArray(k + 1) } }
11+
for (i in 0..<n) {
12+
for (j in 0..k) {
13+
for (m in 0..k) {
14+
dp[i][j][m] = Int.Companion.MAX_VALUE
15+
}
16+
}
17+
}
18+
dp[0][0][0] = 0
19+
for (i in 0..<n - 1) {
20+
var currTime = 0
21+
var curr = 0
22+
while (curr <= k && curr <= i) {
23+
currTime += time[i - curr]
24+
for (used in 0..k) {
25+
if (dp[i][curr][used] == Int.Companion.MAX_VALUE) {
26+
continue
27+
}
28+
var next = 0
29+
while (next <= k - used && next <= n - i - 2) {
30+
val nextI = i + next + 1
31+
dp[nextI][next][next + used] = min(
32+
dp[nextI][next][next + used],
33+
dp[i][curr][used] +
34+
(position[nextI] - position[i]) * currTime,
35+
)
36+
next++
37+
}
38+
}
39+
curr++
40+
}
41+
}
42+
var ans = Int.Companion.MAX_VALUE
43+
for (curr in 0..k) {
44+
ans = min(ans, dp[n - 1][curr][k])
45+
}
46+
return ans
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
3538\. Merge Operations for Minimum Travel Time
2+
3+
Hard
4+
5+
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`.
6+
7+
The array `position` lists the positions (in km) of signs in **strictly** increasing order (with `position[0] = 0` and `position[n - 1] = l`).
8+
9+
Each `time[i]` represents the time (in minutes) required to travel 1 km between `position[i]` and `position[i + 1]`.
10+
11+
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:
12+
13+
* Update the sign at index `i + 1` so that its time becomes `time[i] + time[i + 1]`.
14+
* Remove the sign at index `i`.
15+
16+
Return the **minimum** **total** **travel time** (in minutes) to travel from 0 to `l` after **exactly** `k` merges.
17+
18+
**Example 1:**
19+
20+
**Input:** l = 10, n = 4, k = 1, position = [0,3,8,10], time = [5,8,3,6]
21+
22+
**Output:** 62
23+
24+
**Explanation:**
25+
26+
* 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`.
27+
28+
* After the merge:
29+
* `position` array: `[0, 8, 10]`
30+
* `time` array: `[5, 11, 6]`
31+
32+
| Segment | Distance (km) | Time per km (min) | Segment Travel Time (min) |
33+
|-----------|---------------|-------------------|----------------------------|
34+
| 0 → 8 | 8 | 5 | 8 × 5 = 40 |
35+
| 8 → 10 | 2 | 11 | 2 × 11 = 22 |
36+
37+
38+
* Total Travel Time: `40 + 22 = 62`, which is the minimum possible time after exactly 1 merge.
39+
40+
**Example 2:**
41+
42+
**Input:** l = 5, n = 5, k = 1, position = [0,1,2,3,5], time = [8,3,9,3,3]
43+
44+
**Output:** 34
45+
46+
**Explanation:**
47+
48+
* 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`.
49+
* After the merge:
50+
* `position` array: `[0, 2, 3, 5]`
51+
* `time` array: `[8, 12, 3, 3]`
52+
53+
| Segment | Distance (km) | Time per km (min) | Segment Travel Time (min) |
54+
|-----------|---------------|-------------------|----------------------------|
55+
| 0 → 2 | 2 | 8 | 2 × 8 = 16 |
56+
| 2 → 3 | 1 | 12 | 1 × 12 = 12 |
57+
| 3 → 5 | 2 | 3 | 2 × 3 = 6 |
58+
59+
* Total Travel Time: `16 + 12 + 6 = 34`**,** which is the minimum possible time after exactly 1 merge.
60+
61+
**Constraints:**
62+
63+
* <code>1 <= l <= 10<sup>5</sup></code>
64+
* `2 <= n <= min(l + 1, 50)`
65+
* `0 <= k <= min(n - 2, 10)`
66+
* `position.length == n`
67+
* `position[0] = 0` and `position[n - 1] = l`
68+
* `position` is sorted in strictly increasing order.
69+
* `time.length == n`
70+
* `1 <= time[i] <= 100`
71+
* `1 <= sum(time) <= 100`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package g3501_3600.s3539_find_sum_of_array_product_of_magical_sequences
2+
3+
// #Hard #2025_05_04_Time_58_ms_(_%)_Space_49.73_MB_(_%)
4+
5+
class Solution {
6+
fun magicalSum(m: Int, k: Int, nums: IntArray): Int {
7+
val n = nums.size
8+
val pow = Array<LongArray>(n) { LongArray(m + 1) }
9+
for (j in 0..<n) {
10+
pow[j][0] = 1L
11+
for (c in 1..m) {
12+
pow[j][c] = pow[j][c - 1] * nums[j] % MOD
13+
}
14+
}
15+
var dp = Array<Array<LongArray>>(m + 1) { Array<LongArray>(k + 1) { LongArray(m + 1) } }
16+
var next = Array<Array<LongArray>>(m + 1) { Array<LongArray>(k + 1) { LongArray(m + 1) } }
17+
dp[0][0][0] = 1L
18+
for (i in 0..<n) {
19+
for (t in 0..m) {
20+
for (o in 0..k) {
21+
next[t][o].fill(0L)
22+
}
23+
}
24+
for (t in 0..m) {
25+
for (o in 0..k) {
26+
for (c in 0..m) {
27+
if (dp[t][o][c] == 0L) {
28+
continue
29+
}
30+
for (cc in 0..m - t) {
31+
val total = c + cc
32+
if (o + (total and 1) > k) {
33+
continue
34+
}
35+
next[t + cc][o + (total and 1)][total ushr 1] =
36+
(
37+
(
38+
next[t + cc][o + (total and 1)][total ushr 1] +
39+
dp[t][o][c] *
40+
C[m - t][cc] %
41+
MOD
42+
* pow[i][cc] %
43+
MOD
44+
) %
45+
MOD
46+
)
47+
}
48+
}
49+
}
50+
}
51+
val tmp = dp
52+
dp = next
53+
next = tmp
54+
}
55+
var res: Long = 0
56+
for (o in 0..k) {
57+
for (c in 0..m) {
58+
if (o + P[c] == k) {
59+
res = (res + dp[m][o][c]) % MOD
60+
}
61+
}
62+
}
63+
return res.toInt()
64+
}
65+
66+
companion object {
67+
private const val MOD = 1000000007
68+
private val C: Array<IntArray> = precomputeBinom(31)
69+
private val P: IntArray = precomputePop(31)
70+
71+
private fun precomputeBinom(max: Int): Array<IntArray> {
72+
val res = Array<IntArray>(max) { IntArray(max) }
73+
for (i in 0..<max) {
74+
res[i][i] = 1
75+
res[i][0] = res[i][i]
76+
for (j in 1..<i) {
77+
res[i][j] = (res[i - 1][j - 1] + res[i - 1][j]) % MOD
78+
}
79+
}
80+
return res
81+
}
82+
83+
private fun precomputePop(max: Int): IntArray {
84+
val res = IntArray(max)
85+
for (i in 1..<max) {
86+
res[i] = res[i shr 1] + (i and 1)
87+
}
88+
return res
89+
}
90+
}
91+
}

0 commit comments

Comments
 (0)