Skip to content

Commit f262f9b

Browse files
authored
Added tasks 3254-3261
1 parent 5eb8d8f commit f262f9b

File tree

24 files changed

+1074
-0
lines changed

24 files changed

+1074
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g3201_3300.s3254_find_the_power_of_k_size_subarrays_i
2+
3+
// #Medium #Array #Sliding_Window #2024_08_21_Time_245_ms_(92.59%)_Space_42.2_MB_(16.67%)
4+
5+
class Solution {
6+
fun resultsArray(nums: IntArray, k: Int): IntArray {
7+
val n = nums.size
8+
val arr = IntArray(n - k + 1)
9+
var count = 0
10+
for (i in 1 until k) {
11+
if (nums[i] == nums[i - 1] + 1) {
12+
count++
13+
}
14+
}
15+
arr[0] = if ((count == k - 1)) nums[k - 1] else -1
16+
for (i in 1..n - k) {
17+
if (nums[i] == nums[i - 1] + 1) {
18+
count--
19+
}
20+
if (nums[i + k - 1] == nums[i + k - 2] + 1) {
21+
count++
22+
}
23+
arr[i] = if ((count == k - 1)) nums[i + k - 1] else -1
24+
}
25+
return arr
26+
}
27+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
3254\. Find the Power of K-Size Subarrays I
2+
3+
Medium
4+
5+
You are given an array of integers `nums` of length `n` and a _positive_ integer `k`.
6+
7+
The **power** of an array is defined as:
8+
9+
* Its **maximum** element if _all_ of its elements are **consecutive** and **sorted** in **ascending** order.
10+
* \-1 otherwise.
11+
12+
You need to find the **power** of all subarrays of `nums` of size `k`.
13+
14+
Return an integer array `results` of size `n - k + 1`, where `results[i]` is the _power_ of `nums[i..(i + k - 1)]`.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [1,2,3,4,3,2,5], k = 3
19+
20+
**Output:** [3,4,-1,-1,-1]
21+
22+
**Explanation:**
23+
24+
There are 5 subarrays of `nums` of size 3:
25+
26+
* `[1, 2, 3]` with the maximum element 3.
27+
* `[2, 3, 4]` with the maximum element 4.
28+
* `[3, 4, 3]` whose elements are **not** consecutive.
29+
* `[4, 3, 2]` whose elements are **not** sorted.
30+
* `[3, 2, 5]` whose elements are **not** consecutive.
31+
32+
**Example 2:**
33+
34+
**Input:** nums = [2,2,2,2,2], k = 4
35+
36+
**Output:** [-1,-1]
37+
38+
**Example 3:**
39+
40+
**Input:** nums = [3,2,3,2,3,2], k = 2
41+
42+
**Output:** [-1,3,-1,3,-1]
43+
44+
**Constraints:**
45+
46+
* `1 <= n == nums.length <= 500`
47+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
48+
* `1 <= k <= n`
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g3201_3300.s3255_find_the_power_of_k_size_subarrays_ii
2+
3+
// #Medium #Array #Sliding_Window #2024_08_21_Time_892_ms_(89.36%)_Space_69.8_MB_(76.60%)
4+
5+
class Solution {
6+
fun resultsArray(nums: IntArray, k: Int): IntArray {
7+
if (k == 1) {
8+
return nums
9+
}
10+
var start = 0
11+
val n = nums.size
12+
val output = IntArray(n - k + 1)
13+
for (i in 1 until n) {
14+
if (nums[i] != nums[i - 1] + 1) {
15+
start = i
16+
}
17+
val index = i - k + 1
18+
if (index >= 0) {
19+
if (start > index) {
20+
output[index] = -1
21+
} else {
22+
output[index] = nums[i]
23+
}
24+
}
25+
}
26+
return output
27+
}
28+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
3255\. Find the Power of K-Size Subarrays II
2+
3+
Medium
4+
5+
You are given an array of integers `nums` of length `n` and a _positive_ integer `k`.
6+
7+
The **power** of an array is defined as:
8+
9+
* Its **maximum** element if _all_ of its elements are **consecutive** and **sorted** in **ascending** order.
10+
* \-1 otherwise.
11+
12+
You need to find the **power** of all subarrays of `nums` of size `k`.
13+
14+
Return an integer array `results` of size `n - k + 1`, where `results[i]` is the _power_ of `nums[i..(i + k - 1)]`.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [1,2,3,4,3,2,5], k = 3
19+
20+
**Output:** [3,4,-1,-1,-1]
21+
22+
**Explanation:**
23+
24+
There are 5 subarrays of `nums` of size 3:
25+
26+
* `[1, 2, 3]` with the maximum element 3.
27+
* `[2, 3, 4]` with the maximum element 4.
28+
* `[3, 4, 3]` whose elements are **not** consecutive.
29+
* `[4, 3, 2]` whose elements are **not** sorted.
30+
* `[3, 2, 5]` whose elements are **not** consecutive.
31+
32+
**Example 2:**
33+
34+
**Input:** nums = [2,2,2,2,2], k = 4
35+
36+
**Output:** [-1,-1]
37+
38+
**Example 3:**
39+
40+
**Input:** nums = [3,2,3,2,3,2], k = 2
41+
42+
**Output:** [-1,3,-1,3,-1]
43+
44+
**Constraints:**
45+
46+
* <code>1 <= n == nums.length <= 10<sup>5</sup></code>
47+
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
48+
* `1 <= k <= n`
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package g3201_3300.s3256_maximum_value_sum_by_placing_three_rooks_i
2+
3+
// #Hard #Array #Dynamic_Programming #Matrix #Enumeration
4+
// #2024_08_21_Time_279_ms_(100.00%)_Space_41.6_MB_(93.33%)
5+
6+
import kotlin.math.max
7+
8+
class Solution {
9+
fun maximumValueSum(board: Array<IntArray>): Long {
10+
val n = board.size
11+
val m = board[0].size
12+
val tb = Array(n) { IntArray(m) }
13+
tb[0] = board[0].copyOf(m)
14+
for (i in 1 until n) {
15+
for (j in 0 until m) {
16+
tb[i][j] = max(tb[i - 1][j], board[i][j])
17+
}
18+
}
19+
val bt = Array(n) { IntArray(m) }
20+
bt[n - 1] = board[n - 1].copyOf(m)
21+
for (i in n - 2 downTo 0) {
22+
for (j in 0 until m) {
23+
bt[i][j] = max(bt[i + 1][j], board[i][j])
24+
}
25+
}
26+
var ans = Long.MIN_VALUE
27+
for (i in 1 until n - 1) {
28+
val max3Top = getMax3(tb[i - 1])
29+
val max3Cur = getMax3(board[i])
30+
val max3Bottom = getMax3(bt[i + 1])
31+
for (topCand in max3Top) {
32+
for (curCand in max3Cur) {
33+
for (bottomCand in max3Bottom) {
34+
if (topCand[1] != curCand[1] && topCand[1] != bottomCand[1] && curCand[1] != bottomCand[1]) {
35+
val cand = topCand[0].toLong() + curCand[0] + bottomCand[0]
36+
ans = max(ans, cand)
37+
}
38+
}
39+
}
40+
}
41+
}
42+
return ans
43+
}
44+
45+
private fun getMax3(row: IntArray): Array<IntArray> {
46+
val m = row.size
47+
val ans = Array(3) { IntArray(2) }
48+
ans.fill(intArrayOf(Int.MIN_VALUE, -1))
49+
for (j in 0 until m) {
50+
if (row[j] >= ans[0][0]) {
51+
ans[2] = ans[1]
52+
ans[1] = ans[0]
53+
ans[0] = intArrayOf(row[j], j)
54+
} else if (row[j] >= ans[1][0]) {
55+
ans[2] = ans[1]
56+
ans[1] = intArrayOf(row[j], j)
57+
} else if (row[j] > ans[2][0]) {
58+
ans[2] = intArrayOf(row[j], j)
59+
}
60+
}
61+
return ans
62+
}
63+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
3256\. Maximum Value Sum by Placing Three Rooks I
2+
3+
Hard
4+
5+
You are given a `m x n` 2D array `board` representing a chessboard, where `board[i][j]` represents the **value** of the cell `(i, j)`.
6+
7+
Rooks in the **same** row or column **attack** each other. You need to place _three_ rooks on the chessboard such that the rooks **do not** **attack** each other.
8+
9+
Return the **maximum** sum of the cell **values** on which the rooks are placed.
10+
11+
**Example 1:**
12+
13+
**Input:** board = [[-3,1,1,1],[-3,1,-3,1],[-3,2,1,1]]
14+
15+
**Output:** 4
16+
17+
**Explanation:**
18+
19+
![](https://assets.leetcode.com/uploads/2024/08/08/rooks2.png)
20+
21+
We can place the rooks in the cells `(0, 2)`, `(1, 3)`, and `(2, 1)` for a sum of `1 + 1 + 2 = 4`.
22+
23+
**Example 2:**
24+
25+
**Input:** board = [[1,2,3],[4,5,6],[7,8,9]]
26+
27+
**Output:** 15
28+
29+
**Explanation:**
30+
31+
We can place the rooks in the cells `(0, 0)`, `(1, 1)`, and `(2, 2)` for a sum of `1 + 5 + 9 = 15`.
32+
33+
**Example 3:**
34+
35+
**Input:** board = [[1,1,1],[1,1,1],[1,1,1]]
36+
37+
**Output:** 3
38+
39+
**Explanation:**
40+
41+
We can place the rooks in the cells `(0, 2)`, `(1, 1)`, and `(2, 0)` for a sum of `1 + 1 + 1 = 3`.
42+
43+
**Constraints:**
44+
45+
* `3 <= m == board.length <= 100`
46+
* `3 <= n == board[i].length <= 100`
47+
* <code>-10<sup>9</sup> <= board[i][j] <= 10<sup>9</sup></code>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package g3201_3300.s3257_maximum_value_sum_by_placing_three_rooks_ii
2+
3+
// #Hard #Array #Dynamic_Programming #Matrix #Enumeration
4+
// #2024_08_21_Time_770_ms_(100.00%)_Space_87.5_MB_(33.33%)
5+
6+
import kotlin.math.max
7+
8+
class Solution {
9+
fun maximumValueSum(board: Array<IntArray>): Long {
10+
val n = board.size
11+
val m = board[0].size
12+
val tb = Array(n) { IntArray(m) }
13+
tb[0] = board[0].copyOf(m)
14+
for (i in 1 until n) {
15+
for (j in 0 until m) {
16+
tb[i][j] = max(tb[i - 1][j], board[i][j])
17+
}
18+
}
19+
val bt = Array(n) { IntArray(m) }
20+
bt[n - 1] = board[n - 1].copyOf(m)
21+
for (i in n - 2 downTo 0) {
22+
for (j in 0 until m) {
23+
bt[i][j] = max(bt[i + 1][j], board[i][j])
24+
}
25+
}
26+
var ans = Long.MIN_VALUE
27+
for (i in 1 until n - 1) {
28+
val max3Top = getMax3(tb[i - 1])
29+
val max3Cur = getMax3(board[i])
30+
val max3Bottom = getMax3(bt[i + 1])
31+
for (topCand in max3Top) {
32+
for (curCand in max3Cur) {
33+
for (bottomCand in max3Bottom) {
34+
if (topCand[1] != curCand[1] && topCand[1] != bottomCand[1] && curCand[1] != bottomCand[1]) {
35+
val cand = topCand[0].toLong() + curCand[0] + bottomCand[0]
36+
ans = max(ans, cand)
37+
}
38+
}
39+
}
40+
}
41+
}
42+
return ans
43+
}
44+
45+
private fun getMax3(row: IntArray): Array<IntArray> {
46+
val m = row.size
47+
val ans = Array(3) { IntArray(2) }
48+
ans.fill(intArrayOf(Int.MIN_VALUE, -1))
49+
for (j in 0 until m) {
50+
if (row[j] >= ans[0][0]) {
51+
ans[2] = ans[1]
52+
ans[1] = ans[0]
53+
ans[0] = intArrayOf(row[j], j)
54+
} else if (row[j] >= ans[1][0]) {
55+
ans[2] = ans[1]
56+
ans[1] = intArrayOf(row[j], j)
57+
} else if (row[j] > ans[2][0]) {
58+
ans[2] = intArrayOf(row[j], j)
59+
}
60+
}
61+
return ans
62+
}
63+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
3257\. Maximum Value Sum by Placing Three Rooks II
2+
3+
Hard
4+
5+
You are given a `m x n` 2D array `board` representing a chessboard, where `board[i][j]` represents the **value** of the cell `(i, j)`.
6+
7+
Rooks in the **same** row or column **attack** each other. You need to place _three_ rooks on the chessboard such that the rooks **do not** **attack** each other.
8+
9+
Return the **maximum** sum of the cell **values** on which the rooks are placed.
10+
11+
**Example 1:**
12+
13+
**Input:** board = [[-3,1,1,1],[-3,1,-3,1],[-3,2,1,1]]
14+
15+
**Output:** 4
16+
17+
**Explanation:**
18+
19+
![](https://assets.leetcode.com/uploads/2024/08/08/rooks2.png)
20+
21+
We can place the rooks in the cells `(0, 2)`, `(1, 3)`, and `(2, 1)` for a sum of `1 + 1 + 2 = 4`.
22+
23+
**Example 2:**
24+
25+
**Input:** board = [[1,2,3],[4,5,6],[7,8,9]]
26+
27+
**Output:** 15
28+
29+
**Explanation:**
30+
31+
We can place the rooks in the cells `(0, 0)`, `(1, 1)`, and `(2, 2)` for a sum of `1 + 5 + 9 = 15`.
32+
33+
**Example 3:**
34+
35+
**Input:** board = [[1,1,1],[1,1,1],[1,1,1]]
36+
37+
**Output:** 3
38+
39+
**Explanation:**
40+
41+
We can place the rooks in the cells `(0, 2)`, `(1, 1)`, and `(2, 0)` for a sum of `1 + 1 + 1 = 3`.
42+
43+
**Constraints:**
44+
45+
* `3 <= m == board.length <= 500`
46+
* `3 <= n == board[i].length <= 500`
47+
* <code>-10<sup>9</sup> <= board[i][j] <= 10<sup>9</sup></code>

0 commit comments

Comments
 (0)