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
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1816,7 +1816,32 @@

| # | Title | Difficulty | Tag | Time, ms | Time, %
|------|----------------|-------------|-------------|----------|--------
| 2824 |[Count Pairs Whose Sum is Less than Target](src/main/kotlin/g2801_2900/s2824_count_pairs_whose_sum_is_less_than_target)| Easy | Array, Sorting | s | s
| 2856 |[Minimum Array Length After Pair Removals](src/main/kotlin/g2801_2900/s2856_minimum_array_length_after_pair_removals)| Medium | Array, Hash_Table, Greedy, Binary_Search, Two_Pointers, Counting | 647 | 100.00
| 2855 |[Minimum Right Shifts to Sort the Array](src/main/kotlin/g2801_2900/s2855_minimum_right_shifts_to_sort_the_array)| Easy | Array | 169 | 75.00
| 2851 |[String Transformation](src/main/kotlin/g2801_2900/s2851_string_transformation)| Hard | String, Dynamic_Programming, Math, String_Matching | 377 | 100.00
| 2850 |[Minimum Moves to Spread Stones Over Grid](src/main/kotlin/g2801_2900/s2850_minimum_moves_to_spread_stones_over_grid)| Medium | Array, Dynamic_Programming, Breadth_First_Search, Matrix | 133 | 100.00
| 2849 |[Determine if a Cell Is Reachable at a Given Time](src/main/kotlin/g2801_2900/s2849_determine_if_a_cell_is_reachable_at_a_given_time)| Medium | Math | 138 | 80.95
| 2848 |[Points That Intersect With Cars](src/main/kotlin/g2801_2900/s2848_points_that_intersect_with_cars)| Easy | Hash_Table, Math, Prefix_Sum | 190 | 100.00
| 2846 |[Minimum Edge Weight Equilibrium Queries in a Tree](src/main/kotlin/g2801_2900/s2846_minimum_edge_weight_equilibrium_queries_in_a_tree)| Hard | Array, Tree, Graph, Strongly_Connected_Component | 982 | 100.00
| 2845 |[Count of Interesting Subarrays](src/main/kotlin/g2801_2900/s2845_count_of_interesting_subarrays)| Medium | Array, Hash_Table, Prefix_Sum | 703 | 100.00
| 2844 |[Minimum Operations to Make a Special Number](src/main/kotlin/g2801_2900/s2844_minimum_operations_to_make_a_special_number)| Medium | String, Math, Greedy, Enumeration | 162 | 60.00
| 2843 |[Count Symmetric Integers](src/main/kotlin/g2801_2900/s2843_count_symmetric_integers)| Easy | Math, Enumeration | 203 | 88.24
| 2842 |[Count K-Subsequences of a String With Maximum Beauty](src/main/kotlin/g2801_2900/s2842_count_k_subsequences_of_a_string_with_maximum_beauty)| Hard | String, Hash_Table, Math, Greedy, Combinatorics | 217 | 100.00
| 2841 |[Maximum Sum of Almost Unique Subarray](src/main/kotlin/g2801_2900/s2841_maximum_sum_of_almost_unique_subarray)| Medium | Array, Hash_Table, Sliding_Window | 387 | 100.00
| 2840 |[Check if Strings Can be Made Equal With Operations II](src/main/kotlin/g2801_2900/s2840_check_if_strings_can_be_made_equal_with_operations_ii)| Medium | String, Hash_Table, Sorting | 225 | 100.00
| 2839 |[Check if Strings Can be Made Equal With Operations I](src/main/kotlin/g2801_2900/s2839_check_if_strings_can_be_made_equal_with_operations_i)| Easy | String | 154 | 100.00
| 2836 |[Maximize Value of Function in a Ball Passing Game](src/main/kotlin/g2801_2900/s2836_maximize_value_of_function_in_a_ball_passing_game)| Hard | Array, Dynamic_Programming, Bit_Manipulation | 933 | 100.00
| 2835 |[Minimum Operations to Form Subsequence With Target Sum](src/main/kotlin/g2801_2900/s2835_minimum_operations_to_form_subsequence_with_target_sum)| Hard | Array, Greedy, Bit_Manipulation | 183 | 100.00
| 2834 |[Find the Minimum Possible Sum of a Beautiful Array](src/main/kotlin/g2801_2900/s2834_find_the_minimum_possible_sum_of_a_beautiful_array)| Medium | Math, Greedy | 130 | 100.00
| 2833 |[Furthest Point From Origin](src/main/kotlin/g2801_2900/s2833_furthest_point_from_origin)| Easy | Array, Counting | 160 | 81.25
| 2831 |[Find the Longest Equal Subarray](src/main/kotlin/g2801_2900/s2831_find_the_longest_equal_subarray)| Medium | Array, Hash_Table, Binary_Search, Sliding_Window | 663 | 100.00
| 2830 |[Maximize the Profit as the Salesman](src/main/kotlin/g2801_2900/s2830_maximize_the_profit_as_the_salesman)| Medium | Array, Dynamic_Programming, Sorting, Binary_Search | 776 | 100.00
| 2829 |[Determine the Minimum Sum of a k-avoiding Array](src/main/kotlin/g2801_2900/s2829_determine_the_minimum_sum_of_a_k_avoiding_array)| Medium | Math, Greedy | 162 | 75.00
| 2828 |[Check if a String Is an Acronym of Words](src/main/kotlin/g2801_2900/s2828_check_if_a_string_is_an_acronym_of_words)| Easy | Array, String | 180 | 90.14
| 2827 |[Number of Beautiful Integers in the Range](src/main/kotlin/g2801_2900/s2827_number_of_beautiful_integers_in_the_range)| Hard | Dynamic_Programming, Math | 169 | 100.00
| 2826 |[Sorting Three Groups](src/main/kotlin/g2801_2900/s2826_sorting_three_groups)| Medium | Array, Dynamic_Programming | 250 | 100.00
| 2825 |[Make String a Subsequence Using Cyclic Increments](src/main/kotlin/g2801_2900/s2825_make_string_a_subsequence_using_cyclic_increments)| Medium | String, Two_Pointers | 227 | 83.33
| 2824 |[Count Pairs Whose Sum is Less than Target](src/main/kotlin/g2801_2900/s2824_count_pairs_whose_sum_is_less_than_target)| Easy | Array, Sorting, Two_Pointers | 168 | 90.62
| 2818 |[Apply Operations to Maximize Score](src/main/kotlin/g2801_2900/s2818_apply_operations_to_maximize_score)| Hard | Array, Math, Greedy, Stack, Monotonic_Stack, Number_Theory | 727 | 100.00
| 2817 |[Minimum Absolute Difference Between Elements With Constraint](src/main/kotlin/g2801_2900/s2817_minimum_absolute_difference_between_elements_with_constraint)| Medium | Array, Binary_Search, Ordered_Set | 759 | 100.00
| 2816 |[Double a Number Represented as a Linked List](src/main/kotlin/g2801_2900/s2816_double_a_number_represented_as_a_linked_list)| Medium | Math, Stack, Linked_List | 393 | 100.00
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Solution {
return target.size - longestIncreasingSubsequence(list)
}

private fun longestIncreasingSubsequence(list: MutableList<Int?>): Int {
private fun longestIncreasingSubsequence(list: List<Int?>): Int {
val n = list.size
var l = 0
val arr = IntArray(n)
Expand Down
4 changes: 1 addition & 3 deletions src/main/kotlin/g2301_2400/s2306_naming_a_company/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,10 @@ class Solution {
res += compare(b1, b2)
}
}

return res
}

fun compare(b1: MutableSet<String>, b2: MutableSet<String>): Long {
fun compare(b1: Set<String>, b2: Set<String>): Long {
val set1 = if (b1.size > b2.size) b1 else b2
val set2 = if (b1.size > b2.size) b2 else b1
var n1 = set1.size
Expand All @@ -100,7 +99,6 @@ class Solution {
n2--
}
}

return (n1 * n2) * 2L
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
[![](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)

## 2825\. Make String a Subsequence Using Cyclic Increments

Medium

You are given two **0-indexed** strings `str1` and `str2`.

In an operation, you select a **set** of indices in `str1`, and for each index `i` in the set, increment `str1[i]` to the next character **cyclically**. That is `'a'` becomes `'b'`, `'b'` becomes `'c'`, and so on, and `'z'` becomes `'a'`.

Return `true` _if it is possible to make_ `str2` _a subsequence of_ `str1` _by performing the operation **at most once**_, _and_ `false` _otherwise_.

**Note:** A subsequence of a string is a new string that is formed from the original string by deleting some (possibly none) of the characters without disturbing the relative positions of the remaining characters.

**Example 1:**

**Input:** str1 = "abc", str2 = "ad"

**Output:** true

**Explanation:** Select index 2 in str1. Increment str1[2] to become 'd'. Hence, str1 becomes "abd" and str2 is now a subsequence. Therefore, true is returned.

**Example 2:**

**Input:** str1 = "zc", str2 = "ad"

**Output:** true

**Explanation:** Select indices 0 and 1 in str1. Increment str1[0] to become 'a'. Increment str1[1] to become 'd'. Hence, str1 becomes "ad" and str2 is now a subsequence. Therefore, true is returned.

**Example 3:**

**Input:** str1 = "ab", str2 = "d"

**Output:** false

**Explanation:** In this example, it can be shown that it is impossible to make str2 a subsequence of str1 using the operation at most once. Therefore, false is returned.

**Constraints:**

* <code>1 <= str1.length <= 10<sup>5</sup></code>
* <code>1 <= str2.length <= 10<sup>5</sup></code>
* `str1` and `str2` consist of only lowercase English letters.

## Solution

```kotlin
class Solution {
fun canMakeSubsequence(str1: String, str2: String): Boolean {
var str1ptr = 0
for (element in str2) {
val c2 = element
var found = false
while (str1ptr < str1.length) {
val c1 = str1[str1ptr++]
if (c1 == c2 || (c1.code - 'a'.code + 1) % 26 == c2.code - 'a'.code) {
found = true
break
}
}
if (!found) {
return false
}
}
return true
}
}
```
94 changes: 94 additions & 0 deletions src/main/kotlin/g2801_2900/s2826_sorting_three_groups/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
[![](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)

## 2826\. Sorting Three Groups

Medium

You are given a **0-indexed** integer array `nums` of length `n`.

The numbers from `0` to `n - 1` are divided into three groups numbered from `1` to `3`, where number `i` belongs to group `nums[i]`. Notice that some groups may be **empty**.

You are allowed to perform this operation any number of times:

* Pick number `x` and change its group. More formally, change `nums[x]` to any number from `1` to `3`.

A new array `res` is constructed using the following procedure:

1. Sort the numbers in each group independently.
2. Append the elements of groups `1`, `2`, and `3` to `res` **in this order**.

Array `nums` is called a **beautiful array** if the constructed array `res` is sorted in **non-decreasing** order.

Return _the **minimum** number of operations to make_ `nums` _a **beautiful array**_.

**Example 1:**

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

**Output:** 3

**Explanation:** It's optimal to perform three operations:
1. change nums[0] to 1.
2. change nums[2] to 1.
3. change nums[3] to 1.

After performing the operations and sorting the numbers in each group, group 1 becomes equal to [0,1,2,3,4] and group 2 and group 3 become empty. Hence, res is equal to [0,1,2,3,4] which is sorted in non-decreasing order.

It can be proven that there is no valid sequence of less than three operations.

**Example 2:**

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

**Output:** 2

**Explanation:** It's optimal to perform two operations:
1. change nums[1] to 1.
2. change nums[2] to 1.

After performing the operations and sorting the numbers in each group, group 1 becomes equal to [0,1,2,3], group 2 becomes empty, and group 3 becomes equal to [4,5]. Hence, res is equal to [0,1,2,3,4,5] which is sorted in non-decreasing order.

It can be proven that there is no valid sequence of less than two operations.

**Example 3:**

**Input:** nums = [2,2,2,2,3,3]

**Output:** 0

**Explanation:** It's optimal to not perform operations.

After sorting the numbers in each group, group 1 becomes empty, group 2 becomes equal to [0,1,2,3] and group 3 becomes equal to [4,5]. Hence, res is equal to [0,1,2,3,4,5] which is sorted in non-decreasing order.

**Constraints:**

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

## Solution

```kotlin
import kotlin.math.max

class Solution {
fun minimumOperations(nums: List<Int>): Int {
val n = nums.size
val arr = IntArray(3)
var max = 0
for (num in nums) {
var locMax = 0
val value = num
for (j in 0 until value) {
locMax = max(locMax, arr[j])
}
locMax++
arr[value - 1] = locMax
if (locMax > max) {
max = locMax
}
}
return n - max
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
[![](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)

## 2827\. Number of Beautiful Integers in the Range

Hard

You are given positive integers `low`, `high`, and `k`.

A number is **beautiful** if it meets both of the following conditions:

* The count of even digits in the number is equal to the count of odd digits.
* The number is divisible by `k`.

Return _the number of beautiful integers in the range_ `[low, high]`.

**Example 1:**

**Input:** low = 10, high = 20, k = 3

**Output:** 2

**Explanation:** There are 2 beautiful integers in the given range: [12,18].
- 12 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 3.
- 18 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 3. Additionally we can see that:
- 16 is not beautiful because it is not divisible by k = 3.
- 15 is not beautiful because it does not contain equal counts even and odd digits. It can be shown that there are only 2 beautiful integers in the given range.

**Example 2:**

**Input:** low = 1, high = 10, k = 1

**Output:** 1

**Explanation:** There is 1 beautiful integer in the given range: [10].
- 10 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 1. It can be shown that there is only 1 beautiful integer in the given range.

**Example 3:**

**Input:** low = 5, high = 5, k = 2

**Output:** 0

**Explanation:** There are 0 beautiful integers in the given range.
- 5 is not beautiful because it is not divisible by k = 2 and it does not contain equal even and odd digits.

**Constraints:**

* <code>0 < low <= high <= 10<sup>9</sup></code>
* `0 < k <= 20`

## Solution

```kotlin
import kotlin.math.max

class Solution {
private lateinit var dp: Array<Array<Array<Array<IntArray>>>>
private var maxLength = 0

fun numberOfBeautifulIntegers(low: Int, high: Int, k: Int): Int {
val num1 = low.toString()
val num2 = high.toString()
maxLength = max(num1.length.toDouble(), num2.length.toDouble()).toInt()
dp = Array(4) { Array(maxLength) { Array(maxLength) { Array(maxLength) { IntArray(k) } } } }
for (a in dp) {
for (b in a) {
for (c in b) {
for (d in c) {
d.fill(-1)
}
}
}
}
return dp(num1, num2, 0, 3, 0, 0, 0, 0, k)
}

private fun dp(
low: String,
high: String,
i: Int,
mode: Int,
odd: Int,
even: Int,
num: Int,
rem: Int,
k: Int
): Int {
if (i == maxLength) {
return if (num % k == 0 && odd == even) 1 else 0
}
if (dp[mode][i][odd][even][rem] != -1) {
return dp[mode][i][odd][even][rem]
}
var res = 0
val lowLimit = mode % 2 == 1
val highLimit = mode / 2 == 1
var start = 0
var end = 9
if (lowLimit) {
start = digitAt(low, i)
}
if (highLimit) {
end = digitAt(high, i)
}
for (j in start..end) {
var newMode = 0
if (j == start && lowLimit) {
newMode += 1
}
if (j == end && highLimit) {
newMode += 2
}
var newEven = even
if (num != 0 || j != 0) {
newEven += if (j % 2 == 0) 1 else 0
}
val newOdd = odd + (if (j % 2 == 1) 1 else 0)
res +=
dp(
low,
high,
i + 1,
newMode,
newOdd,
newEven,
num * 10 + j,
(num * 10 + j) % k,
k
)
}
dp[mode][i][odd][even][rem] = res
return res
}

private fun digitAt(num: String, i: Int): Int {
val index = num.length - maxLength + i
return if (index < 0) 0 else num[index].code - '0'.code
}
}
```
Loading