Skip to content

Commit b8b71e5

Browse files
authored
Added tasks 2825-2856
1 parent 48f5682 commit b8b71e5

File tree

28 files changed

+2229
-5
lines changed
  • src/main/kotlin
    • g1701_1800/s1713_minimum_operations_to_make_a_subsequence
    • g2301_2400/s2306_naming_a_company
    • g2801_2900
      • s2825_make_string_a_subsequence_using_cyclic_increments
      • s2826_sorting_three_groups
      • s2827_number_of_beautiful_integers_in_the_range
      • s2828_check_if_a_string_is_an_acronym_of_words
      • s2829_determine_the_minimum_sum_of_a_k_avoiding_array
      • s2830_maximize_the_profit_as_the_salesman
      • s2831_find_the_longest_equal_subarray
      • s2833_furthest_point_from_origin
      • s2834_find_the_minimum_possible_sum_of_a_beautiful_array
      • s2835_minimum_operations_to_form_subsequence_with_target_sum
      • s2836_maximize_value_of_function_in_a_ball_passing_game
      • s2839_check_if_strings_can_be_made_equal_with_operations_i
      • s2840_check_if_strings_can_be_made_equal_with_operations_ii
      • s2841_maximum_sum_of_almost_unique_subarray
      • s2842_count_k_subsequences_of_a_string_with_maximum_beauty
      • s2843_count_symmetric_integers
      • s2844_minimum_operations_to_make_a_special_number
      • s2845_count_of_interesting_subarrays
      • s2846_minimum_edge_weight_equilibrium_queries_in_a_tree
      • s2848_points_that_intersect_with_cars
      • s2849_determine_if_a_cell_is_reachable_at_a_given_time
      • s2850_minimum_moves_to_spread_stones_over_grid
      • s2851_string_transformation
      • s2855_minimum_right_shifts_to_sort_the_array
      • s2856_minimum_array_length_after_pair_removals

28 files changed

+2229
-5
lines changed

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1816,7 +1816,32 @@
18161816

18171817
| # | Title | Difficulty | Tag | Time, ms | Time, %
18181818
|------|----------------|-------------|-------------|----------|--------
1819-
| 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
1819+
| 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
1820+
| 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
1821+
| 2851 |[String Transformation](src/main/kotlin/g2801_2900/s2851_string_transformation)| Hard | String, Dynamic_Programming, Math, String_Matching | 377 | 100.00
1822+
| 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
1823+
| 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
1824+
| 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
1825+
| 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
1826+
| 2845 |[Count of Interesting Subarrays](src/main/kotlin/g2801_2900/s2845_count_of_interesting_subarrays)| Medium | Array, Hash_Table, Prefix_Sum | 703 | 100.00
1827+
| 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
1828+
| 2843 |[Count Symmetric Integers](src/main/kotlin/g2801_2900/s2843_count_symmetric_integers)| Easy | Math, Enumeration | 203 | 88.24
1829+
| 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
1830+
| 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
1831+
| 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
1832+
| 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
1833+
| 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
1834+
| 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
1835+
| 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
1836+
| 2833 |[Furthest Point From Origin](src/main/kotlin/g2801_2900/s2833_furthest_point_from_origin)| Easy | Array, Counting | 160 | 81.25
1837+
| 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
1838+
| 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
1839+
| 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
1840+
| 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
1841+
| 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
1842+
| 2826 |[Sorting Three Groups](src/main/kotlin/g2801_2900/s2826_sorting_three_groups)| Medium | Array, Dynamic_Programming | 250 | 100.00
1843+
| 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
1844+
| 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
18201845
| 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
18211846
| 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
18221847
| 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

src/main/kotlin/g1701_1800/s1713_minimum_operations_to_make_a_subsequence/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class Solution {
5353
return target.size - longestIncreasingSubsequence(list)
5454
}
5555

56-
private fun longestIncreasingSubsequence(list: MutableList<Int?>): Int {
56+
private fun longestIncreasingSubsequence(list: List<Int?>): Int {
5757
val n = list.size
5858
var l = 0
5959
val arr = IntArray(n)

src/main/kotlin/g2301_2400/s2306_naming_a_company/readme.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,10 @@ class Solution {
8585
res += compare(b1, b2)
8686
}
8787
}
88-
8988
return res
9089
}
9190

92-
fun compare(b1: MutableSet<String>, b2: MutableSet<String>): Long {
91+
fun compare(b1: Set<String>, b2: Set<String>): Long {
9392
val set1 = if (b1.size > b2.size) b1 else b2
9493
val set2 = if (b1.size > b2.size) b2 else b1
9594
var n1 = set1.size
@@ -100,7 +99,6 @@ class Solution {
10099
n2--
101100
}
102101
}
103-
104102
return (n1 * n2) * 2L
105103
}
106104
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](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)
3+
4+
## 2825\. Make String a Subsequence Using Cyclic Increments
5+
6+
Medium
7+
8+
You are given two **0-indexed** strings `str1` and `str2`.
9+
10+
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'`.
11+
12+
Return `true` _if it is possible to make_ `str2` _a subsequence of_ `str1` _by performing the operation **at most once**_, _and_ `false` _otherwise_.
13+
14+
**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.
15+
16+
**Example 1:**
17+
18+
**Input:** str1 = "abc", str2 = "ad"
19+
20+
**Output:** true
21+
22+
**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.
23+
24+
**Example 2:**
25+
26+
**Input:** str1 = "zc", str2 = "ad"
27+
28+
**Output:** true
29+
30+
**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.
31+
32+
**Example 3:**
33+
34+
**Input:** str1 = "ab", str2 = "d"
35+
36+
**Output:** false
37+
38+
**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.
39+
40+
**Constraints:**
41+
42+
* <code>1 <= str1.length <= 10<sup>5</sup></code>
43+
* <code>1 <= str2.length <= 10<sup>5</sup></code>
44+
* `str1` and `str2` consist of only lowercase English letters.
45+
46+
## Solution
47+
48+
```kotlin
49+
class Solution {
50+
fun canMakeSubsequence(str1: String, str2: String): Boolean {
51+
var str1ptr = 0
52+
for (element in str2) {
53+
val c2 = element
54+
var found = false
55+
while (str1ptr < str1.length) {
56+
val c1 = str1[str1ptr++]
57+
if (c1 == c2 || (c1.code - 'a'.code + 1) % 26 == c2.code - 'a'.code) {
58+
found = true
59+
break
60+
}
61+
}
62+
if (!found) {
63+
return false
64+
}
65+
}
66+
return true
67+
}
68+
}
69+
```
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](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)
3+
4+
## 2826\. Sorting Three Groups
5+
6+
Medium
7+
8+
You are given a **0-indexed** integer array `nums` of length `n`.
9+
10+
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**.
11+
12+
You are allowed to perform this operation any number of times:
13+
14+
* Pick number `x` and change its group. More formally, change `nums[x]` to any number from `1` to `3`.
15+
16+
A new array `res` is constructed using the following procedure:
17+
18+
1. Sort the numbers in each group independently.
19+
2. Append the elements of groups `1`, `2`, and `3` to `res` **in this order**.
20+
21+
Array `nums` is called a **beautiful array** if the constructed array `res` is sorted in **non-decreasing** order.
22+
23+
Return _the **minimum** number of operations to make_ `nums` _a **beautiful array**_.
24+
25+
**Example 1:**
26+
27+
**Input:** nums = [2,1,3,2,1]
28+
29+
**Output:** 3
30+
31+
**Explanation:** It's optimal to perform three operations:
32+
1. change nums[0] to 1.
33+
2. change nums[2] to 1.
34+
3. change nums[3] to 1.
35+
36+
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.
37+
38+
It can be proven that there is no valid sequence of less than three operations.
39+
40+
**Example 2:**
41+
42+
**Input:** nums = [1,3,2,1,3,3]
43+
44+
**Output:** 2
45+
46+
**Explanation:** It's optimal to perform two operations:
47+
1. change nums[1] to 1.
48+
2. change nums[2] to 1.
49+
50+
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.
51+
52+
It can be proven that there is no valid sequence of less than two operations.
53+
54+
**Example 3:**
55+
56+
**Input:** nums = [2,2,2,2,3,3]
57+
58+
**Output:** 0
59+
60+
**Explanation:** It's optimal to not perform operations.
61+
62+
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.
63+
64+
**Constraints:**
65+
66+
* `1 <= nums.length <= 100`
67+
* `1 <= nums[i] <= 3`
68+
69+
## Solution
70+
71+
```kotlin
72+
import kotlin.math.max
73+
74+
class Solution {
75+
fun minimumOperations(nums: List<Int>): Int {
76+
val n = nums.size
77+
val arr = IntArray(3)
78+
var max = 0
79+
for (num in nums) {
80+
var locMax = 0
81+
val value = num
82+
for (j in 0 until value) {
83+
locMax = max(locMax, arr[j])
84+
}
85+
locMax++
86+
arr[value - 1] = locMax
87+
if (locMax > max) {
88+
max = locMax
89+
}
90+
}
91+
return n - max
92+
}
93+
}
94+
```
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](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)
3+
4+
## 2827\. Number of Beautiful Integers in the Range
5+
6+
Hard
7+
8+
You are given positive integers `low`, `high`, and `k`.
9+
10+
A number is **beautiful** if it meets both of the following conditions:
11+
12+
* The count of even digits in the number is equal to the count of odd digits.
13+
* The number is divisible by `k`.
14+
15+
Return _the number of beautiful integers in the range_ `[low, high]`.
16+
17+
**Example 1:**
18+
19+
**Input:** low = 10, high = 20, k = 3
20+
21+
**Output:** 2
22+
23+
**Explanation:** There are 2 beautiful integers in the given range: [12,18].
24+
- 12 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 3.
25+
- 18 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 3. Additionally we can see that:
26+
- 16 is not beautiful because it is not divisible by k = 3.
27+
- 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.
28+
29+
**Example 2:**
30+
31+
**Input:** low = 1, high = 10, k = 1
32+
33+
**Output:** 1
34+
35+
**Explanation:** There is 1 beautiful integer in the given range: [10].
36+
- 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.
37+
38+
**Example 3:**
39+
40+
**Input:** low = 5, high = 5, k = 2
41+
42+
**Output:** 0
43+
44+
**Explanation:** There are 0 beautiful integers in the given range.
45+
- 5 is not beautiful because it is not divisible by k = 2 and it does not contain equal even and odd digits.
46+
47+
**Constraints:**
48+
49+
* <code>0 < low <= high <= 10<sup>9</sup></code>
50+
* `0 < k <= 20`
51+
52+
## Solution
53+
54+
```kotlin
55+
import kotlin.math.max
56+
57+
class Solution {
58+
private lateinit var dp: Array<Array<Array<Array<IntArray>>>>
59+
private var maxLength = 0
60+
61+
fun numberOfBeautifulIntegers(low: Int, high: Int, k: Int): Int {
62+
val num1 = low.toString()
63+
val num2 = high.toString()
64+
maxLength = max(num1.length.toDouble(), num2.length.toDouble()).toInt()
65+
dp = Array(4) { Array(maxLength) { Array(maxLength) { Array(maxLength) { IntArray(k) } } } }
66+
for (a in dp) {
67+
for (b in a) {
68+
for (c in b) {
69+
for (d in c) {
70+
d.fill(-1)
71+
}
72+
}
73+
}
74+
}
75+
return dp(num1, num2, 0, 3, 0, 0, 0, 0, k)
76+
}
77+
78+
private fun dp(
79+
low: String,
80+
high: String,
81+
i: Int,
82+
mode: Int,
83+
odd: Int,
84+
even: Int,
85+
num: Int,
86+
rem: Int,
87+
k: Int
88+
): Int {
89+
if (i == maxLength) {
90+
return if (num % k == 0 && odd == even) 1 else 0
91+
}
92+
if (dp[mode][i][odd][even][rem] != -1) {
93+
return dp[mode][i][odd][even][rem]
94+
}
95+
var res = 0
96+
val lowLimit = mode % 2 == 1
97+
val highLimit = mode / 2 == 1
98+
var start = 0
99+
var end = 9
100+
if (lowLimit) {
101+
start = digitAt(low, i)
102+
}
103+
if (highLimit) {
104+
end = digitAt(high, i)
105+
}
106+
for (j in start..end) {
107+
var newMode = 0
108+
if (j == start && lowLimit) {
109+
newMode += 1
110+
}
111+
if (j == end && highLimit) {
112+
newMode += 2
113+
}
114+
var newEven = even
115+
if (num != 0 || j != 0) {
116+
newEven += if (j % 2 == 0) 1 else 0
117+
}
118+
val newOdd = odd + (if (j % 2 == 1) 1 else 0)
119+
res +=
120+
dp(
121+
low,
122+
high,
123+
i + 1,
124+
newMode,
125+
newOdd,
126+
newEven,
127+
num * 10 + j,
128+
(num * 10 + j) % k,
129+
k
130+
)
131+
}
132+
dp[mode][i][odd][even][rem] = res
133+
return res
134+
}
135+
136+
private fun digitAt(num: String, i: Int): Int {
137+
val index = num.length - maxLength + i
138+
return if (index < 0) 0 else num[index].code - '0'.code
139+
}
140+
}
141+
```

0 commit comments

Comments
 (0)