Skip to content

Added tasks 3254-3261 #682

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 1 commit into from
Aug 21, 2024
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 g3201_3300.s3254_find_the_power_of_k_size_subarrays_i

// #Medium #Array #Sliding_Window #2024_08_21_Time_245_ms_(92.59%)_Space_42.2_MB_(16.67%)

class Solution {
fun resultsArray(nums: IntArray, k: Int): IntArray {
val n = nums.size
val arr = IntArray(n - k + 1)
var count = 0
for (i in 1 until k) {
if (nums[i] == nums[i - 1] + 1) {
count++
}
}
arr[0] = if ((count == k - 1)) nums[k - 1] else -1
for (i in 1..n - k) {
if (nums[i] == nums[i - 1] + 1) {
count--
}
if (nums[i + k - 1] == nums[i + k - 2] + 1) {
count++
}
arr[i] = if ((count == k - 1)) nums[i + k - 1] else -1
}
return arr
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
3254\. Find the Power of K-Size Subarrays I

Medium

You are given an array of integers `nums` of length `n` and a _positive_ integer `k`.

The **power** of an array is defined as:

* Its **maximum** element if _all_ of its elements are **consecutive** and **sorted** in **ascending** order.
* \-1 otherwise.

You need to find the **power** of all subarrays of `nums` of size `k`.

Return an integer array `results` of size `n - k + 1`, where `results[i]` is the _power_ of `nums[i..(i + k - 1)]`.

**Example 1:**

**Input:** nums = [1,2,3,4,3,2,5], k = 3

**Output:** [3,4,-1,-1,-1]

**Explanation:**

There are 5 subarrays of `nums` of size 3:

* `[1, 2, 3]` with the maximum element 3.
* `[2, 3, 4]` with the maximum element 4.
* `[3, 4, 3]` whose elements are **not** consecutive.
* `[4, 3, 2]` whose elements are **not** sorted.
* `[3, 2, 5]` whose elements are **not** consecutive.

**Example 2:**

**Input:** nums = [2,2,2,2,2], k = 4

**Output:** [-1,-1]

**Example 3:**

**Input:** nums = [3,2,3,2,3,2], k = 2

**Output:** [-1,3,-1,3,-1]

**Constraints:**

* `1 <= n == nums.length <= 500`
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
* `1 <= k <= n`
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package g3201_3300.s3255_find_the_power_of_k_size_subarrays_ii

// #Medium #Array #Sliding_Window #2024_08_21_Time_892_ms_(89.36%)_Space_69.8_MB_(76.60%)

class Solution {
fun resultsArray(nums: IntArray, k: Int): IntArray {
if (k == 1) {
return nums
}
var start = 0
val n = nums.size
val output = IntArray(n - k + 1)
for (i in 1 until n) {
if (nums[i] != nums[i - 1] + 1) {
start = i
}
val index = i - k + 1
if (index >= 0) {
if (start > index) {
output[index] = -1
} else {
output[index] = nums[i]
}
}
}
return output
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
3255\. Find the Power of K-Size Subarrays II

Medium

You are given an array of integers `nums` of length `n` and a _positive_ integer `k`.

The **power** of an array is defined as:

* Its **maximum** element if _all_ of its elements are **consecutive** and **sorted** in **ascending** order.
* \-1 otherwise.

You need to find the **power** of all subarrays of `nums` of size `k`.

Return an integer array `results` of size `n - k + 1`, where `results[i]` is the _power_ of `nums[i..(i + k - 1)]`.

**Example 1:**

**Input:** nums = [1,2,3,4,3,2,5], k = 3

**Output:** [3,4,-1,-1,-1]

**Explanation:**

There are 5 subarrays of `nums` of size 3:

* `[1, 2, 3]` with the maximum element 3.
* `[2, 3, 4]` with the maximum element 4.
* `[3, 4, 3]` whose elements are **not** consecutive.
* `[4, 3, 2]` whose elements are **not** sorted.
* `[3, 2, 5]` whose elements are **not** consecutive.

**Example 2:**

**Input:** nums = [2,2,2,2,2], k = 4

**Output:** [-1,-1]

**Example 3:**

**Input:** nums = [3,2,3,2,3,2], k = 2

**Output:** [-1,3,-1,3,-1]

**Constraints:**

* <code>1 <= n == nums.length <= 10<sup>5</sup></code>
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
* `1 <= k <= n`
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package g3201_3300.s3256_maximum_value_sum_by_placing_three_rooks_i

// #Hard #Array #Dynamic_Programming #Matrix #Enumeration
// #2024_08_21_Time_279_ms_(100.00%)_Space_41.6_MB_(93.33%)

import kotlin.math.max

class Solution {
fun maximumValueSum(board: Array<IntArray>): Long {
val n = board.size
val m = board[0].size
val tb = Array(n) { IntArray(m) }
tb[0] = board[0].copyOf(m)
for (i in 1 until n) {
for (j in 0 until m) {
tb[i][j] = max(tb[i - 1][j], board[i][j])
}
}
val bt = Array(n) { IntArray(m) }
bt[n - 1] = board[n - 1].copyOf(m)
for (i in n - 2 downTo 0) {
for (j in 0 until m) {
bt[i][j] = max(bt[i + 1][j], board[i][j])
}
}
var ans = Long.MIN_VALUE
for (i in 1 until n - 1) {
val max3Top = getMax3(tb[i - 1])
val max3Cur = getMax3(board[i])
val max3Bottom = getMax3(bt[i + 1])
for (topCand in max3Top) {
for (curCand in max3Cur) {
for (bottomCand in max3Bottom) {
if (topCand[1] != curCand[1] && topCand[1] != bottomCand[1] && curCand[1] != bottomCand[1]) {
val cand = topCand[0].toLong() + curCand[0] + bottomCand[0]
ans = max(ans, cand)
}
}
}
}
}
return ans
}

private fun getMax3(row: IntArray): Array<IntArray> {
val m = row.size
val ans = Array(3) { IntArray(2) }
ans.fill(intArrayOf(Int.MIN_VALUE, -1))
for (j in 0 until m) {
if (row[j] >= ans[0][0]) {
ans[2] = ans[1]
ans[1] = ans[0]
ans[0] = intArrayOf(row[j], j)
} else if (row[j] >= ans[1][0]) {
ans[2] = ans[1]
ans[1] = intArrayOf(row[j], j)
} else if (row[j] > ans[2][0]) {
ans[2] = intArrayOf(row[j], j)
}
}
return ans
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
3256\. Maximum Value Sum by Placing Three Rooks I

Hard

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)`.

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.

Return the **maximum** sum of the cell **values** on which the rooks are placed.

**Example 1:**

**Input:** board = [[-3,1,1,1],[-3,1,-3,1],[-3,2,1,1]]

**Output:** 4

**Explanation:**

![](https://assets.leetcode.com/uploads/2024/08/08/rooks2.png)

We can place the rooks in the cells `(0, 2)`, `(1, 3)`, and `(2, 1)` for a sum of `1 + 1 + 2 = 4`.

**Example 2:**

**Input:** board = [[1,2,3],[4,5,6],[7,8,9]]

**Output:** 15

**Explanation:**

We can place the rooks in the cells `(0, 0)`, `(1, 1)`, and `(2, 2)` for a sum of `1 + 5 + 9 = 15`.

**Example 3:**

**Input:** board = [[1,1,1],[1,1,1],[1,1,1]]

**Output:** 3

**Explanation:**

We can place the rooks in the cells `(0, 2)`, `(1, 1)`, and `(2, 0)` for a sum of `1 + 1 + 1 = 3`.

**Constraints:**

* `3 <= m == board.length <= 100`
* `3 <= n == board[i].length <= 100`
* <code>-10<sup>9</sup> <= board[i][j] <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package g3201_3300.s3257_maximum_value_sum_by_placing_three_rooks_ii

// #Hard #Array #Dynamic_Programming #Matrix #Enumeration
// #2024_08_21_Time_770_ms_(100.00%)_Space_87.5_MB_(33.33%)

import kotlin.math.max

class Solution {
fun maximumValueSum(board: Array<IntArray>): Long {
val n = board.size
val m = board[0].size
val tb = Array(n) { IntArray(m) }
tb[0] = board[0].copyOf(m)
for (i in 1 until n) {
for (j in 0 until m) {
tb[i][j] = max(tb[i - 1][j], board[i][j])
}
}
val bt = Array(n) { IntArray(m) }
bt[n - 1] = board[n - 1].copyOf(m)
for (i in n - 2 downTo 0) {
for (j in 0 until m) {
bt[i][j] = max(bt[i + 1][j], board[i][j])
}
}
var ans = Long.MIN_VALUE
for (i in 1 until n - 1) {
val max3Top = getMax3(tb[i - 1])
val max3Cur = getMax3(board[i])
val max3Bottom = getMax3(bt[i + 1])
for (topCand in max3Top) {
for (curCand in max3Cur) {
for (bottomCand in max3Bottom) {
if (topCand[1] != curCand[1] && topCand[1] != bottomCand[1] && curCand[1] != bottomCand[1]) {
val cand = topCand[0].toLong() + curCand[0] + bottomCand[0]
ans = max(ans, cand)
}
}
}
}
}
return ans
}

private fun getMax3(row: IntArray): Array<IntArray> {
val m = row.size
val ans = Array(3) { IntArray(2) }
ans.fill(intArrayOf(Int.MIN_VALUE, -1))
for (j in 0 until m) {
if (row[j] >= ans[0][0]) {
ans[2] = ans[1]
ans[1] = ans[0]
ans[0] = intArrayOf(row[j], j)
} else if (row[j] >= ans[1][0]) {
ans[2] = ans[1]
ans[1] = intArrayOf(row[j], j)
} else if (row[j] > ans[2][0]) {
ans[2] = intArrayOf(row[j], j)
}
}
return ans
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
3257\. Maximum Value Sum by Placing Three Rooks II

Hard

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)`.

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.

Return the **maximum** sum of the cell **values** on which the rooks are placed.

**Example 1:**

**Input:** board = [[-3,1,1,1],[-3,1,-3,1],[-3,2,1,1]]

**Output:** 4

**Explanation:**

![](https://assets.leetcode.com/uploads/2024/08/08/rooks2.png)

We can place the rooks in the cells `(0, 2)`, `(1, 3)`, and `(2, 1)` for a sum of `1 + 1 + 2 = 4`.

**Example 2:**

**Input:** board = [[1,2,3],[4,5,6],[7,8,9]]

**Output:** 15

**Explanation:**

We can place the rooks in the cells `(0, 0)`, `(1, 1)`, and `(2, 2)` for a sum of `1 + 5 + 9 = 15`.

**Example 3:**

**Input:** board = [[1,1,1],[1,1,1],[1,1,1]]

**Output:** 3

**Explanation:**

We can place the rooks in the cells `(0, 2)`, `(1, 1)`, and `(2, 0)` for a sum of `1 + 1 + 1 = 3`.

**Constraints:**

* `3 <= m == board.length <= 500`
* `3 <= n == board[i].length <= 500`
* <code>-10<sup>9</sup> <= board[i][j] <= 10<sup>9</sup></code>
Loading