Skip to content

Commit 703fd7c

Browse files
authored
Added tasks 2901-2929, 2945-2949
1 parent ef8f7f3 commit 703fd7c

File tree

30 files changed

+2726
-0
lines changed
  • src/main/kotlin/g2901_3000
    • s2901_longest_unequal_adjacent_groups_subsequence_ii
    • s2902_count_of_sub_multisets_with_bounded_sum
    • s2903_find_indices_with_index_and_value_difference_i
    • s2904_shortest_and_lexicographically_smallest_beautiful_string
    • s2905_find_indices_with_index_and_value_difference_ii
    • s2906_construct_product_matrix
    • s2908_minimum_sum_of_mountain_triplets_i
    • s2909_minimum_sum_of_mountain_triplets_ii
    • s2910_minimum_number_of_groups_to_create_a_valid_assignment
    • s2911_minimum_changes_to_make_k_semi_palindromes
    • s2913_subarrays_distinct_element_sum_of_squares_i
    • s2914_minimum_number_of_changes_to_make_binary_string_beautiful
    • s2915_length_of_the_longest_subsequence_that_sums_to_target
    • s2916_subarrays_distinct_element_sum_of_squares_ii
    • s2917_find_the_k_or_of_an_array
    • s2918_minimum_equal_sum_of_two_arrays_after_replacing_zeros
    • s2919_minimum_increment_operations_to_make_array_beautiful
    • s2920_maximum_points_after_collecting_coins_from_all_nodes
    • s2923_find_champion_i
    • s2924_find_champion_ii
    • s2925_maximum_score_after_applying_operations_on_a_tree
    • s2926_maximum_balanced_subsequence_sum
    • s2928_distribute_candies_among_children_i
    • s2929_distribute_candies_among_children_ii
    • s2945_find_maximum_non_decreasing_array_length
    • s2946_matrix_similarity_after_cyclic_shifts
    • s2947_count_beautiful_substrings_i
    • s2948_make_lexicographically_smallest_array_by_swapping_elements
    • s2949_count_beautiful_substrings_ii

30 files changed

+2726
-0
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,6 +1816,35 @@
18161816

18171817
| # | Title | Difficulty | Tag | Time, ms | Time, %
18181818
|------|----------------|-------------|-------------|----------|--------
1819+
| 2949 |[Count Beautiful Substrings II](src/main/kotlin/g2901_3000/s2949_count_beautiful_substrings_ii)| Hard | String, Hash_Table, Math, Prefix_Sum, Number_Theory | 299 | 100.00
1820+
| 2948 |[Make Lexicographically Smallest Array by Swapping Elements](src/main/kotlin/g2901_3000/s2948_make_lexicographically_smallest_array_by_swapping_elements)| Medium | Array, Sorting, Union_Find | 2141 | 6.45
1821+
| 2947 |[Count Beautiful Substrings I](src/main/kotlin/g2901_3000/s2947_count_beautiful_substrings_i)| Medium | String, Prefix_Sum, Enumeration | 291 | 59.52
1822+
| 2946 |[Matrix Similarity After Cyclic Shifts](src/main/kotlin/g2901_3000/s2946_matrix_similarity_after_cyclic_shifts)| Easy | Array, Math, Matrix, Simulation | 210 | 75.00
1823+
| 2945 |[Find Maximum Non-decreasing Array Length](src/main/kotlin/g2901_3000/s2945_find_maximum_non_decreasing_array_length)| Hard | Array, Dynamic_Programming, Binary_Search, Stack, Monotonic_Stack, Queue, Monotonic_Queue | 636 | 87.50
1824+
| 2929 |[Distribute Candies Among Children II](src/main/kotlin/g2901_3000/s2929_distribute_candies_among_children_ii)| Medium | Math, Enumeration, Combinatorics | 193 | 18.18
1825+
| 2928 |[Distribute Candies Among Children I](src/main/kotlin/g2901_3000/s2928_distribute_candies_among_children_i)| Easy | Math, Enumeration, Combinatorics | 282 | 6.38
1826+
| 2926 |[Maximum Balanced Subsequence Sum](src/main/kotlin/g2901_3000/s2926_maximum_balanced_subsequence_sum)| Hard | Array, Dynamic_Programming, Binary_Search, Segment_Tree, Binary_Indexed_Tree | 1067 | 12.50
1827+
| 2925 |[Maximum Score After Applying Operations on a Tree](src/main/kotlin/g2901_3000/s2925_maximum_score_after_applying_operations_on_a_tree)| Medium | Dynamic_Programming, Depth_First_Search, Tree | 1274 | 6.67
1828+
| 2924 |[Find Champion II](src/main/kotlin/g2901_3000/s2924_find_champion_ii)| Medium | Graph | 724 | 6.98
1829+
| 2923 |[Find Champion I](src/main/kotlin/g2901_3000/s2923_find_champion_i)| Easy | Array, Matrix | 591 | 5.63
1830+
| 2920 |[Maximum Points After Collecting Coins From All Nodes](src/main/kotlin/g2901_3000/s2920_maximum_points_after_collecting_coins_from_all_nodes)| Hard | Array, Dynamic_Programming, Depth_First_Search, Tree, Bit_Manipulation | 2255 | 25.00
1831+
| 2919 |[Minimum Increment Operations to Make Array Beautiful](src/main/kotlin/g2901_3000/s2919_minimum_increment_operations_to_make_array_beautiful)| Medium | Array, Dynamic_Programming | 520 | 62.50
1832+
| 2918 |[Minimum Equal Sum of Two Arrays After Replacing Zeros](src/main/kotlin/g2901_3000/s2918_minimum_equal_sum_of_two_arrays_after_replacing_zeros)| Medium | Array, Greedy | 1410 | 7.69
1833+
| 2917 |[Find the K-or of an Array](src/main/kotlin/g2901_3000/s2917_find_the_k_or_of_an_array)| Easy | Array, Bit_Manipulation | 191 | 76.47
1834+
| 2916 |[Subarrays Distinct Element Sum of Squares II](src/main/kotlin/g2901_3000/s2916_subarrays_distinct_element_sum_of_squares_ii)| Hard | Array, Dynamic_Programming, Segment_Tree, Binary_Indexed_Tree | 467 | 100.00
1835+
| 2915 |[Length of the Longest Subsequence That Sums to Target](src/main/kotlin/g2901_3000/s2915_length_of_the_longest_subsequence_that_sums_to_target)| Medium | Array, Dynamic_Programming | 552 | 66.67
1836+
| 2914 |[Minimum Number of Changes to Make Binary String Beautiful](src/main/kotlin/g2901_3000/s2914_minimum_number_of_changes_to_make_binary_string_beautiful)| Medium | String | 180 | 100.00
1837+
| 2913 |[Subarrays Distinct Element Sum of Squares I](src/main/kotlin/g2901_3000/s2913_subarrays_distinct_element_sum_of_squares_i)| Easy | Array, Hash_Table | 184 | 95.74
1838+
| 2911 |[Minimum Changes to Make K Semi-palindromes](src/main/kotlin/g2901_3000/s2911_minimum_changes_to_make_k_semi_palindromes)| Hard | String, Dynamic_Programming, Two_Pointers | 216 | 100.00
1839+
| 2910 |[Minimum Number of Groups to Create a Valid Assignment](src/main/kotlin/g2901_3000/s2910_minimum_number_of_groups_to_create_a_valid_assignment)| Medium | Array, Hash_Table, Greedy | 745 | 75.00
1840+
| 2909 |[Minimum Sum of Mountain Triplets II](src/main/kotlin/g2901_3000/s2909_minimum_sum_of_mountain_triplets_ii)| Medium | Array | 463 | 100.00
1841+
| 2908 |[Minimum Sum of Mountain Triplets I](src/main/kotlin/g2901_3000/s2908_minimum_sum_of_mountain_triplets_i)| Easy | Array | 174 | 73.33
1842+
| 2906 |[Construct Product Matrix](src/main/kotlin/g2901_3000/s2906_construct_product_matrix)| Medium | Array, Matrix, Prefix_Sum | 1016 | 100.00
1843+
| 2905 |[Find Indices With Index and Value Difference II](src/main/kotlin/g2901_3000/s2905_find_indices_with_index_and_value_difference_ii)| Medium | Array | 485 | 50.00
1844+
| 2904 |[Shortest and Lexicographically Smallest Beautiful String](src/main/kotlin/g2901_3000/s2904_shortest_and_lexicographically_smallest_beautiful_string)| Medium | String, Sliding_Window | 169 | 66.67
1845+
| 2903 |[Find Indices With Index and Value Difference I](src/main/kotlin/g2901_3000/s2903_find_indices_with_index_and_value_difference_i)| Easy | Array | 192 | 100.00
1846+
| 2902 |[Count of Sub-Multisets With Bounded Sum](src/main/kotlin/g2901_3000/s2902_count_of_sub_multisets_with_bounded_sum)| Hard | Array, Hash_Table, Dynamic_Programming, Sliding_Window | 2416 | 100.00
1847+
| 2901 |[Longest Unequal Adjacent Groups Subsequence II](src/main/kotlin/g2901_3000/s2901_longest_unequal_adjacent_groups_subsequence_ii)| Medium | Array, String, Dynamic_Programming | 305 | 100.00
18191848
| 2900 |[Longest Unequal Adjacent Groups Subsequence I](src/main/kotlin/g2801_2900/s2900_longest_unequal_adjacent_groups_subsequence_i)| Medium | Array, String, Dynamic_Programming, Greedy | 294 | 100.00
18201849
| 2899 |[Last Visited Integers](src/main/kotlin/g2801_2900/s2899_last_visited_integers)| Easy | Array, String, Simulation | 209 | 72.73
18211850
| 2897 |[Apply Operations on Array to Maximize Sum of Squares](src/main/kotlin/g2801_2900/s2897_apply_operations_on_array_to_maximize_sum_of_squares)| Hard | Array, Hash_Table, Greedy, Bit_Manipulation | 572 | 100.00
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
## 2901\. Longest Unequal Adjacent Groups Subsequence II
5+
6+
Medium
7+
8+
You are given an integer `n`, a **0-indexed** string array `words`, and a **0-indexed** array `groups`, both arrays having length `n`.
9+
10+
The **hamming distance** between two strings of equal length is the number of positions at which the corresponding characters are **different**.
11+
12+
You need to select the **longest** **subsequence** from an array of indices `[0, 1, ..., n - 1]`, such that for the subsequence denoted as <code>[i<sub>0</sub>, i<sub>1</sub>, ..., i<sub>k - 1</sub>]</code> having length `k`, the following holds:
13+
14+
* For **adjacent** indices in the subsequence, their corresponding groups are **unequal**, i.e., <code>groups[i<sub>j</sub>] != groups[i<sub>j + 1</sub>]</code>, for each `j` where `0 < j + 1 < k`.
15+
* <code>words[i<sub>j</sub>]</code> and <code>words[i<sub>j + 1</sub>]</code> are **equal** in length, and the **hamming distance** between them is `1`, where `0 < j + 1 < k`, for all indices in the subsequence.
16+
17+
Return _a string array containing the words corresponding to the indices **(in order)** in the selected subsequence_. If there are multiple answers, return _any of them_.
18+
19+
A **subsequence** of an array is a new array that is formed from the original array by deleting some (possibly none) of the elements without disturbing the relative positions of the remaining elements.
20+
21+
**Note:** strings in `words` may be **unequal** in length.
22+
23+
**Example 1:**
24+
25+
**Input:** n = 3, words = ["bab","dab","cab"], groups = [1,2,2]
26+
27+
**Output:** ["bab","cab"]
28+
29+
**Explanation:** A subsequence that can be selected is [0,2].
30+
- groups[0] != groups[2]
31+
- words[0].length == words[2].length, and the hamming distance between them is 1.
32+
33+
So, a valid answer is [words[0],words[2]] = ["bab","cab"]. Another subsequence that can be selected is [0,1].
34+
- groups[0] != groups[1]
35+
- words[0].length == words[1].length, and the hamming distance between them is 1.
36+
37+
So, another valid answer is [words[0],words[1]] = ["bab","dab"]. It can be shown that the length of the longest subsequence of indices that satisfies the conditions is 2.
38+
39+
**Example 2:**
40+
41+
**Input:** n = 4, words = ["a","b","c","d"], groups = [1,2,3,4]
42+
43+
**Output:** ["a","b","c","d"]
44+
45+
**Explanation:** We can select the subsequence [0,1,2,3]. It satisfies both conditions. Hence, the answer is [words[0],words[1],words[2],words[3]] = ["a","b","c","d"]. It has the longest length among all subsequences of indices that satisfy the conditions. Hence, it is the only answer.
46+
47+
**Constraints:**
48+
49+
* `1 <= n == words.length == groups.length <= 1000`
50+
* `1 <= words[i].length <= 10`
51+
* `1 <= groups[i] <= n`
52+
* `words` consists of **distinct** strings.
53+
* `words[i]` consists of lowercase English letters.
54+
55+
## Solution
56+
57+
```kotlin
58+
class Solution {
59+
fun getWordsInLongestSubsequence(n: Int, words: Array<String>, groups: IntArray): List<String> {
60+
val check = IntArray(groups.size)
61+
val before = IntArray(groups.size)
62+
check.fill(1)
63+
before.fill(-1)
64+
var index = 0
65+
var max = 1
66+
for (i in 1 until n) {
67+
for (j in i - 1 downTo 0) {
68+
if (groups[i] != groups[j] && ham(words[i], words[j]) && check[j] + 1 > check[i]) {
69+
check[i] = check[j] + 1
70+
before[i] = j
71+
if (check[i] > max) {
72+
max = check[i]
73+
index = i
74+
}
75+
}
76+
}
77+
}
78+
val ans: MutableList<String> = ArrayList()
79+
while (index >= 0) {
80+
ans.add(words[index])
81+
index = before[index]
82+
}
83+
ans.reverse()
84+
return ans
85+
}
86+
87+
private fun ham(s1: String, s2: String): Boolean {
88+
if (s1.length != s2.length) {
89+
return false
90+
}
91+
var count = 0
92+
for (i in s1.indices) {
93+
if (s1[i] != s2[i]) {
94+
count++
95+
}
96+
if (count > 1) {
97+
return false
98+
}
99+
}
100+
return count == 1
101+
}
102+
}
103+
```
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
## 2902\. Count of Sub-Multisets With Bounded Sum
5+
6+
Hard
7+
8+
You are given a **0-indexed** array `nums` of non-negative integers, and two integers `l` and `r`.
9+
10+
Return _the **count of sub-multisets** within_ `nums` _where the sum of elements in each subset falls within the inclusive range of_ `[l, r]`.
11+
12+
Since the answer may be large, return it modulo <code>10<sup>9</sup> + 7</code>.
13+
14+
A **sub-multiset** is an **unordered** collection of elements of the array in which a given value `x` can occur `0, 1, ..., occ[x]` times, where `occ[x]` is the number of occurrences of `x` in the array.
15+
16+
**Note** that:
17+
18+
* Two **sub-multisets** are the same if sorting both sub-multisets results in identical multisets.
19+
* The sum of an **empty** multiset is `0`.
20+
21+
**Example 1:**
22+
23+
**Input:** nums = [1,2,2,3], l = 6, r = 6
24+
25+
**Output:** 1
26+
27+
**Explanation:** The only subset of nums that has a sum of 6 is {1, 2, 3}.
28+
29+
**Example 2:**
30+
31+
**Input:** nums = [2,1,4,2,7], l = 1, r = 5
32+
33+
**Output:** 7
34+
35+
**Explanation:** The subsets of nums that have a sum within the range [1, 5] are {1}, {2}, {4}, {2, 2}, {1, 2}, {1, 4}, and {1, 2, 2}.
36+
37+
**Example 3:**
38+
39+
**Input:** nums = [1,2,1,3,5,2], l = 3, r = 5
40+
41+
**Output:** 9
42+
43+
**Explanation:** The subsets of nums that have a sum within the range [3, 5] are {3}, {5}, {1, 2}, {1, 3}, {2, 2}, {2, 3}, {1, 1, 2}, {1, 1, 3}, and {1, 2, 2}.
44+
45+
**Constraints:**
46+
47+
* <code>1 <= nums.length <= 2 * 10<sup>4</sup></code>
48+
* <code>0 <= nums[i] <= 2 * 10<sup>4</sup></code>
49+
* Sum of `nums` does not exceed <code>2 * 10<sup>4</sup></code>.
50+
* <code>0 <= l <= r <= 2 * 10<sup>4</sup></code>
51+
52+
## Solution
53+
54+
```kotlin
55+
class Solution {
56+
private var map: HashMap<Int, Int>? = null
57+
private lateinit var dp: Array<IntArray>
58+
59+
private fun solve(al: List<Int>, l: Int, r: Int, index: Int, sum: Int): Int {
60+
if (sum > r) {
61+
return 0
62+
}
63+
var ans: Long = 0
64+
if (index >= al.size) {
65+
return ans.toInt()
66+
}
67+
if (dp[index][sum] != -1) {
68+
return dp[index][sum]
69+
}
70+
val cur = al[index]
71+
val count = map!![cur]!!
72+
for (i in 0..count) {
73+
val curSum = sum + cur * i
74+
if (curSum > r) {
75+
break
76+
}
77+
ans += solve(al, l, r, index + 1, curSum)
78+
if (i != 0 && curSum >= l) {
79+
ans += 1
80+
}
81+
ans %= MOD
82+
}
83+
dp[index][sum] = ans.toInt()
84+
return ans.toInt()
85+
}
86+
87+
fun countSubMultisets(nums: List<Int>, l: Int, r: Int): Int {
88+
map = HashMap()
89+
val al: MutableList<Int> = ArrayList()
90+
for (cur in nums) {
91+
val count = map!!.getOrDefault(cur, 0) + 1
92+
map!![cur] = count
93+
if (count == 1) {
94+
al.add(cur)
95+
}
96+
}
97+
val n = al.size
98+
dp = Array(n) { IntArray(r + 1) }
99+
for (i in dp.indices) {
100+
for (j in dp[0].indices) {
101+
dp[i][j] = -1
102+
}
103+
}
104+
al.sort()
105+
var ans = solve(al, l, r, 0, 0)
106+
if (l == 0) {
107+
ans += 1
108+
}
109+
ans %= MOD
110+
return ans
111+
}
112+
113+
companion object {
114+
private const val MOD = 1e9.toInt() + 7
115+
}
116+
}
117+
```
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
## 2903\. Find Indices With Index and Value Difference I
5+
6+
Easy
7+
8+
You are given a **0-indexed** integer array `nums` having length `n`, an integer `indexDifference`, and an integer `valueDifference`.
9+
10+
Your task is to find **two** indices `i` and `j`, both in the range `[0, n - 1]`, that satisfy the following conditions:
11+
12+
* `abs(i - j) >= indexDifference`, and
13+
* `abs(nums[i] - nums[j]) >= valueDifference`
14+
15+
Return _an integer array_ `answer`, _where_ `answer = [i, j]` _if there are two such indices_, _and_ `answer = [-1, -1]` _otherwise_. If there are multiple choices for the two indices, return _any of them_.
16+
17+
**Note:** `i` and `j` may be **equal**.
18+
19+
**Example 1:**
20+
21+
**Input:** nums = [5,1,4,1], indexDifference = 2, valueDifference = 4
22+
23+
**Output:** [0,3]
24+
25+
**Explanation:** In this example, i = 0 and j = 3 can be selected.
26+
27+
abs(0 - 3) >= 2 and abs(nums[0] - nums[3]) >= 4.
28+
29+
Hence, a valid answer is [0,3]. [3,0] is also a valid answer.
30+
31+
**Example 2:**
32+
33+
**Input:** nums = [2,1], indexDifference = 0, valueDifference = 0
34+
35+
**Output:** [0,0]
36+
37+
**Explanation:** In this example, i = 0 and j = 0 can be selected.
38+
39+
abs(0 - 0) >= 0 and abs(nums[0] - nums[0]) >= 0.
40+
41+
Hence, a valid answer is [0,0].
42+
43+
Other valid answers are [0,1], [1,0], and [1,1].
44+
45+
**Example 3:**
46+
47+
**Input:** nums = [1,2,3], indexDifference = 2, valueDifference = 4
48+
49+
**Output:** [-1,-1]
50+
51+
**Explanation:** In this example, it can be shown that it is impossible to find two indices that satisfy both conditions. Hence, [-1,-1] is returned.
52+
53+
**Constraints:**
54+
55+
* `1 <= n == nums.length <= 100`
56+
* `0 <= nums[i] <= 50`
57+
* `0 <= indexDifference <= 100`
58+
* `0 <= valueDifference <= 50`
59+
60+
## Solution
61+
62+
```kotlin
63+
import kotlin.math.abs
64+
65+
class Solution {
66+
fun findIndices(nums: IntArray, indexDifference: Int, valueDifference: Int): IntArray {
67+
for (i in nums.indices) {
68+
for (j in i until nums.size) {
69+
if (j - i >= indexDifference && abs((nums[i] - nums[j]).toDouble()) >= valueDifference) {
70+
return intArrayOf(i, j)
71+
}
72+
}
73+
}
74+
return intArrayOf(-1, -1)
75+
}
76+
}
77+
```

0 commit comments

Comments
 (0)