Skip to content

Commit 8302ae6

Browse files
authored
Added tasks 2750-2762
1 parent 2725ffc commit 8302ae6

File tree

15 files changed

+550
-0
lines changed

15 files changed

+550
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g2701_2800.s2750_ways_to_split_array_into_good_subarrays
2+
3+
// #Medium #Array #Dynamic_Programming #Math
4+
// #2023_08_09_Time_916_ms_(100.00%)_Space_71.5_MB_(76.32%)
5+
6+
class Solution {
7+
fun numberOfGoodSubarraySplits(nums: IntArray): Int {
8+
val res: MutableList<Int> = ArrayList()
9+
val modulo = 1000000007L
10+
for (i in nums.indices) {
11+
if (nums[i] == 1) res.add(i)
12+
}
13+
var ans: Long = 0
14+
if (res.isNotEmpty()) ans = 1
15+
var kanishk = ans
16+
for (i in res.size - 2 downTo 0) {
17+
val leftInd = res[i]
18+
val rightInd = res[i + 1]
19+
val df = rightInd - leftInd
20+
val mul = df.toLong() % modulo * kanishk % modulo % modulo
21+
kanishk = mul
22+
ans = mul
23+
}
24+
return ans.toInt()
25+
}
26+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2750\. Ways to Split Array Into Good Subarrays
2+
3+
Medium
4+
5+
You are given a binary array `nums`.
6+
7+
A subarray of an array is **good** if it contains **exactly** **one** element with the value `1`.
8+
9+
Return _an integer denoting the number of ways to split the array_ `nums` _into **good** subarrays_. As the number may be too large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
10+
11+
A subarray is a contiguous **non-empty** sequence of elements within an array.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [0,1,0,0,1]
16+
17+
**Output:** 3
18+
19+
**Explanation:** There are 3 ways to split nums into good subarrays:
20+
- [0,1] [0,0,1]
21+
- [0,1,0] [0,1]
22+
- [0,1,0,0] [1]
23+
24+
**Example 2:**
25+
26+
**Input:** nums = [0,1,0]
27+
28+
**Output:** 1
29+
30+
**Explanation:** There is 1 way to split nums into good subarrays: - [0,1,0]
31+
32+
**Constraints:**
33+
34+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
35+
* `0 <= nums[i] <= 1`
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package g2701_2800.s2751_robot_collisions
2+
3+
// #Hard #Array #Sorting #Stack #Simulation
4+
// #2023_08_09_Time_1049_ms_(100.00%)_Space_78.2_MB_(66.67%)
5+
6+
import java.util.ArrayDeque
7+
8+
class Solution {
9+
fun survivedRobotsHealths(pos: IntArray, h: IntArray, dir: String): List<Int> {
10+
val a = Array(pos.size) { IntArray(4) { 0 } }
11+
for (i in pos.indices) {
12+
a[i][0] = pos[i]
13+
a[i][1] = h[i]
14+
a[i][2] = if (dir[i] == 'R') 1 else 0
15+
a[i][3] = i
16+
}
17+
a.sortWith(compareBy { it[0] })
18+
val q = ArrayDeque<Int>()
19+
for (i in a.indices) {
20+
if (q.isEmpty() || a[i][2] == 1) {
21+
q.push(i)
22+
} else {
23+
var prev = a[q.peek()]
24+
if (prev[2] == 1) {
25+
if (a[i][1] == prev[1]) {
26+
q.pop()
27+
continue
28+
} else {
29+
while (true) {
30+
if (a[i][1] == prev[1]) {
31+
q.pop()
32+
break
33+
}
34+
if (prev[1] > a[i][1]) {
35+
prev[1] -= 1
36+
break
37+
} else {
38+
q.pop()
39+
a[i][1] -= 1
40+
if (q.isEmpty() || a[q.peek()][2] == 0) {
41+
q.push(i)
42+
break
43+
} else {
44+
prev = a[q.peek()]
45+
}
46+
}
47+
}
48+
}
49+
} else {
50+
q.push(i)
51+
}
52+
}
53+
}
54+
val b = Array(q.size) { IntArray(2) { 0 } }
55+
var j = 0
56+
while (q.isNotEmpty()) {
57+
val n = q.pop()
58+
b[j][0] = a[n][1]
59+
b[j][1] = a[n][3]
60+
j++
61+
}
62+
b.sortWith(compareBy { it[1] })
63+
val res = mutableListOf<Int>()
64+
for (element in b) {
65+
res.add(element[0])
66+
}
67+
return res
68+
}
69+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2751\. Robot Collisions
2+
3+
Hard
4+
5+
There are `n` **1-indexed** robots, each having a position on a line, health, and movement direction.
6+
7+
You are given **0-indexed** integer arrays `positions`, `healths`, and a string `directions` (`directions[i]` is either **'L'** for **left** or **'R'** for **right**). All integers in `positions` are **unique**.
8+
9+
All robots start moving on the line **simultaneously** at the **same speed** in their given directions. If two robots ever share the same position while moving, they will **collide**.
10+
11+
If two robots collide, the robot with **lower health** is **removed** from the line, and the health of the other robot **decreases** **by one**. The surviving robot continues in the **same** direction it was going. If both robots have the **same** health, they are both removed from the line.
12+
13+
Your task is to determine the **health** of the robots that survive the collisions, in the same **order** that the robots were given, i.e. final heath of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.
14+
15+
Return _an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur._
16+
17+
**Note:** The positions may be unsorted.
18+
19+
**Example 1:**
20+
21+
![](https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png)
22+
23+
**Input:** positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = "RRRRR"
24+
25+
**Output:** [2,17,9,15,10]
26+
27+
**Explanation:** No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10].
28+
29+
**Example 2:**
30+
31+
![](https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png)
32+
33+
**Input:** positions = [3,5,2,6], healths = [10,10,15,12], directions = "RLRL"
34+
35+
**Output:** [14]
36+
37+
**Explanation:** There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4's health is smaller, it gets removed, and robot 3's health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14].
38+
39+
**Example 3:**
40+
41+
![](https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png)
42+
43+
**Input:** positions = [1,2,5,6], healths = [10,10,11,11], directions = "RLRL"
44+
45+
**Output:** []
46+
47+
**Explanation:** Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].
48+
49+
**Constraints:**
50+
51+
* <code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code>
52+
* <code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code>
53+
* `directions[i] == 'L'` or `directions[i] == 'R'`
54+
* All values in `positions` are distinct
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g2701_2800.s2760_longest_even_odd_subarray_with_threshold
2+
3+
// #Easy #Array #Sliding_Window #2023_08_09_Time_285_ms_(95.45%)_Space_46.8_MB_(31.82%)
4+
5+
class Solution {
6+
fun longestAlternatingSubarray(nums: IntArray, threshold: Int): Int {
7+
var maxLength = 0
8+
var i = 0
9+
while (i < nums.size) {
10+
if (nums[i] % 2 == 0 && nums[i] <= threshold) {
11+
var length = 1
12+
var j = i + 1
13+
while (j < nums.size &&
14+
nums[j] <= threshold &&
15+
nums[j] % 2 != nums[j - 1] % 2
16+
) {
17+
length++
18+
j++
19+
}
20+
maxLength = maxLength.coerceAtLeast(length)
21+
i = j - 1
22+
}
23+
i++
24+
}
25+
return maxLength
26+
}
27+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2760\. Longest Even Odd Subarray With Threshold
2+
3+
Easy
4+
5+
You are given a **0-indexed** integer array `nums` and an integer `threshold`.
6+
7+
Find the length of the **longest subarray** of `nums` starting at index `l` and ending at index `r` `(0 <= l <= r < nums.length)` that satisfies the following conditions:
8+
9+
* `nums[l] % 2 == 0`
10+
* For all indices `i` in the range `[l, r - 1]`, `nums[i] % 2 != nums[i + 1] % 2`
11+
* For all indices `i` in the range `[l, r]`, `nums[i] <= threshold`
12+
13+
Return _an integer denoting the length of the longest such subarray._
14+
15+
**Note:** A **subarray** is a contiguous non-empty sequence of elements within an array.
16+
17+
**Example 1:**
18+
19+
**Input:** nums = [3,2,5,4], threshold = 5
20+
21+
**Output:** 3
22+
23+
**Explanation:**
24+
25+
In this example, we can select the subarray that starts at l = 1 and ends at r = 3 => [2,5,4]. This subarray satisfies the conditions.
26+
27+
Hence, the answer is the length of the subarray, 3. We can show that 3 is the maximum possible achievable length.
28+
29+
**Example 2:**
30+
31+
**Input:** nums = [1,2], threshold = 2
32+
33+
**Output:** 1
34+
35+
**Explanation:**
36+
37+
In this example, we can select the subarray that starts at l = 1 and ends at r = 1 => [2].
38+
39+
It satisfies all the conditions and we can show that 1 is the maximum possible achievable length.
40+
41+
**Example 3:**
42+
43+
**Input:** nums = [2,3,4,5], threshold = 4
44+
45+
**Output:** 3
46+
47+
**Explanation:**
48+
49+
In this example, we can select the subarray that starts at l = 0 and ends at r = 2 => [2,3,4].
50+
51+
It satisfies all the conditions. Hence, the answer is the length of the subarray, 3. We can show that 3 is the maximum possible achievable length.
52+
53+
**Constraints:**
54+
55+
* `1 <= nums.length <= 100`
56+
* `1 <= nums[i] <= 100`
57+
* `1 <= threshold <= 100`
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g2701_2800.s2761_prime_pairs_with_target_sum
2+
3+
// #Medium #Array #Math #Enumeration #Number_Theory
4+
// #2023_08_10_Time_537_ms_(100.00%)_Space_54.2_MB_(46.15%)
5+
6+
class Solution {
7+
fun findPrimePairs(n: Int): List<List<Int>> {
8+
val answer: MutableList<List<Int>> = ArrayList()
9+
for (a in list) {
10+
val other = n - a
11+
if (other < n / 2 || a > n / 2) break
12+
if (primes.contains(other)) answer.add(listOf(a, other))
13+
}
14+
return answer
15+
}
16+
17+
companion object {
18+
private val primes: HashSet<Int> = HashSet()
19+
private val list: MutableList<Int> = ArrayList()
20+
21+
init {
22+
val m = 1000001
23+
val visited = BooleanArray(m)
24+
for (i in 2 until m) {
25+
if (!visited[i]) {
26+
primes.add(i)
27+
list.add(i)
28+
var j: Int = i
29+
while (j < m) {
30+
visited[j] = true
31+
j += i
32+
}
33+
}
34+
}
35+
}
36+
}
37+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2761\. Prime Pairs With Target Sum
2+
3+
Medium
4+
5+
You are given an integer `n`. We say that two integers `x` and `y` form a prime number pair if:
6+
7+
* `1 <= x <= y <= n`
8+
* `x + y == n`
9+
* `x` and `y` are prime numbers
10+
11+
Return _the 2D sorted list of prime number pairs_ <code>[x<sub>i</sub>, y<sub>i</sub>]</code>. The list should be sorted in **increasing** order of <code>x<sub>i</sub></code>. If there are no prime number pairs at all, return _an empty array_.
12+
13+
**Note:** A prime number is a natural number greater than `1` with only two factors, itself and `1`.
14+
15+
**Example 1:**
16+
17+
**Input:** n = 10
18+
19+
**Output:** [[3,7],[5,5]]
20+
21+
**Explanation:**
22+
23+
In this example, there are two prime pairs that satisfy the criteria.
24+
25+
These pairs are [3,7] and [5,5], and we return them in the sorted order as described in the problem statement.
26+
27+
**Example 2:**
28+
29+
**Input:** n = 2
30+
31+
**Output:** []
32+
33+
**Explanation:**
34+
35+
We can show that there is no prime number pair that gives a sum of 2, so we return an empty array.
36+
37+
**Constraints:**
38+
39+
* <code>1 <= n <= 10<sup>6</sup></code>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g2701_2800.s2762_continuous_subarrays
2+
3+
// #Medium #Array #Heap_Priority_Queue #Sliding_Window #Ordered_Set #Queue #Monotonic_Queue
4+
// #2023_08_10_Time_492_ms_(100.00%)_Space_62.4_MB_(84.62%)
5+
6+
import java.util.TreeMap
7+
8+
class Solution {
9+
fun continuousSubarrays(nums: IntArray): Long {
10+
var left = 0
11+
var right = 0
12+
var total = 0L
13+
val tree = TreeMap<Int, Int>()
14+
val n = nums.size
15+
while (right < n) {
16+
if (!tree.containsKey(nums[right])) {
17+
tree[nums[right]] = 0
18+
}
19+
tree[nums[right]] = tree[nums[right]]!! + 1
20+
while (kotlin.math.abs(tree.lastKey() - nums[right]) > 2 || Math.abs(tree.firstKey() - nums[right]) > 2) {
21+
val keyL = nums[left]
22+
tree[keyL] = tree[keyL]!! - 1
23+
if (tree[keyL] == 0) tree.remove(keyL)
24+
left++
25+
}
26+
total += right - left + 1
27+
right++
28+
}
29+
return total
30+
}
31+
}

0 commit comments

Comments
 (0)