Skip to content

Commit ed531fa

Browse files
authored
Added tasks 3038-3049
1 parent 6468fc0 commit ed531fa

File tree

18 files changed

+805
-0
lines changed

18 files changed

+805
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g3001_3100.s3038_maximum_number_of_operations_with_the_same_score_i
2+
3+
// #Easy #Array #Simulation #2024_03_06_Time_142_ms_(100.00%)_Space_34.9_MB_(57.78%)
4+
5+
class Solution {
6+
fun maxOperations(nums: IntArray): Int {
7+
var c = 1
8+
var i = 2
9+
val s = nums[0] + nums[1]
10+
val l = nums.size - (if (nums.size % 2 == 0) 0 else 1)
11+
while (i < l) {
12+
if (nums[i] + nums[i + 1] == s) {
13+
c++
14+
} else {
15+
break
16+
}
17+
i += 2
18+
}
19+
return c
20+
}
21+
}
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,27 @@
1+
package g3001_3100.s3039_apply_operations_to_make_string_empty
2+
3+
// #Medium #Array #Hash_Table #Sorting #Counting
4+
// #2024_03_06_Time_335_ms_(97.73%)_Space_49.8_MB_(81.82%)
5+
6+
import kotlin.math.max
7+
8+
class Solution {
9+
fun lastNonEmptyString(s: String): String {
10+
val freq = IntArray(26)
11+
val ar = s.toCharArray()
12+
val n = ar.size
13+
var max = 1
14+
val sb = StringBuilder()
15+
for (c in ar) {
16+
freq[c.code - 'a'.code]++
17+
max = max(freq[c.code - 'a'.code].toDouble(), max.toDouble()).toInt()
18+
}
19+
for (i in n - 1 downTo 0) {
20+
if (freq[ar[i].code - 'a'.code] == max) {
21+
sb.append(ar[i])
22+
freq[ar[i].code - 'a'.code] = 0
23+
}
24+
}
25+
return sb.reverse().toString()
26+
}
27+
}
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,71 @@
1+
package g3001_3100.s3040_maximum_number_of_operations_with_the_same_score_ii
2+
3+
// #Medium #Array #Dynamic_Programming #Memoization
4+
// #2024_03_06_Time_179_ms_(100.00%)_Space_38.4_MB_(100.00%)
5+
6+
import java.util.Objects
7+
import kotlin.math.max
8+
9+
class Solution {
10+
private lateinit var nums: IntArray
11+
12+
private var maxOps = 1
13+
14+
private val dp: MutableMap<Pos, Int> = HashMap()
15+
16+
private class Pos(var start: Int, var end: Int, var sum: Int) {
17+
override fun equals(other: Any?): Boolean {
18+
if (other == null) {
19+
return false
20+
}
21+
if (other !is Pos) {
22+
return false
23+
}
24+
return start == other.start && end == other.end && sum == other.sum
25+
}
26+
27+
override fun hashCode(): Int {
28+
return Objects.hash(start, end, sum)
29+
}
30+
}
31+
32+
fun maxOperations(nums: IntArray): Int {
33+
this.nums = nums
34+
val length = nums.size
35+
36+
maxOperations(2, length - 1, nums[0] + nums[1], 1)
37+
maxOperations(0, length - 3, nums[length - 2] + nums[length - 1], 1)
38+
maxOperations(1, length - 2, nums[0] + nums[length - 1], 1)
39+
40+
return maxOps
41+
}
42+
43+
private fun maxOperations(start: Int, end: Int, sum: Int, nOps: Int) {
44+
if (start >= end) {
45+
return
46+
}
47+
48+
if ((((end - start) / 2) + nOps) < maxOps) {
49+
return
50+
}
51+
52+
val pos = Pos(start, end, sum)
53+
val posNops = dp[pos]
54+
if (posNops != null && posNops >= nOps) {
55+
return
56+
}
57+
dp[pos] = nOps
58+
if (nums[start] + nums[start + 1] == sum) {
59+
maxOps = max(maxOps, (nOps + 1))
60+
maxOperations(start + 2, end, sum, nOps + 1)
61+
}
62+
if (nums[end - 1] + nums[end] == sum) {
63+
maxOps = max(maxOps, (nOps + 1))
64+
maxOperations(start, end - 2, sum, nOps + 1)
65+
}
66+
if (nums[start] + nums[end] == sum) {
67+
maxOps = max(maxOps, (nOps + 1))
68+
maxOperations(start + 1, end - 1, sum, nOps + 1)
69+
}
70+
}
71+
}
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,34 @@
1+
package g3001_3100.s3047_find_the_largest_area_of_square_inside_two_rectangles
2+
3+
// #Medium #Array #Math #Geometry #2024_03_06_Time_753_ms_(40.42%)_Space_57.7_MB_(72.34%)
4+
5+
import kotlin.math.max
6+
import kotlin.math.min
7+
import kotlin.math.pow
8+
9+
class Solution {
10+
fun largestSquareArea(bottomLeft: Array<IntArray>, topRight: Array<IntArray>): Long {
11+
val n = bottomLeft.size
12+
var maxArea: Long = 0
13+
for (i in 0 until n) {
14+
val ax = bottomLeft[i][0]
15+
val ay = bottomLeft[i][1]
16+
val bx = topRight[i][0]
17+
val by = topRight[i][1]
18+
for (j in i + 1 until n) {
19+
val cx = bottomLeft[j][0]
20+
val cy = bottomLeft[j][1]
21+
val dx = topRight[j][0]
22+
val dy = topRight[j][1]
23+
val x1 = max(ax, cx)
24+
val y1 = max(ay, cy)
25+
val x2 = min(bx, dx)
26+
val y2 = min(by, dy)
27+
val minSide = min((x2 - x1), (y2 - y1))
28+
val area = max(minSide.toDouble(), 0.0).pow(2.0).toLong()
29+
maxArea = max(maxArea, area)
30+
}
31+
}
32+
return maxArea
33+
}
34+
}
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]`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package g3001_3100.s3048_earliest_second_to_mark_indices_i
2+
3+
// #Medium #Array #Binary_Search #2024_03_06_Time_223_ms_(75.00%)_Space_44.7_MB_(33.33%)
4+
5+
class Solution {
6+
fun earliestSecondToMarkIndices(nums: IntArray, changeIndices: IntArray): Int {
7+
val n = nums.size
8+
if (nums.isEmpty() || changeIndices.isEmpty()) {
9+
return 0
10+
}
11+
val last = IntArray(n)
12+
last.fill(-1)
13+
for (i in changeIndices.indices) {
14+
changeIndices[i] -= 1
15+
}
16+
var low = 0
17+
var high = changeIndices.size - 1
18+
while (low < high) {
19+
val mid = low + (high - low) / 2
20+
if (isPossible(mid, nums, changeIndices, last)) {
21+
high = mid
22+
} else {
23+
low = mid + 1
24+
}
25+
}
26+
return if (isPossible(low, nums, changeIndices, last)) low + 1 else -1
27+
}
28+
29+
private fun isPossible(s: Int, nums: IntArray, changeIndices: IntArray, last: IntArray): Boolean {
30+
val n = nums.size
31+
last.fill(-1)
32+
for (i in 0..s) {
33+
last[changeIndices[i]] = i
34+
}
35+
var marked = 0
36+
var operations = 0
37+
for (i in 0..s) {
38+
if (i == last[changeIndices[i]]) {
39+
if (nums[changeIndices[i]] > operations) {
40+
return false
41+
}
42+
operations -= nums[changeIndices[i]]
43+
marked++
44+
} else {
45+
operations++
46+
}
47+
}
48+
return marked == n
49+
}
50+
}

0 commit comments

Comments
 (0)