Skip to content
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
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1821,6 +1821,30 @@
| 2798 |[Number of Employees Who Met the Target](src/main/kotlin/g2701_2800/s2798_number_of_employees_who_met_the_target)| Easy | Array, Enumeration | 153 | 92.50
| 2791 |[Count Paths That Can Form a Palindrome in a Tree](src/main/kotlin/g2701_2800/s2791_count_paths_that_can_form_a_palindrome_in_a_tree)| Hard | Dynamic_Programming, Tree, Bit_Manipulation, Bitmask, Depth_First_Search | 683 | 100.00
| 2790 |[Maximum Number of Groups With Increasing Length](src/main/kotlin/g2701_2800/s2790_maximum_number_of_groups_with_increasing_length)| Hard | Array, Math, Sorting, Greedy, Binary_Search | 545 | 100.00
| 2789 |[Largest Element in an Array after Merge Operations](src/main/kotlin/g2701_2800/s2789_largest_element_in_an_array_after_merge_operations)| Medium | Array, Greedy, Prefix_Sum | 683 | 73.68
| 2788 |[Split Strings by Separator](src/main/kotlin/g2701_2800/s2788_split_strings_by_separator)| Easy | Array, String | 314 | 85.45
| 2787 |[Ways to Express an Integer as Sum of Powers](src/main/kotlin/g2701_2800/s2787_ways_to_express_an_integer_as_sum_of_powers)| Medium | Dynamic_Programming | 152 | 100.00
| 2786 |[Visit Array Positions to Maximize Score](src/main/kotlin/g2701_2800/s2786_visit_array_positions_to_maximize_score)| Medium | Array, Dynamic_Programming | 625 | 84.00
| 2785 |[Sort Vowels in a String](src/main/kotlin/g2701_2800/s2785_sort_vowels_in_a_string)| Medium | String, Sorting | 233 | 100.00
| 2784 |[Check if Array is Good](src/main/kotlin/g2701_2800/s2784_check_if_array_is_good)| Easy | Array, Hash_Table, Sorting | 177 | 88.89
| 2781 |[Length of the Longest Valid Substring](src/main/kotlin/g2701_2800/s2781_length_of_the_longest_valid_substring)| Hard | Array, String, Hash_Table, Sliding_Window | 647 | 100.00
| 2780 |[Minimum Index of a Valid Split](src/main/kotlin/g2701_2800/s2780_minimum_index_of_a_valid_split)| Medium | Array, Hash_Table, Sorting | 640 | 91.67
| 2779 |[Maximum Beauty of an Array After Applying Operation](src/main/kotlin/g2701_2800/s2779_maximum_beauty_of_an_array_after_applying_operation)| Medium | Array, Sorting, Binary_Search, Sliding_Window | 649 | 96.97
| 2778 |[Sum of Squares of Special Elements](src/main/kotlin/g2701_2800/s2778_sum_of_squares_of_special_elements)| Easy | Array, Simulation | 183 | 86.44
| 2772 |[Apply Operations to Make All Array Elements Equal to Zero](src/main/kotlin/g2701_2800/s2772_apply_operations_to_make_all_array_elements_equal_to_zero)| Medium | Array, Prefix_Sum | 578 | 97.06
| 2771 |[Longest Non-decreasing Subarray From Two Arrays](src/main/kotlin/g2701_2800/s2771_longest_non_decreasing_subarray_from_two_arrays)| Medium | Array, Dynamic_Programming | 665 | 96.88
| 2770 |[Maximum Number of Jumps to Reach the Last Index](src/main/kotlin/g2701_2800/s2770_maximum_number_of_jumps_to_reach_the_last_index)| Medium | Array, Dynamic_Programming | 325 | 51.16
| 2769 |[Find the Maximum Achievable Number](src/main/kotlin/g2701_2800/s2769_find_the_maximum_achievable_number)| Easy | Math | 134 | 97.89
| 2768 |[Number of Black Blocks](src/main/kotlin/g2701_2800/s2768_number_of_black_blocks)| Medium | Array, Hash_Table, Enumeration | 719 | 100.00
| 2767 |[Partition String Into Minimum Beautiful Substrings](src/main/kotlin/g2701_2800/s2767_partition_string_into_minimum_beautiful_substrings)| Medium | String, Hash_Table, Dynamic_Programming, Backtracking | 162 | 96.00
| 2766 |[Relocate Marbles](src/main/kotlin/g2701_2800/s2766_relocate_marbles)| Medium | Array, Hash_Table, Sorting, Simulation | 1038 | 100.00
| 2765 |[Longest Alternating Subarray](src/main/kotlin/g2701_2800/s2765_longest_alternating_subarray)| Easy | Array, Enumeration | 191 | 97.92
| 2763 |[Sum of Imbalance Numbers of All Subarrays](src/main/kotlin/g2701_2800/s2763_sum_of_imbalance_numbers_of_all_subarrays)| Hard | Array, Hash_Table, Ordered_Set | 417 | 95.24
| 2762 |[Continuous Subarrays](src/main/kotlin/g2701_2800/s2762_continuous_subarrays)| Medium | Array, Heap_Priority_Queue, Sliding_Window, Ordered_Set, Queue, Monotonic_Queue | 492 | 100.00
| 2761 |[Prime Pairs With Target Sum](src/main/kotlin/g2701_2800/s2761_prime_pairs_with_target_sum)| Medium | Array, Math, Enumeration, Number_Theory | 537 | 100.00
| 2760 |[Longest Even Odd Subarray With Threshold](src/main/kotlin/g2701_2800/s2760_longest_even_odd_subarray_with_threshold)| Easy | Array, Sliding_Window | 285 | 95.45
| 2751 |[Robot Collisions](src/main/kotlin/g2701_2800/s2751_robot_collisions)| Hard | Array, Sorting, Stack, Simulation | 1049 | 100.00
| 2750 |[Ways to Split Array Into Good Subarrays](src/main/kotlin/g2701_2800/s2750_ways_to_split_array_into_good_subarrays)| Medium | Array, Dynamic_Programming, Math | 916 | 100.00
| 2749 |[Minimum Operations to Make the Integer Zero](src/main/kotlin/g2701_2800/s2749_minimum_operations_to_make_the_integer_zero)| Medium | Bit_Manipulation, Brainteaser | 132 | 91.67
| 2748 |[Number of Beautiful Pairs](src/main/kotlin/g2701_2800/s2748_number_of_beautiful_pairs)| Easy | Array, Math, Number_Theory | 227 | 100.00
| 2747 |[Count Zero Request Servers](src/main/kotlin/g2701_2800/s2747_count_zero_request_servers)| Medium | Array, Hash_Table, Sorting, Sliding_Window | 991 | 100.00
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)

## 2750\. Ways to Split Array Into Good Subarrays

Medium

You are given a binary array `nums`.

A subarray of an array is **good** if it contains **exactly** **one** element with the value `1`.

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>.

A subarray is a contiguous **non-empty** sequence of elements within an array.

**Example 1:**

**Input:** nums = [0,1,0,0,1]

**Output:** 3

**Explanation:** There are 3 ways to split nums into good subarrays:
- \[0,1] [0,0,1]
- \[0,1,0] [0,1]
- \[0,1,0,0] [1]

**Example 2:**

**Input:** nums = [0,1,0]

**Output:** 1

**Explanation:** There is 1 way to split nums into good subarrays: - [0,1,0]

**Constraints:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* `0 <= nums[i] <= 1`

## Solution

```kotlin
class Solution {
fun numberOfGoodSubarraySplits(nums: IntArray): Int {
val res: MutableList<Int> = ArrayList()
val modulo = 1000000007L
for (i in nums.indices) {
if (nums[i] == 1) res.add(i)
}
var ans: Long = 0
if (res.isNotEmpty()) ans = 1
var kanishk = ans
for (i in res.size - 2 downTo 0) {
val leftInd = res[i]
val rightInd = res[i + 1]
val df = rightInd - leftInd
val mul = df.toLong() % modulo * kanishk % modulo % modulo
kanishk = mul
ans = mul
}
return ans.toInt()
}
}
```
126 changes: 126 additions & 0 deletions src/main/kotlin/g2701_2800/s2751_robot_collisions/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)

## 2751\. Robot Collisions

Hard

There are `n` **1-indexed** robots, each having a position on a line, health, and movement direction.

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**.

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**.

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.

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.

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._

**Note:** The positions may be unsorted.

**Example 1:**

![](https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png)

**Input:** positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = "RRRRR"

**Output:** [2,17,9,15,10]

**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].

**Example 2:**

![](https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png)

**Input:** positions = [3,5,2,6], healths = [10,10,15,12], directions = "RLRL"

**Output:** [14]

**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].

**Example 3:**

![](https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png)

**Input:** positions = [1,2,5,6], healths = [10,10,11,11], directions = "RLRL"

**Output:** []

**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, [].

**Constraints:**

* <code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code>
* <code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code>
* `directions[i] == 'L'` or `directions[i] == 'R'`
* All values in `positions` are distinct

## Solution

```kotlin
import java.util.ArrayDeque

class Solution {
fun survivedRobotsHealths(pos: IntArray, h: IntArray, dir: String): List<Int> {
val a = Array(pos.size) { IntArray(4) { 0 } }
for (i in pos.indices) {
a[i][0] = pos[i]
a[i][1] = h[i]
a[i][2] = if (dir[i] == 'R') 1 else 0
a[i][3] = i
}
a.sortWith(compareBy { it[0] })
val q = ArrayDeque<Int>()
for (i in a.indices) {
if (q.isEmpty() || a[i][2] == 1) {
q.push(i)
} else {
var prev = a[q.peek()]
if (prev[2] == 1) {
if (a[i][1] == prev[1]) {
q.pop()
continue
} else {
while (true) {
if (a[i][1] == prev[1]) {
q.pop()
break
}
if (prev[1] > a[i][1]) {
prev[1] -= 1
break
} else {
q.pop()
a[i][1] -= 1
if (q.isEmpty() || a[q.peek()][2] == 0) {
q.push(i)
break
} else {
prev = a[q.peek()]
}
}
}
}
} else {
q.push(i)
}
}
}
val b = Array(q.size) { IntArray(2) { 0 } }
var j = 0
while (q.isNotEmpty()) {
val n = q.pop()
b[j][0] = a[n][1]
b[j][1] = a[n][3]
j++
}
b.sortWith(compareBy { it[1] })
val res = mutableListOf<Int>()
for (element in b) {
res.add(element[0])
}
return res
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)

## 2760\. Longest Even Odd Subarray With Threshold

Easy

You are given a **0-indexed** integer array `nums` and an integer `threshold`.

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:

* `nums[l] % 2 == 0`
* For all indices `i` in the range `[l, r - 1]`, `nums[i] % 2 != nums[i + 1] % 2`
* For all indices `i` in the range `[l, r]`, `nums[i] <= threshold`

Return _an integer denoting the length of the longest such subarray._

**Note:** A **subarray** is a contiguous non-empty sequence of elements within an array.

**Example 1:**

**Input:** nums = [3,2,5,4], threshold = 5

**Output:** 3

**Explanation:**

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.

Hence, the answer is the length of the subarray, 3. We can show that 3 is the maximum possible achievable length.

**Example 2:**

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

**Output:** 1

**Explanation:**

In this example, we can select the subarray that starts at l = 1 and ends at r = 1 => [2].

It satisfies all the conditions and we can show that 1 is the maximum possible achievable length.

**Example 3:**

**Input:** nums = [2,3,4,5], threshold = 4

**Output:** 3

**Explanation:**

In this example, we can select the subarray that starts at l = 0 and ends at r = 2 => [2,3,4].

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.

**Constraints:**

* `1 <= nums.length <= 100`
* `1 <= nums[i] <= 100`
* `1 <= threshold <= 100`

## Solution

```kotlin
class Solution {
fun longestAlternatingSubarray(nums: IntArray, threshold: Int): Int {
var maxLength = 0
var i = 0
while (i < nums.size) {
if (nums[i] % 2 == 0 && nums[i] <= threshold) {
var length = 1
var j = i + 1
while (j < nums.size &&
nums[j] <= threshold &&
nums[j] % 2 != nums[j - 1] % 2
) {
length++
j++
}
maxLength = maxLength.coerceAtLeast(length)
i = j - 1
}
i++
}
return maxLength
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)

## 2761\. Prime Pairs With Target Sum

Medium

You are given an integer `n`. We say that two integers `x` and `y` form a prime number pair if:

* `1 <= x <= y <= n`
* `x + y == n`
* `x` and `y` are prime numbers

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_.

**Note:** A prime number is a natural number greater than `1` with only two factors, itself and `1`.

**Example 1:**

**Input:** n = 10

**Output:** [[3,7],[5,5]]

**Explanation:**

In this example, there are two prime pairs that satisfy the criteria.

These pairs are [3,7] and [5,5], and we return them in the sorted order as described in the problem statement.

**Example 2:**

**Input:** n = 2

**Output:** []

**Explanation:**

We can show that there is no prime number pair that gives a sum of 2, so we return an empty array.

**Constraints:**

* <code>1 <= n <= 10<sup>6</sup></code>

## Solution

```kotlin
class Solution {
fun findPrimePairs(n: Int): List<List<Int>> {
val answer: MutableList<List<Int>> = ArrayList()
for (a in list) {
val other = n - a
if (other < n / 2 || a > n / 2) break
if (primes.contains(other)) answer.add(listOf(a, other))
}
return answer
}

companion object {
private val primes: HashSet<Int> = HashSet()
private val list: MutableList<Int> = ArrayList()

init {
val m = 1000001
val visited = BooleanArray(m)
for (i in 2 until m) {
if (!visited[i]) {
primes.add(i)
list.add(i)
var j: Int = i
while (j < m) {
visited[j] = true
j += i
}
}
}
}
}
}
```
Loading