Skip to content

Added tasks 3254-3261 #1804

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 6 commits into from
Aug 20, 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_20_Time_1_ms_(100.00%)_Space_45.2_MB_(95.96%)

public class Solution {
public int[] resultsArray(int[] nums, int k) {
int n = nums.length;
int[] arr = new int[n - k + 1];
int count = 0;
for (int i = 1; i < k; i++) {
if (nums[i] == nums[i - 1] + 1) {
count++;
}
}
arr[0] = (count == k - 1) ? nums[k - 1] : -1;
for (int i = 1; i <= n - k; i++) {
if (nums[i] == nums[i - 1] + 1) {
count--;
}
if (nums[i + k - 1] == nums[i + k - 2] + 1) {
count++;
}
arr[i] = (count == k - 1) ? nums[i + k - 1] : -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_20_Time_3_ms_(99.24%)_Space_64.1_MB_(67.44%)

public class Solution {
public int[] resultsArray(int[] nums, int k) {
if (k == 1) {
return nums;
}
int start = 0;
int n = nums.length;
int[] output = new int[n - k + 1];
for (int i = 1; i < n; i++) {
if (nums[i] != nums[i - 1] + 1) {
start = i;
}
int 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,65 @@
package g3201_3300.s3256_maximum_value_sum_by_placing_three_rooks_i;

// #Hard #Array #Dynamic_Programming #Matrix #Enumeration
// #2024_08_20_Time_7_ms_(100.00%)_Space_45.1_MB_(90.97%)

import java.util.Arrays;

public class Solution {
public long maximumValueSum(int[][] board) {
int n = board.length;
int m = board[0].length;
int[][] tb = new int[n][m];
tb[0] = Arrays.copyOf(board[0], m);
for (int i = 1; i < n; i++) {
for (int j = 0; j < m; j++) {
tb[i][j] = Math.max(tb[i - 1][j], board[i][j]);
}
}
int[][] bt = new int[n][m];
bt[n - 1] = Arrays.copyOf(board[n - 1], m);
for (int i = n - 2; i >= 0; i--) {
for (int j = 0; j < m; j++) {
bt[i][j] = Math.max(bt[i + 1][j], board[i][j]);
}
}
long ans = Long.MIN_VALUE;
for (int i = 1; i < n - 1; i++) {
int[][] max3Top = getMax3(tb[i - 1]);
int[][] max3Cur = getMax3(board[i]);
int[][] max3Bottom = getMax3(bt[i + 1]);
for (int[] topCand : max3Top) {
for (int[] curCand : max3Cur) {
for (int[] bottomCand : max3Bottom) {
if (topCand[1] != curCand[1]
&& topCand[1] != bottomCand[1]
&& curCand[1] != bottomCand[1]) {
long cand = (long) topCand[0] + curCand[0] + bottomCand[0];
ans = Math.max(ans, cand);
}
}
}
}
}
return ans;
}

private int[][] getMax3(int[] row) {
int m = row.length;
int[][] ans = new int[3][2];
Arrays.fill(ans, new int[] {Integer.MIN_VALUE, -1});
for (int j = 0; j < m; j++) {
if (row[j] >= ans[0][0]) {
ans[2] = ans[1];
ans[1] = ans[0];
ans[0] = new int[] {row[j], j};
} else if (row[j] >= ans[1][0]) {
ans[2] = ans[1];
ans[1] = new int[] {row[j], j};
} else if (row[j] > ans[2][0]) {
ans[2] = new int[] {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,65 @@
package g3201_3300.s3257_maximum_value_sum_by_placing_three_rooks_ii;

// #Hard #Array #Dynamic_Programming #Matrix #Enumeration
// #2024_08_20_Time_18_ms_(99.59%)_Space_74.2_MB_(66.81%)

import java.util.Arrays;

public class Solution {
public long maximumValueSum(int[][] board) {
int n = board.length;
int m = board[0].length;
int[][] tb = new int[n][m];
tb[0] = Arrays.copyOf(board[0], m);
for (int i = 1; i < n; i++) {
for (int j = 0; j < m; j++) {
tb[i][j] = Math.max(tb[i - 1][j], board[i][j]);
}
}
int[][] bt = new int[n][m];
bt[n - 1] = Arrays.copyOf(board[n - 1], m);
for (int i = n - 2; i >= 0; i--) {
for (int j = 0; j < m; j++) {
bt[i][j] = Math.max(bt[i + 1][j], board[i][j]);
}
}
long ans = Long.MIN_VALUE;
for (int i = 1; i < n - 1; i++) {
int[][] max3Top = getMax3(tb[i - 1]);
int[][] max3Cur = getMax3(board[i]);
int[][] max3Bottom = getMax3(bt[i + 1]);
for (int[] topCand : max3Top) {
for (int[] curCand : max3Cur) {
for (int[] bottomCand : max3Bottom) {
if (topCand[1] != curCand[1]
&& topCand[1] != bottomCand[1]
&& curCand[1] != bottomCand[1]) {
long cand = (long) topCand[0] + curCand[0] + bottomCand[0];
ans = Math.max(ans, cand);
}
}
}
}
}
return ans;
}

private int[][] getMax3(int[] row) {
int m = row.length;
int[][] ans = new int[3][2];
Arrays.fill(ans, new int[] {Integer.MIN_VALUE, -1});
for (int j = 0; j < m; j++) {
if (row[j] >= ans[0][0]) {
ans[2] = ans[1];
ans[1] = ans[0];
ans[0] = new int[] {row[j], j};
} else if (row[j] >= ans[1][0]) {
ans[2] = ans[1];
ans[1] = new int[] {row[j], j};
} else if (row[j] > ans[2][0]) {
ans[2] = new int[] {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
Loading