Skip to content

Commit 1a3e15d

Browse files
authored
Added tasks 3038-3049
1 parent e5c1f73 commit 1a3e15d

File tree

18 files changed

+800
-0
lines changed

18 files changed

+800
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g3001_3100.s3038_maximum_number_of_operations_with_the_same_score_i;
2+
3+
// #Easy #Array #Simulation #2024_03_04_Time_0_ms_(100.00%)_Space_41.5_MB_(92.21%)
4+
5+
public class Solution {
6+
public int maxOperations(int[] nums) {
7+
int c = 1;
8+
for (int i = 2, s = nums[0] + nums[1], l = nums.length - (nums.length % 2 == 0 ? 0 : 1);
9+
i < l;
10+
i += 2) {
11+
if (nums[i] + nums[i + 1] == s) {
12+
c++;
13+
} else {
14+
break;
15+
}
16+
}
17+
return c;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
3038\. Maximum Number of Operations With the Same Score I
2+
3+
Easy
4+
5+
Given an array of integers called `nums`, you can perform the following operation while `nums` contains **at least** `2` elements:
6+
7+
* Choose the first two elements of `nums` and delete them.
8+
9+
The **score** of the operation is the sum of the deleted elements.
10+
11+
Your task is to find the **maximum** number of operations that can be performed, such that **all operations have the same score**.
12+
13+
Return _the **maximum** number of operations possible that satisfy the condition mentioned above_.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [3,2,1,4,5]
18+
19+
**Output:** 2
20+
21+
**Explanation:** We perform the following operations:
22+
- Delete the first two elements, with score 3 + 2 = 5, nums = [1,4,5].
23+
- Delete the first two elements, with score 1 + 4 = 5, nums = [5].
24+
25+
We are unable to perform any more operations as nums contain only 1 element.
26+
27+
**Example 2:**
28+
29+
**Input:** nums = [3,2,6,1,4]
30+
31+
**Output:** 1
32+
33+
**Explanation:** We perform the following operations:
34+
- Delete the first two elements, with score 3 + 2 = 5, nums = [6,1,4].
35+
36+
We are unable to perform any more operations as the score of the next operation isn't the same as the previous one.
37+
38+
**Constraints:**
39+
40+
* `2 <= nums.length <= 100`
41+
* `1 <= nums[i] <= 1000`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g3001_3100.s3039_apply_operations_to_make_string_empty;
2+
3+
// #Medium #Array #Hash_Table #Sorting #Counting
4+
// #2024_03_04_Time_18_ms_(93.00%)_Space_48.3_MB_(94.05%)
5+
6+
public class Solution {
7+
public String lastNonEmptyString(String s) {
8+
int[] freq = new int[26];
9+
char[] ar = s.toCharArray();
10+
int n = ar.length;
11+
int max = 1;
12+
StringBuilder sb = new StringBuilder();
13+
for (char c : ar) {
14+
freq[c - 'a']++;
15+
max = Math.max(freq[c - 'a'], max);
16+
}
17+
for (int i = n - 1; i >= 0; i--) {
18+
if (freq[ar[i] - 'a'] == max) {
19+
sb.append(ar[i]);
20+
freq[ar[i] - 'a'] = 0;
21+
}
22+
}
23+
return sb.reverse().toString();
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
3039\. Apply Operations to Make String Empty
2+
3+
Medium
4+
5+
You are given a string `s`.
6+
7+
Consider performing the following operation until `s` becomes **empty**:
8+
9+
* For **every** alphabet character from `'a'` to `'z'`, remove the **first** occurrence of that character in `s` (if it exists).
10+
11+
For example, let initially `s = "aabcbbca"`. We do the following operations:
12+
13+
* Remove the underlined characters <code>s = "<ins>**a**</ins>a**<ins>bc</ins>**bbca"</code>. The resulting string is `s = "abbca"`.
14+
* Remove the underlined characters <code>s = "<ins>**ab**</ins>b<ins>**c**</ins>a"</code>. The resulting string is `s = "ba"`.
15+
* Remove the underlined characters <code>s = "<ins>**ba**</ins>"</code>. The resulting string is `s = ""`.
16+
17+
Return _the value of the string_ `s` _right **before** applying the **last** operation_. In the example above, answer is `"ba"`.
18+
19+
**Example 1:**
20+
21+
**Input:** s = "aabcbbca"
22+
23+
**Output:** "ba"
24+
25+
**Explanation:** Explained in the statement.
26+
27+
**Example 2:**
28+
29+
**Input:** s = "abcd"
30+
31+
**Output:** "abcd"
32+
33+
**Explanation:** We do the following operation:
34+
- Remove the underlined characters s = "<ins>**abcd**</ins>". The resulting string is s = "".
35+
36+
The string just before the last operation is "abcd".
37+
38+
**Constraints:**
39+
40+
* <code>1 <= s.length <= 5 * 10<sup>5</sup></code>
41+
* `s` consists only of lowercase English letters.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package g3001_3100.s3040_maximum_number_of_operations_with_the_same_score_ii;
2+
3+
// #Medium #Array #Dynamic_Programming #Memoization
4+
// #2024_03_04_Time_3_ms_(99.75%)_Space_44.6_MB_(99.29%)
5+
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
import java.util.Objects;
9+
10+
public class Solution {
11+
private int[] nums;
12+
13+
private int maxOps = 1;
14+
15+
private final Map<Pos, Integer> dp = new HashMap<>();
16+
17+
private static class Pos {
18+
int start;
19+
int end;
20+
int sum;
21+
22+
Pos(int start, int end, int sum) {
23+
this.start = start;
24+
this.end = end;
25+
this.sum = sum;
26+
}
27+
28+
@Override
29+
public boolean equals(Object o) {
30+
if (o == null) {
31+
return false;
32+
}
33+
if (!(o instanceof Pos)) {
34+
return false;
35+
}
36+
return start == ((Pos) o).start && end == ((Pos) o).end && sum == ((Pos) o).sum;
37+
}
38+
39+
@Override
40+
public int hashCode() {
41+
return Objects.hash(start, end, sum);
42+
}
43+
}
44+
45+
public int maxOperations(int[] nums) {
46+
this.nums = nums;
47+
var length = nums.length;
48+
49+
maxOperations(2, length - 1, nums[0] + nums[1], 1);
50+
maxOperations(0, length - 3, nums[length - 2] + nums[length - 1], 1);
51+
maxOperations(1, length - 2, nums[0] + nums[length - 1], 1);
52+
53+
return maxOps;
54+
}
55+
56+
private void maxOperations(int start, int end, int sum, int nOps) {
57+
if (start >= end) {
58+
return;
59+
}
60+
61+
if ((((end - start) / 2) + nOps) < maxOps) {
62+
return;
63+
}
64+
65+
Pos pos = new Pos(start, end, sum);
66+
Integer posNops = dp.get(pos);
67+
if (posNops != null && posNops >= nOps) {
68+
return;
69+
}
70+
dp.put(pos, nOps);
71+
if (nums[start] + nums[start + 1] == sum) {
72+
maxOps = Math.max(maxOps, nOps + 1);
73+
maxOperations(start + 2, end, sum, nOps + 1);
74+
}
75+
if (nums[end - 1] + nums[end] == sum) {
76+
maxOps = Math.max(maxOps, nOps + 1);
77+
maxOperations(start, end - 2, sum, nOps + 1);
78+
}
79+
if (nums[start] + nums[end] == sum) {
80+
maxOps = Math.max(maxOps, nOps + 1);
81+
maxOperations(start + 1, end - 1, sum, nOps + 1);
82+
}
83+
}
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
3040\. Maximum Number of Operations With the Same Score II
2+
3+
Medium
4+
5+
Given an array of integers called `nums`, you can perform **any** of the following operation while `nums` contains **at least** `2` elements:
6+
7+
* Choose the first two elements of `nums` and delete them.
8+
* Choose the last two elements of `nums` and delete them.
9+
* Choose the first and the last elements of `nums` and delete them.
10+
11+
The **score** of the operation is the sum of the deleted elements.
12+
13+
Your task is to find the **maximum** number of operations that can be performed, such that **all operations have the same score**.
14+
15+
Return _the **maximum** number of operations possible that satisfy the condition mentioned above_.
16+
17+
**Example 1:**
18+
19+
**Input:** nums = [3,2,1,2,3,4]
20+
21+
**Output:** 3
22+
23+
**Explanation:** We perform the following operations:
24+
- Delete the first two elements, with score 3 + 2 = 5, nums = [1,2,3,4].
25+
- Delete the first and the last elements, with score 1 + 4 = 5, nums = [2,3].
26+
- Delete the first and the last elements, with score 2 + 3 = 5, nums = [].
27+
28+
We are unable to perform any more operations as nums is empty.
29+
30+
**Example 2:**
31+
32+
**Input:** nums = [3,2,6,1,4]
33+
34+
**Output:** 2
35+
36+
**Explanation:** We perform the following operations:
37+
- Delete the first two elements, with score 3 + 2 = 5, nums = [6,1,4].
38+
- Delete the last two elements, with score 1 + 4 = 5, nums = [6].
39+
40+
It can be proven that we can perform at most 2 operations.
41+
42+
**Constraints:**
43+
44+
* `2 <= nums.length <= 2000`
45+
* `1 <= nums[i] <= 1000`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g3001_3100.s3047_find_the_largest_area_of_square_inside_two_rectangles;
2+
3+
// #Medium #Array #Math #Geometry #2024_03_04_Time_50_ms_(97.63%)_Space_44.7_MB_(98.11%)
4+
5+
public class Solution {
6+
public long largestSquareArea(int[][] bottomLeft, int[][] topRight) {
7+
int n = bottomLeft.length;
8+
long maxArea = 0;
9+
for (int i = 0; i < n; i++) {
10+
int ax = bottomLeft[i][0];
11+
int ay = bottomLeft[i][1];
12+
int bx = topRight[i][0];
13+
int by = topRight[i][1];
14+
for (int j = i + 1; j < n; j++) {
15+
int cx = bottomLeft[j][0];
16+
int cy = bottomLeft[j][1];
17+
int dx = topRight[j][0];
18+
int dy = topRight[j][1];
19+
int x1 = Math.max(ax, cx);
20+
int y1 = Math.max(ay, cy);
21+
int x2 = Math.min(bx, dx);
22+
int y2 = Math.min(by, dy);
23+
int minSide = Math.min(x2 - x1, y2 - y1);
24+
long area = (long) Math.pow(Math.max(minSide, 0), 2);
25+
maxArea = Math.max(maxArea, area);
26+
}
27+
}
28+
return maxArea;
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
3047\. Find the Largest Area of Square Inside Two Rectangles
2+
3+
Medium
4+
5+
There exist `n` rectangles in a 2D plane. You are given two **0-indexed** 2D integer arrays `bottomLeft` and `topRight`, both of size `n x 2`, where `bottomLeft[i]` and `topRight[i]` represent the **bottom-left** and **top-right** coordinates of the <code>i<sup>th</sup></code> rectangle respectively.
6+
7+
You can select a region formed from the **intersection** of two of the given rectangles. You need to find the **largest** area of a **square** that can fit **inside** this region if you select the region optimally.
8+
9+
Return _the **largest** possible area of a square, or_ `0` _if there **do not** exist any intersecting regions between the rectangles_.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2024/01/05/example12.png)
14+
15+
**Input:** bottomLeft = [[1,1],[2,2],[3,1]], topRight = [[3,3],[4,4],[6,6]]
16+
17+
**Output:** 1
18+
19+
**Explanation:** A square with side length 1 can fit inside either the intersecting region of rectangle 0 and rectangle 1, or the intersecting region of rectangle 1 and rectangle 2. Hence the largest area is side \* side which is 1 \* 1 == 1.
20+
21+
It can be shown that a square with a greater side length can not fit inside any intersecting region.
22+
23+
**Example 2:**
24+
25+
![](https://assets.leetcode.com/uploads/2024/01/04/rectanglesexample2.png)
26+
27+
**Input:** bottomLeft = [[1,1],[2,2],[1,2]], topRight = [[3,3],[4,4],[3,4]]
28+
29+
**Output:** 1
30+
31+
**Explanation:** A square with side length 1 can fit inside either the intersecting region of rectangle 0 and rectangle 1, the intersecting region of rectangle 1 and rectangle 2, or the intersection region of all 3 rectangles. Hence the largest area is side \* side which is 1 \* 1 == 1.
32+
33+
It can be shown that a square with a greater side length can not fit inside any intersecting region. Note that the region can be formed by the intersection of more than 2 rectangles.
34+
35+
**Example 3:**
36+
37+
![](https://assets.leetcode.com/uploads/2024/01/04/rectanglesexample3.png)
38+
39+
**Input:** bottomLeft = [[1,1],[3,3],[3,1]], topRight = [[2,2],[4,4],[4,2]]
40+
41+
**Output:** 0
42+
43+
**Explanation:** No pair of rectangles intersect, hence, we return 0.
44+
45+
**Constraints:**
46+
47+
* `n == bottomLeft.length == topRight.length`
48+
* <code>2 <= n <= 10<sup>3</sup></code>
49+
* `bottomLeft[i].length == topRight[i].length == 2`
50+
* <code>1 <= bottomLeft[i][0], bottomLeft[i][1] <= 10<sup>7</sup></code>
51+
* <code>1 <= topRight[i][0], topRight[i][1] <= 10<sup>7</sup></code>
52+
* `bottomLeft[i][0] < topRight[i][0]`
53+
* `bottomLeft[i][1] < topRight[i][1]`

0 commit comments

Comments
 (0)