Skip to content

Added tasks 3070-3075 #1732

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 3 commits into from
Apr 15, 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,24 @@
package g3001_3100.s3070_count_submatrices_with_top_left_element_and_sum_less_than_k;

// #Medium #Array #Matrix #Prefix_Sum #2024_04_15_Time_2_ms_(100.00%)_Space_117.3_MB_(94.08%)

public class Solution {
public int countSubmatrices(int[][] grid, int k) {
int n = grid[0].length;
int[] sums = new int[n];
int ans = 0;
for (int[] ints : grid) {
int sum = 0;
for (int col = 0; col < n; col++) {
sum += ints[col];
sums[col] += sum;
if (sums[col] <= k) {
ans++;
} else {
break;
}
}
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
3070\. Count Submatrices with Top-Left Element and Sum Less Than k

Medium

You are given a **0-indexed** integer matrix `grid` and an integer `k`.

Return _the **number** of submatrices that contain the top-left element of the_ `grid`, _and have a sum less than or equal to_ `k`.

**Example 1:**

![](https://assets.leetcode.com/uploads/2024/01/01/example1.png)

**Input:** grid = [[7,6,3],[6,6,1]], k = 18

**Output:** 4

**Explanation:** There are only 4 submatrices, shown in the image above, that contain the top-left element of grid, and have a sum less than or equal to 18.

**Example 2:**

![](https://assets.leetcode.com/uploads/2024/01/01/example21.png)

**Input:** grid = [[7,2,9],[1,5,0],[2,6,6]], k = 20

**Output:** 6

**Explanation:** There are only 6 submatrices, shown in the image above, that contain the top-left element of grid, and have a sum less than or equal to 20.

**Constraints:**

* `m == grid.length`
* `n == grid[i].length`
* `1 <= n, m <= 1000`
* `0 <= grid[i][j] <= 1000`
* <code>1 <= k <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package g3001_3100.s3071_minimum_operations_to_write_the_letter_y_on_a_grid;

// #Medium #Array #Hash_Table #Matrix #Counting #2024_04_15_Time_1_ms_(100.00%)_Space_45_MB_(60.73%)

public class Solution {
public int minimumOperationsToWriteY(int[][] arr) {
int n = arr.length;
int[] cnt1 = new int[3];
int[] cnt2 = new int[3];
int x = n / 2;
int y = n / 2;
for (int j = x; j < n; j++) {
cnt1[arr[j][y]]++;
arr[j][y] = 3;
}
for (int j = x; j >= 0; j--) {
if (arr[j][j] != 3) {
cnt1[arr[j][j]]++;
}
arr[j][j] = 3;
}
for (int j = x; j >= 0; j--) {
if (arr[j][n - 1 - j] != 3) {
cnt1[arr[j][n - 1 - j]]++;
}
arr[j][n - 1 - j] = 3;
}
for (int[] ints : arr) {
for (int j = 0; j < n; j++) {
if (ints[j] != 3) {
cnt2[ints[j]]++;
}
}
}
int s1 = cnt1[0] + cnt1[1] + cnt1[2];
int s2 = cnt2[0] + cnt2[1] + cnt2[2];
int min = Integer.MAX_VALUE;
for (int i = 0; i <= 2; i++) {
for (int j = 0; j <= 2; j++) {
if (i != j) {
min = Math.min(s1 - cnt1[i] + s2 - cnt2[j], min);
}
}
}
return min;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
3071\. Minimum Operations to Write the Letter Y on a Grid

Medium

You are given a **0-indexed** `n x n` grid where `n` is odd, and `grid[r][c]` is `0`, `1`, or `2`.

We say that a cell belongs to the Letter **Y** if it belongs to one of the following:

* The diagonal starting at the top-left cell and ending at the center cell of the grid.
* The diagonal starting at the top-right cell and ending at the center cell of the grid.
* The vertical line starting at the center cell and ending at the bottom border of the grid.

The Letter **Y** is written on the grid if and only if:

* All values at cells belonging to the Y are equal.
* All values at cells not belonging to the Y are equal.
* The values at cells belonging to the Y are different from the values at cells not belonging to the Y.

Return _the **minimum** number of operations needed to write the letter Y on the grid given that in one operation you can change the value at any cell to_ `0`_,_ `1`_,_ _or_ `2`_._

**Example 1:**

![](https://assets.leetcode.com/uploads/2024/01/22/y2.png)

**Input:** grid = [[1,2,2],[1,1,0],[0,1,0]]

**Output:** 3

**Explanation:** We can write Y on the grid by applying the changes highlighted in blue in the image above. After the operations, all cells that belong to Y, denoted in bold, have the same value of 1 while those that do not belong to Y are equal to 0. It can be shown that 3 is the minimum number of operations needed to write Y on the grid.

**Example 2:**

![](https://assets.leetcode.com/uploads/2024/01/22/y3.png)

**Input:** grid = [[0,1,0,1,0],[2,1,0,1,2],[2,2,2,0,1],[2,2,2,2,2],[2,1,2,2,2]]

**Output:** 12

**Explanation:** We can write Y on the grid by applying the changes highlighted in blue in the image above. After the operations, all cells that belong to Y, denoted in bold, have the same value of 0 while those that do not belong to Y are equal to 2. It can be shown that 12 is the minimum number of operations needed to write Y on the grid.

**Constraints:**

* `3 <= n <= 49`
* `n == grid.length == grid[i].length`
* `0 <= grid[i][j] <= 2`
* `n` is odd.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package g3001_3100.s3072_distribute_elements_into_two_arrays_ii;

// #Hard #Array #Simulation #Segment_Tree #Binary_Indexed_Tree
// #2024_04_15_Time_48_ms_(99.90%)_Space_65_MB_(74.73%)

import java.util.Arrays;

public class Solution {
static class BIT {
private final int[] tree;

public BIT(int size) {
tree = new int[size + 1];
}

public void update(int ind) {
while (ind < tree.length) {
tree[ind]++;
ind += lsb(ind);
}
}

public int rsq(int ind) {
int sum = 0;
while (ind > 0) {
sum += tree[ind];
ind -= lsb(ind);
}

return sum;
}

private int lsb(int n) {
return n & (-n);
}
}

public int[] resultArray(int[] source) {
int[] nums = shrink(source);
int[] arr1 = new int[nums.length];
int[] arr2 = new int[nums.length];
arr1[0] = source[0];
arr2[0] = source[1];
int p1 = 0;
int p2 = 0;
BIT bit1 = new BIT(nums.length);
bit1.update(nums[0]);
BIT bit2 = new BIT(nums.length);
bit2.update(nums[1]);

for (int i = 2; i < nums.length; i++) {
int g1 = p1 + 1 - bit1.rsq(nums[i]);
int g2 = p2 + 1 - bit2.rsq(nums[i]);
if (g1 < g2 || p1 > p2) {
p2++;
arr2[p2] = source[i];
bit2.update(nums[i]);
} else {
p1++;
arr1[p1] = source[i];
bit1.update(nums[i]);
}
}

for (int i = p1 + 1; i < arr1.length; i++) {
arr1[i] = arr2[i - p1 - 1];
}

return arr1;
}

private int[] shrink(int[] nums) {
long[] b = new long[nums.length];
for (int i = 0; i < nums.length; i++) {
b[i] = (long) nums[i] << 32 | i;
}
Arrays.sort(b);
int[] result = new int[nums.length];
int p = 1;
for (int i = 0; i < nums.length; i++) {
if (i > 0 && (b[i] ^ b[i - 1]) >> 32 != 0) {
p++;
}
result[(int) b[i]] = p;
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
3072\. Distribute Elements Into Two Arrays II

Hard

You are given a **1-indexed** array of integers `nums` of length `n`.

We define a function `greaterCount` such that `greaterCount(arr, val)` returns the number of elements in `arr` that are **strictly greater** than `val`.

You need to distribute all the elements of `nums` between two arrays `arr1` and `arr2` using `n` operations. In the first operation, append `nums[1]` to `arr1`. In the second operation, append `nums[2]` to `arr2`. Afterwards, in the <code>i<sup>th</sup></code> operation:

* If `greaterCount(arr1, nums[i]) > greaterCount(arr2, nums[i])`, append `nums[i]` to `arr1`.
* If `greaterCount(arr1, nums[i]) < greaterCount(arr2, nums[i])`, append `nums[i]` to `arr2`.
* If `greaterCount(arr1, nums[i]) == greaterCount(arr2, nums[i])`, append `nums[i]` to the array with a **lesser** number of elements.
* If there is still a tie, append `nums[i]` to `arr1`.

The array `result` is formed by concatenating the arrays `arr1` and `arr2`. For example, if `arr1 == [1,2,3]` and `arr2 == [4,5,6]`, then `result = [1,2,3,4,5,6]`.

Return _the integer array_ `result`.

**Example 1:**

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

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

**Explanation:** After the first 2 operations, arr1 = [2] and arr2 = [1].

In the 3<sup>rd</sup> operation, the number of elements greater than 3 is zero in both arrays.

Also, the lengths are equal, hence, append nums[3] to arr1. In the 4<sup>th</sup> operation, the number of elements greater than 3 is zero in both arrays. As the length of arr2 is lesser, hence, append nums[4] to arr2.

After 4 operations, arr1 = [2,3] and arr2 = [1,3]. Hence, the array result formed by concatenation is [2,3,1,3].

**Example 2:**

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

**Output:** [5,3,1,2,14]

**Explanation:** After the first 2 operations, arr1 = [5] and arr2 = [14].

In the 3<sup>rd</sup> operation, the number of elements greater than 3 is one in both arrays. Also, the lengths are equal, hence, append nums[3] to arr1.

In the 4<sup>th</sup> operation, the number of elements greater than 1 is greater in arr1 than arr2 (2 > 1). Hence, append nums[4] to arr1. In the 5<sup>th</sup> operation, the number of elements greater than 2 is greater in arr1 than arr2 (2 > 1). Hence, append nums[5] to arr1.

zAfter 5 operations, arr1 = [5,3,1,2] and arr2 = [14]. Hence, the array result formed by concatenation is [5,3,1,2,14].

**Example 3:**

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

**Output:** [3,3,3,3]

**Explanation:** At the end of 4 operations, arr1 = [3,3] and arr2 = [3,3]. Hence, the array result formed by concatenation is [3,3,3,3].

**Constraints:**

* <code>3 <= n <= 10<sup>5</sup></code>
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package g3001_3100.s3074_apple_redistribution_into_boxes;

// #Easy #Array #Sorting #Greedy #2024_04_15_Time_1_ms_(99.81%)_Space_41.9_MB_(89.46%)

public class Solution {
public int minimumBoxes(int[] apple, int[] capacity) {
int[] count = new int[51];
int appleSum = 0;
for (int j : apple) {
appleSum += j;
}
int reqCapacity = 0;
int max = 0;
for (int j : capacity) {
count[j]++;
max = Math.max(max, j);
}
for (int i = max; i >= 0; i--) {
if (count[i] >= 1) {
while (count[i] != 0) {
appleSum -= i;
reqCapacity++;
if (appleSum <= 0) {
return reqCapacity;
}
count[i]--;
}
}
}
return reqCapacity;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
3074\. Apple Redistribution into Boxes

Easy

You are given an array `apple` of size `n` and an array `capacity` of size `m`.

There are `n` packs where the <code>i<sup>th</sup></code> pack contains `apple[i]` apples. There are `m` boxes as well, and the <code>i<sup>th</sup></code> box has a capacity of `capacity[i]` apples.

Return _the **minimum** number of boxes you need to select to redistribute these_ `n` _packs of apples into boxes_.

**Note** that, apples from the same pack can be distributed into different boxes.

**Example 1:**

**Input:** apple = [1,3,2], capacity = [4,3,1,5,2]

**Output:** 2

**Explanation:** We will use boxes with capacities 4 and 5. It is possible to distribute the apples as the total capacity is greater than or equal to the total number of apples.

**Example 2:**

**Input:** apple = [5,5,5], capacity = [2,4,2,7]

**Output:** 4

**Explanation:** We will need to use all the boxes.

**Constraints:**

* `1 <= n == apple.length <= 50`
* `1 <= m == capacity.length <= 50`
* `1 <= apple[i], capacity[i] <= 50`
* The input is generated such that it's possible to redistribute packs of apples into boxes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package g3001_3100.s3075_maximize_happiness_of_selected_children;

// #Medium #Array #Sorting #Greedy #2024_04_15_Time_34_ms_(97.43%)_Space_61.4_MB_(77.84%)

import java.util.Arrays;

public class Solution {
public long maximumHappinessSum(int[] happiness, int k) {
Arrays.sort(happiness);
long sum = 0;
for (int i = happiness.length - 1; i >= happiness.length - k; i--) {
happiness[i] = Math.max(0, happiness[i] - (happiness.length - 1 - i));
sum += happiness[i];
}
return sum;
}
}
Loading
Loading