Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9411c04

Browse files
Added tasks 3070-3075
1 parent a36b1e7 commit 9411c04

File tree

15 files changed

+572
-0
lines changed

15 files changed

+572
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g3001_3100.s3070_count_submatrices_with_top_left_element_and_sum_less_than_k;
2+
3+
// #Medium #Array #Matrix #Prefix_Sum #2024_04_15_Time_2_ms_(100.00%)_Space_117.3_MB_(94.08%)
4+
5+
public class Solution {
6+
public int countSubmatrices(int[][] grid, int k) {
7+
int n = grid[0].length;
8+
int[] sums = new int[n];
9+
int ans = 0;
10+
for (int[] ints : grid) {
11+
int sum = 0;
12+
for (int col = 0; col < n; col++) {
13+
sum += ints[col];
14+
sums[col] += sum;
15+
if (sums[col] <= k) {
16+
ans++;
17+
} else {
18+
break;
19+
}
20+
}
21+
}
22+
return ans;
23+
}
24+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
3070\. Count Submatrices with Top-Left Element and Sum Less Than k
2+
3+
Medium
4+
5+
You are given a **0-indexed** integer matrix `grid` and an integer `k`.
6+
7+
Return _the **number** of submatrices that contain the top-left element of the_ `grid`, _and have a sum less than or equal to_ `k`.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2024/01/01/example1.png)
12+
13+
**Input:** grid = [[7,6,3],[6,6,1]], k = 18
14+
15+
**Output:** 4
16+
17+
**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.
18+
19+
**Example 2:**
20+
21+
![](https://assets.leetcode.com/uploads/2024/01/01/example21.png)
22+
23+
**Input:** grid = [[7,2,9],[1,5,0],[2,6,6]], k = 20
24+
25+
**Output:** 6
26+
27+
**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.
28+
29+
**Constraints:**
30+
31+
* `m == grid.length`
32+
* `n == grid[i].length`
33+
* `1 <= n, m <= 1000`
34+
* `0 <= grid[i][j] <= 1000`
35+
* <code>1 <= k <= 10<sup>9</sup></code>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package g3001_3100.s3071_minimum_operations_to_write_the_letter_y_on_a_grid;
2+
3+
// #Medium #Array #Hash_Table #Matrix #Counting #2024_04_15_Time_1_ms_(100.00%)_Space_45_MB_(60.73%)
4+
5+
public class Solution {
6+
public int minimumOperationsToWriteY(int[][] arr) {
7+
int n = arr.length;
8+
int[] cnt1 = new int[3];
9+
int[] cnt2 = new int[3];
10+
int x = n / 2;
11+
int y = n / 2;
12+
for (int j = x; j < n; j++) {
13+
cnt1[arr[j][y]]++;
14+
arr[j][y] = 3;
15+
}
16+
for (int j = x; j >= 0; j--) {
17+
if (arr[j][j] != 3) {
18+
cnt1[arr[j][j]]++;
19+
}
20+
arr[j][j] = 3;
21+
}
22+
for (int j = x; j >= 0; j--) {
23+
if (arr[j][n - 1 - j] != 3) {
24+
cnt1[arr[j][n - 1 - j]]++;
25+
}
26+
arr[j][n - 1 - j] = 3;
27+
}
28+
for (int[] ints : arr) {
29+
for (int j = 0; j < n; j++) {
30+
if (ints[j] != 3) {
31+
cnt2[ints[j]]++;
32+
}
33+
}
34+
}
35+
int s1 = cnt1[0] + cnt1[1] + cnt1[2];
36+
int s2 = cnt2[0] + cnt2[1] + cnt2[2];
37+
int min = Integer.MAX_VALUE;
38+
for (int i = 0; i <= 2; i++) {
39+
for (int j = 0; j <= 2; j++) {
40+
if (i != j) {
41+
min = Math.min(s1 - cnt1[i] + s2 - cnt2[j], min);
42+
}
43+
}
44+
}
45+
return min;
46+
}
47+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
3071\. Minimum Operations to Write the Letter Y on a Grid
2+
3+
Medium
4+
5+
You are given a **0-indexed** `n x n` grid where `n` is odd, and `grid[r][c]` is `0`, `1`, or `2`.
6+
7+
We say that a cell belongs to the Letter **Y** if it belongs to one of the following:
8+
9+
* The diagonal starting at the top-left cell and ending at the center cell of the grid.
10+
* The diagonal starting at the top-right cell and ending at the center cell of the grid.
11+
* The vertical line starting at the center cell and ending at the bottom border of the grid.
12+
13+
The Letter **Y** is written on the grid if and only if:
14+
15+
* All values at cells belonging to the Y are equal.
16+
* All values at cells not belonging to the Y are equal.
17+
* The values at cells belonging to the Y are different from the values at cells not belonging to the Y.
18+
19+
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`_._
20+
21+
**Example 1:**
22+
23+
![](https://assets.leetcode.com/uploads/2024/01/22/y2.png)
24+
25+
**Input:** grid = [[1,2,2],[1,1,0],[0,1,0]]
26+
27+
**Output:** 3
28+
29+
**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.
30+
31+
**Example 2:**
32+
33+
![](https://assets.leetcode.com/uploads/2024/01/22/y3.png)
34+
35+
**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]]
36+
37+
**Output:** 12
38+
39+
**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.
40+
41+
**Constraints:**
42+
43+
* `3 <= n <= 49`
44+
* `n == grid.length == grid[i].length`
45+
* `0 <= grid[i][j] <= 2`
46+
* `n` is odd.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package g3001_3100.s3072_distribute_elements_into_two_arrays_ii;
2+
3+
// #Hard #Array #Simulation #Segment_Tree #Binary_Indexed_Tree
4+
// #2024_04_15_Time_48_ms_(99.90%)_Space_65_MB_(74.73%)
5+
6+
import java.util.Arrays;
7+
8+
public class Solution {
9+
static class BIT {
10+
private final int[] tree;
11+
12+
public BIT(int size) {
13+
tree = new int[size + 1];
14+
}
15+
16+
public void update(int ind) {
17+
while (ind < tree.length) {
18+
tree[ind]++;
19+
ind += lsb(ind);
20+
}
21+
}
22+
23+
public int rsq(int ind) {
24+
int sum = 0;
25+
while (ind > 0) {
26+
sum += tree[ind];
27+
ind -= lsb(ind);
28+
}
29+
30+
return sum;
31+
}
32+
33+
private int lsb(int n) {
34+
return n & (-n);
35+
}
36+
}
37+
38+
public int[] resultArray(int[] source) {
39+
int[] nums = shrink(source);
40+
int[] arr1 = new int[nums.length];
41+
int[] arr2 = new int[nums.length];
42+
arr1[0] = source[0];
43+
arr2[0] = source[1];
44+
int p1 = 0;
45+
int p2 = 0;
46+
BIT bit1 = new BIT(nums.length);
47+
bit1.update(nums[0]);
48+
BIT bit2 = new BIT(nums.length);
49+
bit2.update(nums[1]);
50+
51+
for (int i = 2; i < nums.length; i++) {
52+
int g1 = p1 + 1 - bit1.rsq(nums[i]);
53+
int g2 = p2 + 1 - bit2.rsq(nums[i]);
54+
if (g1 < g2 || p1 > p2) {
55+
p2++;
56+
arr2[p2] = source[i];
57+
bit2.update(nums[i]);
58+
} else {
59+
p1++;
60+
arr1[p1] = source[i];
61+
bit1.update(nums[i]);
62+
}
63+
}
64+
65+
for (int i = p1 + 1; i < arr1.length; i++) {
66+
arr1[i] = arr2[i - p1 - 1];
67+
}
68+
69+
return arr1;
70+
}
71+
72+
private int[] shrink(int[] nums) {
73+
long[] b = new long[nums.length];
74+
for (int i = 0; i < nums.length; i++) {
75+
b[i] = (long) nums[i] << 32 | i;
76+
}
77+
Arrays.sort(b);
78+
int[] result = new int[nums.length];
79+
int p = 1;
80+
for (int i = 0; i < nums.length; i++) {
81+
if (i > 0 && (b[i] ^ b[i - 1]) >> 32 != 0) {
82+
p++;
83+
}
84+
result[(int) b[i]] = p;
85+
}
86+
return result;
87+
}
88+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
3072\. Distribute Elements Into Two Arrays II
2+
3+
Hard
4+
5+
You are given a **1-indexed** array of integers `nums` of length `n`.
6+
7+
We define a function `greaterCount` such that `greaterCount(arr, val)` returns the number of elements in `arr` that are **strictly greater** than `val`.
8+
9+
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:
10+
11+
* If `greaterCount(arr1, nums[i]) > greaterCount(arr2, nums[i])`, append `nums[i]` to `arr1`.
12+
* If `greaterCount(arr1, nums[i]) < greaterCount(arr2, nums[i])`, append `nums[i]` to `arr2`.
13+
* If `greaterCount(arr1, nums[i]) == greaterCount(arr2, nums[i])`, append `nums[i]` to the array with a **lesser** number of elements.
14+
* If there is still a tie, append `nums[i]` to `arr1`.
15+
16+
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]`.
17+
18+
Return _the integer array_ `result`.
19+
20+
**Example 1:**
21+
22+
**Input:** nums = [2,1,3,3]
23+
24+
**Output:** [2,3,1,3]
25+
26+
**Explanation:** After the first 2 operations, arr1 = [2] and arr2 = [1].
27+
28+
In the 3<sup>rd</sup> operation, the number of elements greater than 3 is zero in both arrays.
29+
30+
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.
31+
32+
After 4 operations, arr1 = [2,3] and arr2 = [1,3]. Hence, the array result formed by concatenation is [2,3,1,3].
33+
34+
**Example 2:**
35+
36+
**Input:** nums = [5,14,3,1,2]
37+
38+
**Output:** [5,3,1,2,14]
39+
40+
**Explanation:** After the first 2 operations, arr1 = [5] and arr2 = [14].
41+
42+
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.
43+
44+
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.
45+
46+
zAfter 5 operations, arr1 = [5,3,1,2] and arr2 = [14]. Hence, the array result formed by concatenation is [5,3,1,2,14].
47+
48+
**Example 3:**
49+
50+
**Input:** nums = [3,3,3,3]
51+
52+
**Output:** [3,3,3,3]
53+
54+
**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].
55+
56+
**Constraints:**
57+
58+
* <code>3 <= n <= 10<sup>5</sup></code>
59+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g3001_3100.s3074_apple_redistribution_into_boxes;
2+
3+
// #Easy #Array #Sorting #Greedy #2024_04_15_Time_1_ms_(99.81%)_Space_41.9_MB_(89.46%)
4+
5+
public class Solution {
6+
public int minimumBoxes(int[] apple, int[] capacity) {
7+
int[] count = new int[51];
8+
int appleSum = 0;
9+
for (int j : apple) {
10+
appleSum += j;
11+
}
12+
int reqCapacity = 0;
13+
int max = 0;
14+
for (int j : capacity) {
15+
count[j]++;
16+
max = Math.max(max, j);
17+
}
18+
for (int i = max; i >= 0; i--) {
19+
if (count[i] >= 1) {
20+
while (count[i] != 0) {
21+
appleSum -= i;
22+
reqCapacity++;
23+
if (appleSum <= 0) {
24+
return reqCapacity;
25+
}
26+
count[i]--;
27+
}
28+
}
29+
}
30+
return reqCapacity;
31+
}
32+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
3074\. Apple Redistribution into Boxes
2+
3+
Easy
4+
5+
You are given an array `apple` of size `n` and an array `capacity` of size `m`.
6+
7+
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.
8+
9+
Return _the **minimum** number of boxes you need to select to redistribute these_ `n` _packs of apples into boxes_.
10+
11+
**Note** that, apples from the same pack can be distributed into different boxes.
12+
13+
**Example 1:**
14+
15+
**Input:** apple = [1,3,2], capacity = [4,3,1,5,2]
16+
17+
**Output:** 2
18+
19+
**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.
20+
21+
**Example 2:**
22+
23+
**Input:** apple = [5,5,5], capacity = [2,4,2,7]
24+
25+
**Output:** 4
26+
27+
**Explanation:** We will need to use all the boxes.
28+
29+
**Constraints:**
30+
31+
* `1 <= n == apple.length <= 50`
32+
* `1 <= m == capacity.length <= 50`
33+
* `1 <= apple[i], capacity[i] <= 50`
34+
* The input is generated such that it's possible to redistribute packs of apples into boxes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g3001_3100.s3075_maximize_happiness_of_selected_children;
2+
3+
// #Medium #Array #Sorting #Greedy #2024_04_15_Time_34_ms_(97.43%)_Space_61.4_MB_(77.84%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public long maximumHappinessSum(int[] happiness, int k) {
9+
Arrays.sort(happiness);
10+
long sum = 0;
11+
for (int i = happiness.length - 1; i >= happiness.length - k; i--) {
12+
happiness[i] = Math.max(0, happiness[i] - (happiness.length - 1 - i));
13+
sum += happiness[i];
14+
}
15+
return sum;
16+
}
17+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /