Skip to content

Added tasks 3536-3539 #1974

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 8 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,27 @@
package g3501_3600.s3536_maximum_product_of_two_digits;

// #Easy #Math #Sorting #2025_05_06_Time_1_ms_(95.82%)_Space_40.95_MB_(91.71%)

public class Solution {
public int maxProduct(int n) {
int m1 = n % 10;
n /= 10;
int m2 = n % 10;
n /= 10;
while (n > 0) {
int 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>
32 changes: 32 additions & 0 deletions src/main/java/g3501_3600/s3537_fill_a_special_grid/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package g3501_3600.s3537_fill_a_special_grid;

// #Medium #Array #Matrix #Divide_and_Conquer
// #2025_05_06_Time_2_ms_(100.00%)_Space_87.14_MB_(16.42%)

public class Solution {
public int[][] specialGrid(int n) {
if (n == 0) {
return new int[][] {{0}};
}
int len = (int) Math.pow(2, n);
int[][] ans = new int[len][len];
int[] num = new int[] {(int) Math.pow(2, 2D * n) - 1};
backtrack(ans, len, len, 0, 0, num);
return ans;
}

private void backtrack(int[][] ans, int m, int n, int x, int y, int[] num) {
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/java/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,43 @@
package g3501_3600.s3538_merge_operations_for_minimum_travel_time;

// #Hard #Array #Dynamic_Programming #Prefix_Sum
// #2025_05_06_Time_7_ms_(99.32%)_Space_45.14_MB_(87.16%)

@SuppressWarnings({"unused", "java:S1172"})
public class Solution {
public int minTravelTime(int l, int n, int k, int[] position, int[] time) {
int[][][] dp = new int[n][k + 1][k + 1];
for (int i = 0; i < n; i++) {
for (int j = 0; j <= k; j++) {
for (int m = 0; m <= k; m++) {
dp[i][j][m] = Integer.MAX_VALUE;
}
}
}
dp[0][0][0] = 0;
for (int i = 0; i < n - 1; i++) {
int currTime = 0;
for (int curr = 0; curr <= k && curr <= i; curr++) {
currTime += time[i - curr];
for (int used = 0; used <= k; used++) {
if (dp[i][curr][used] == Integer.MAX_VALUE) {
continue;
}
for (int next = 0; next <= k - used && next <= n - i - 2; next++) {
int nextI = i + next + 1;
dp[nextI][next][next + used] =
Math.min(
dp[nextI][next][next + used],
dp[i][curr][used]
+ (position[nextI] - position[i]) * currTime);
}
}
}
}
int ans = Integer.MAX_VALUE;
for (int curr = 0; curr <= k; curr++) {
ans = Math.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,87 @@
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_39_ms_(95.71%)_Space_44.58_MB_(98.57%)

import java.util.Arrays;

public class Solution {
private static final int MOD = 1_000_000_007;
private static final int[][] C = precomputeBinom(31);
private static final int[] P = precomputePop(31);

public int magicalSum(int m, int k, int[] nums) {
int n = nums.length;
long[][] pow = new long[n][m + 1];
for (int j = 0; j < n; j++) {
pow[j][0] = 1L;
for (int c = 1; c <= m; c++) {
pow[j][c] = pow[j][c - 1] * nums[j] % MOD;
}
}
long[][][] dp = new long[m + 1][k + 1][m + 1];
long[][][] next = new long[m + 1][k + 1][m + 1];
dp[0][0][0] = 1L;
for (int i = 0; i < n; i++) {
for (int t = 0; t <= m; t++) {
for (int o = 0; o <= k; o++) {
Arrays.fill(next[t][o], 0L);
}
}
for (int t = 0; t <= m; t++) {
for (int o = 0; o <= k; o++) {
for (int c = 0; c <= m; c++) {
if (dp[t][o][c] == 0) {
continue;
}
for (int cc = 0; cc <= m - t; cc++) {
int total = c + cc;
if (o + (total & 1) > k) {
continue;
}
next[t + cc][o + (total & 1)][total >>> 1] =
(next[t + cc][o + (total & 1)][total >>> 1]
+ dp[t][o][c]
* C[m - t][cc]
% MOD
* pow[i][cc]
% MOD)
% MOD;
}
}
}
}
long[][][] tmp = dp;
dp = next;
next = tmp;
}
long res = 0;
for (int o = 0; o <= k; o++) {
for (int c = 0; c <= m; c++) {
if (o + P[c] == k) {
res = (res + dp[m][o][c]) % MOD;
}
}
}
return (int) res;
}

private static int[][] precomputeBinom(int max) {
int[][] res = new int[max][max];
for (int i = 0; i < max; i++) {
res[i][0] = res[i][i] = 1;
for (int j = 1; j < i; j++) {
res[i][j] = (res[i - 1][j - 1] + res[i - 1][j]) % MOD;
}
}
return res;
}

private static int[] precomputePop(int max) {
int[] res = new int[max];
for (int i = 1; i < max; i++) {
res[i] = res[i >> 1] + (i & 1);
}
return res;
}
}
Loading