Skip to content

Commit 8546ff7

Browse files
committed
Added tasks 3402-3405
1 parent 6199a59 commit 8546ff7

File tree

12 files changed

+385
-0
lines changed

12 files changed

+385
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3401_3500.s3402_minimum_operations_to_make_columns_strictly_increasing;
2+
3+
// #Easy #2024_12_29_Time_1_(100.00%)_Space_44.99_(100.00%)
4+
5+
public class Solution {
6+
public int minimumOperations(int[][] grid) {
7+
int ans = 0;
8+
for (int c = 0; c < grid[0].length; ++c) {
9+
for (int r = 1; r < grid.length; ++r) {
10+
if (grid[r][c] <= grid[r - 1][c]) {
11+
ans += grid[r - 1][c] + 1 - grid[r][c];
12+
grid[r][c] = grid[r - 1][c] + 1;
13+
}
14+
}
15+
}
16+
return ans;
17+
}
18+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
3402\. Minimum Operations to Make Columns Strictly Increasing
2+
3+
Easy
4+
5+
You are given a `m x n` matrix `grid` consisting of **non-negative** integers.
6+
7+
In one operation, you can increment the value of any `grid[i][j]` by 1.
8+
9+
Return the **minimum** number of operations needed to make all columns of `grid` **strictly increasing**.
10+
11+
**Example 1:**
12+
13+
**Input:** grid = [[3,2],[1,3],[3,4],[0,1]]
14+
15+
**Output:** 15
16+
17+
**Explanation:**
18+
19+
* To make the <code>0<sup>th</sup></code> column strictly increasing, we can apply 3 operations on `grid[1][0]`, 2 operations on `grid[2][0]`, and 6 operations on `grid[3][0]`.
20+
* To make the <code>1<sup>st</sup></code> column strictly increasing, we can apply 4 operations on `grid[3][1]`.
21+
22+
![](https://assets.leetcode.com/uploads/2024/11/10/firstexample.png)
23+
24+
**Example 2:**
25+
26+
**Input:** grid = [[3,2,1],[2,1,0],[1,2,3]]
27+
28+
**Output:** 12
29+
30+
**Explanation:**
31+
32+
* To make the <code>0<sup>th</sup></code> column strictly increasing, we can apply 2 operations on `grid[1][0]`, and 4 operations on `grid[2][0]`.
33+
* To make the <code>1<sup>st</sup></code> column strictly increasing, we can apply 2 operations on `grid[1][1]`, and 2 operations on `grid[2][1]`.
34+
* To make the <code>2<sup>nd</sup></code> column strictly increasing, we can apply 2 operations on `grid[1][2]`.
35+
36+
![](https://assets.leetcode.com/uploads/2024/11/10/secondexample.png)
37+
38+
**Constraints:**
39+
40+
* `m == grid.length`
41+
* `n == grid[i].length`
42+
* `1 <= m, n <= 50`
43+
* `0 <= grid[i][j] < 2500`
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g3401_3500.s3403_find_the_lexicographically_largest_string_from_the_box_i;
2+
3+
// #Medium #2024_12_29_Time_5_(100.00%)_Space_45.20_(100.00%)
4+
5+
public class Solution {
6+
public String answerString(String word, int numFriends) {
7+
if (numFriends == 1) {
8+
return word;
9+
}
10+
int n = word.length();
11+
int maxlen = n - numFriends + 1;
12+
char maxchar = word.charAt(0);
13+
String res = "";
14+
for (int i = 0; i < n; i++) {
15+
if (word.charAt(i) >= maxchar) {
16+
String curr = word.substring(i, Math.min(i + maxlen, n));
17+
if (curr.compareTo(res) > 0) {
18+
res = curr;
19+
}
20+
maxchar = word.charAt(i);
21+
}
22+
}
23+
return res;
24+
}
25+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
3403\. Find the Lexicographically Largest String From the Box I
2+
3+
Medium
4+
5+
You are given a string `word`, and an integer `numFriends`.
6+
7+
Alice is organizing a game for her `numFriends` friends. There are multiple rounds in the game, where in each round:
8+
9+
* `word` is split into `numFriends` **non-empty** strings, such that no previous round has had the **exact** same split.
10+
* All the split words are put into a box.
11+
12+
Find the **lexicographically largest** string from the box after all the rounds are finished.
13+
14+
A string `a` is **lexicographically smaller** than a string `b` if in the first position where `a` and `b` differ, string `a` has a letter that appears earlier in the alphabet than the corresponding letter in `b`.
15+
If the first `min(a.length, b.length)` characters do not differ, then the shorter string is the lexicographically smaller one.
16+
17+
**Example 1:**
18+
19+
**Input:** word = "dbca", numFriends = 2
20+
21+
**Output:** "dbc"
22+
23+
**Explanation:**
24+
25+
All possible splits are:
26+
27+
* `"d"` and `"bca"`.
28+
* `"db"` and `"ca"`.
29+
* `"dbc"` and `"a"`.
30+
31+
**Example 2:**
32+
33+
**Input:** word = "gggg", numFriends = 4
34+
35+
**Output:** "g"
36+
37+
**Explanation:**
38+
39+
The only possible split is: `"g"`, `"g"`, `"g"`, and `"g"`.
40+
41+
**Constraints:**
42+
43+
* <code>1 <= word.length <= 5 * 10<sup>3</sup></code>
44+
* `word` consists only of lowercase English letters.
45+
* `1 <= numFriends <= word.length`
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3401_3500.s3404_count_special_subsequences;
2+
3+
// #Medium #2024_12_29_Time_331_(100.00%)_Space_55.49_(100.00%)
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public class Solution {
9+
public long numberOfSubsequences(int[] nums) {
10+
Map<Double, Integer> freq = new HashMap<>();
11+
long ans = 0;
12+
for (int r = 4; r < nums.length; ++r) {
13+
for (int p = 0, q = r - 2; p < q - 1; ++p) {
14+
Double key = (double) nums[p] / nums[q];
15+
freq.put(key, freq.getOrDefault(key, 0) + 1);
16+
}
17+
for (int s = r + 2; s < nums.length; ++s) {
18+
ans += freq.getOrDefault((double) nums[s] / nums[r], 0);
19+
}
20+
}
21+
return ans;
22+
}
23+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
3404\. Count Special Subsequences
2+
3+
Medium
4+
5+
You are given an array `nums` consisting of positive integers.
6+
7+
A **special subsequence** is defined as a subsequence of length 4, represented by indices `(p, q, r, s)`, where `p < q < r < s`. This subsequence **must** satisfy the following conditions:
8+
9+
* `nums[p] * nums[r] == nums[q] * nums[s]`
10+
* There must be _at least_ **one** element between each pair of indices. In other words, `q - p > 1`, `r - q > 1` and `s - r > 1`.
11+
12+
A subsequence is a sequence derived from the array by deleting zero or more elements without changing the order of the remaining elements.
13+
14+
Return the _number_ of different **special** **subsequences** in `nums`.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [1,2,3,4,3,6,1]
19+
20+
**Output:** 1
21+
22+
**Explanation:**
23+
24+
There is one special subsequence in `nums`.
25+
26+
* `(p, q, r, s) = (0, 2, 4, 6)`:
27+
* This corresponds to elements `(1, 3, 3, 1)`.
28+
* `nums[p] * nums[r] = nums[0] * nums[4] = 1 * 3 = 3`
29+
* `nums[q] * nums[s] = nums[2] * nums[6] = 3 * 1 = 3`
30+
31+
**Example 2:**
32+
33+
**Input:** nums = [3,4,3,4,3,4,3,4]
34+
35+
**Output:** 3
36+
37+
**Explanation:**
38+
39+
There are three special subsequences in `nums`.
40+
41+
* `(p, q, r, s) = (0, 2, 4, 6)`:
42+
* This corresponds to elements `(3, 3, 3, 3)`.
43+
* `nums[p] * nums[r] = nums[0] * nums[4] = 3 * 3 = 9`
44+
* `nums[q] * nums[s] = nums[2] * nums[6] = 3 * 3 = 9`
45+
* `(p, q, r, s) = (1, 3, 5, 7)`:
46+
* This corresponds to elements `(4, 4, 4, 4)`.
47+
* `nums[p] * nums[r] = nums[1] * nums[5] = 4 * 4 = 16`
48+
* `nums[q] * nums[s] = nums[3] * nums[7] = 4 * 4 = 16`
49+
* `(p, q, r, s) = (0, 2, 5, 7)`:
50+
* This corresponds to elements `(3, 3, 4, 4)`.
51+
* `nums[p] * nums[r] = nums[0] * nums[5] = 3 * 4 = 12`
52+
* `nums[q] * nums[s] = nums[2] * nums[7] = 3 * 4 = 12`
53+
54+
**Constraints:**
55+
56+
* `7 <= nums.length <= 1000`
57+
* `1 <= nums[i] <= 1000`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package g3401_3500.s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements;
2+
3+
// #Hard #2024_12_29_Time_57_(100.00%)_Space_44.55_(100.00%)
4+
5+
public class Solution {
6+
private final int mod = (int) (1e9 + 7);
7+
8+
public int countGoodArrays(int n, int m, int k) {
9+
long[] f = new long[n + 1];
10+
f[0] = 1;
11+
f[1] = 1;
12+
for (int i = 2; i < f.length; i++) {
13+
f[i] = (f[i - 1] * i % mod);
14+
}
15+
long ans = comb(n - 1, k, f);
16+
ans = ans * m % mod;
17+
ans = ans * ex(m - 1, n - k - 1) % mod;
18+
return (int) ans;
19+
}
20+
21+
private long ex(long b, long e) {
22+
long ans = 1;
23+
while (e > 0) {
24+
if (e % 2 == 1) {
25+
ans = (ans * b) % mod;
26+
}
27+
b = (b * b) % mod;
28+
e = e >> 1;
29+
}
30+
return ans;
31+
}
32+
33+
private long comb(int n, int r, long[] f) {
34+
return f[n] * (ff(f[r])) % mod * ff(f[n - r]) % mod;
35+
}
36+
37+
private long ff(long x) {
38+
return ex(x, mod - 2);
39+
}
40+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
3405\. Count the Number of Arrays with K Matching Adjacent Elements
2+
3+
Hard
4+
5+
You are given three integers `n`, `m`, `k`. A **good array** `arr` of size `n` is defined as follows:
6+
7+
* Each element in `arr` is in the **inclusive** range `[1, m]`.
8+
* _Exactly_ `k` indices `i` (where `1 <= i < n`) satisfy the condition `arr[i - 1] == arr[i]`.
9+
10+
Return the number of **good arrays** that can be formed.
11+
12+
Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
13+
14+
**Example 1:**
15+
16+
**Input:** n = 3, m = 2, k = 1
17+
18+
**Output:** 4
19+
20+
**Explanation:**
21+
22+
* There are 4 good arrays. They are `[1, 1, 2]`, `[1, 2, 2]`, `[2, 1, 1]` and `[2, 2, 1]`.
23+
* Hence, the answer is 4.
24+
25+
**Example 2:**
26+
27+
**Input:** n = 4, m = 2, k = 2
28+
29+
**Output:** 6
30+
31+
**Explanation:**
32+
33+
* The good arrays are `[1, 1, 1, 2]`, `[1, 1, 2, 2]`, `[1, 2, 2, 2]`, `[2, 1, 1, 1]`, `[2, 2, 1, 1]` and `[2, 2, 2, 1]`.
34+
* Hence, the answer is 6.
35+
36+
**Example 3:**
37+
38+
**Input:** n = 5, m = 2, k = 0
39+
40+
**Output:** 2
41+
42+
**Explanation:**
43+
44+
* The good arrays are `[1, 2, 1, 2, 1]` and `[2, 1, 2, 1, 2]`. Hence, the answer is 2.
45+
46+
**Constraints:**
47+
48+
* <code>1 <= n <= 10<sup>5</sup></code>
49+
* <code>1 <= m <= 10<sup>5</sup></code>
50+
* `0 <= k <= n - 1`
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g3401_3500.s3402_minimum_operations_to_make_columns_strictly_increasing;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void minimumOperations() {
11+
assertThat(
12+
new Solution().minimumOperations(new int[][] {{3, 2}, {1, 3}, {3, 4}, {0, 1}}),
13+
equalTo(15));
14+
}
15+
16+
@Test
17+
void minimumOperations2() {
18+
assertThat(
19+
new Solution().minimumOperations(new int[][] {{3, 2, 1}, {2, 1, 0}, {1, 2, 3}}),
20+
equalTo(12));
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3401_3500.s3403_find_the_lexicographically_largest_string_from_the_box_i;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void answerString() {
11+
assertThat(new Solution().answerString("dbca", 2), equalTo("dbc"));
12+
}
13+
14+
@Test
15+
void answerString2() {
16+
assertThat(new Solution().answerString("gggg", 4), equalTo("g"));
17+
}
18+
}

0 commit comments

Comments
 (0)