Skip to content

Commit dabacd5

Browse files
committed
Added tasks 3402-3405
1 parent ebea510 commit dabacd5

File tree

12 files changed

+410
-0
lines changed

12 files changed

+410
-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.94_(100.00%)
4+
5+
class Solution {
6+
fun minimumOperations(grid: Array<IntArray>): Int {
7+
var ans = 0
8+
for (c in grid[0].indices) {
9+
for (r in 1..<grid.size) {
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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g3401_3500.s3403_find_the_lexicographically_largest_string_from_the_box_i
2+
3+
// #Medium #2024_12_29_Time_22_(100.00%)_Space_38.72_(100.00%)
4+
5+
import kotlin.math.min
6+
7+
class Solution {
8+
fun answerString(word: String, numFriends: Int): String {
9+
if (numFriends == 1) {
10+
return word
11+
}
12+
val n = word.length
13+
val maxlen = n - numFriends + 1
14+
var maxchar = word[0]
15+
var res = ""
16+
for (i in 0..<n) {
17+
if (word[i] >= maxchar) {
18+
val curr = word.substring(i, min((i + maxlen), n))
19+
if (curr > res) {
20+
res = curr
21+
}
22+
maxchar = word[i]
23+
}
24+
}
25+
return res
26+
}
27+
}
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_276_(100.00%)_Space_60.90_(100.00%)
4+
5+
class Solution {
6+
fun numberOfSubsequences(nums: IntArray): Long {
7+
val freq: MutableMap<Double?, Int?> = HashMap<Double?, Int?>()
8+
var ans: Long = 0
9+
for (r in 4..<nums.size) {
10+
var p = 0
11+
val q = r - 2
12+
while (p < q - 1) {
13+
val key = nums[p].toDouble() / nums[q]
14+
freq.put(key, freq.getOrDefault(key, 0)!! + 1)
15+
++p
16+
}
17+
for (s in r + 2..<nums.size) {
18+
ans += freq.getOrDefault(nums[s].toDouble() / nums[r], 0)!!.toLong()
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,44 @@
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_37.63_(100.00%)
4+
5+
class Solution {
6+
fun countGoodArrays(n: Int, m: Int, k: Int): Int {
7+
val f = LongArray(n + 1)
8+
f[0] = 1
9+
f[1] = 1
10+
for (i in 2..<f.size) {
11+
f[i] = f[i - 1] * i % MOD
12+
}
13+
var ans = comb(n - 1, k, f)
14+
ans = ans * m % MOD
15+
ans = ans * ex(m - 1L, n - k - 1L) % MOD
16+
return ans.toInt()
17+
}
18+
19+
private fun ex(b: Long, e: Long): Long {
20+
var b = b
21+
var e = e
22+
var ans: Long = 1
23+
while (e > 0) {
24+
if (e % 2 == 1L) {
25+
ans = (ans * b) % MOD
26+
}
27+
b = (b * b) % MOD
28+
e = e shr 1
29+
}
30+
return ans
31+
}
32+
33+
private fun comb(n: Int, r: Int, f: LongArray): Long {
34+
return f[n] * ff(f[r]) % MOD * ff(f[n - r]) % MOD
35+
}
36+
37+
private fun ff(x: Long): Long {
38+
return ex(x, MOD - 2L)
39+
}
40+
41+
companion object {
42+
private val MOD = (1e9 + 7).toInt()
43+
}
44+
}
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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g3401_3500.s3402_minimum_operations_to_make_columns_strictly_increasing
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun minimumOperations() {
10+
assertThat<Int>(
11+
Solution().minimumOperations(
12+
arrayOf<IntArray>(
13+
intArrayOf(3, 2),
14+
intArrayOf(1, 3),
15+
intArrayOf(3, 4),
16+
intArrayOf(0, 1),
17+
),
18+
),
19+
equalTo<Int>(15),
20+
)
21+
}
22+
23+
@Test
24+
fun minimumOperations2() {
25+
assertThat<Int>(
26+
Solution().minimumOperations(
27+
arrayOf<IntArray>(
28+
intArrayOf(3, 2, 1),
29+
intArrayOf(2, 1, 0),
30+
intArrayOf(1, 2, 3),
31+
),
32+
),
33+
equalTo<Int>(12),
34+
)
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g3401_3500.s3403_find_the_lexicographically_largest_string_from_the_box_i
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun answerString() {
10+
assertThat<String>(Solution().answerString("dbca", 2), equalTo<String>("dbc"))
11+
}
12+
13+
@Test
14+
fun answerString2() {
15+
assertThat<String>(Solution().answerString("gggg", 4), equalTo<String>("g"))
16+
}
17+
18+
@Test
19+
fun answerString3() {
20+
assertThat<String>(Solution().answerString("a", 1), equalTo<String>("a"))
21+
}
22+
}

0 commit comments

Comments
 (0)