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
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1816,6 +1816,35 @@

| # | Title | Difficulty | Tag | Time, ms | Time, %
|------|----------------|-------------|-------------|----------|--------
| 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
| 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
| 2947 |[Count Beautiful Substrings I](src/main/kotlin/g2901_3000/s2947_count_beautiful_substrings_i)| Medium | String, Prefix_Sum, Enumeration | 291 | 59.52
| 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
| 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
| 2929 |[Distribute Candies Among Children II](src/main/kotlin/g2901_3000/s2929_distribute_candies_among_children_ii)| Medium | Math, Enumeration, Combinatorics | 193 | 18.18
| 2928 |[Distribute Candies Among Children I](src/main/kotlin/g2901_3000/s2928_distribute_candies_among_children_i)| Easy | Math, Enumeration, Combinatorics | 282 | 6.38
| 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
| 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
| 2924 |[Find Champion II](src/main/kotlin/g2901_3000/s2924_find_champion_ii)| Medium | Graph | 724 | 6.98
| 2923 |[Find Champion I](src/main/kotlin/g2901_3000/s2923_find_champion_i)| Easy | Array, Matrix | 591 | 5.63
| 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
| 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
| 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
| 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
| 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
| 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
| 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
| 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
| 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
| 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
| 2909 |[Minimum Sum of Mountain Triplets II](src/main/kotlin/g2901_3000/s2909_minimum_sum_of_mountain_triplets_ii)| Medium | Array | 463 | 100.00
| 2908 |[Minimum Sum of Mountain Triplets I](src/main/kotlin/g2901_3000/s2908_minimum_sum_of_mountain_triplets_i)| Easy | Array | 174 | 73.33
| 2906 |[Construct Product Matrix](src/main/kotlin/g2901_3000/s2906_construct_product_matrix)| Medium | Array, Matrix, Prefix_Sum | 1016 | 100.00
| 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
| 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
| 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
| 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
| 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
| 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
| 2899 |[Last Visited Integers](src/main/kotlin/g2801_2900/s2899_last_visited_integers)| Easy | Array, String, Simulation | 209 | 72.73
| 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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
[![](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)

## 2901\. Longest Unequal Adjacent Groups Subsequence II

Medium

You are given an integer `n`, a **0-indexed** string array `words`, and a **0-indexed** array `groups`, both arrays having length `n`.

The **hamming distance** between two strings of equal length is the number of positions at which the corresponding characters are **different**.

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:

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

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

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.

**Note:** strings in `words` may be **unequal** in length.

**Example 1:**

**Input:** n = 3, words = ["bab","dab","cab"], groups = [1,2,2]

**Output:** ["bab","cab"]

**Explanation:** A subsequence that can be selected is [0,2].
- groups[0] != groups[2]
- words[0].length == words[2].length, and the hamming distance between them is 1.

So, a valid answer is [words[0],words[2]] = ["bab","cab"]. Another subsequence that can be selected is [0,1].
- groups[0] != groups[1]
- words[0].length == words[1].length, and the hamming distance between them is 1.

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.

**Example 2:**

**Input:** n = 4, words = ["a","b","c","d"], groups = [1,2,3,4]

**Output:** ["a","b","c","d"]

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

**Constraints:**

* `1 <= n == words.length == groups.length <= 1000`
* `1 <= words[i].length <= 10`
* `1 <= groups[i] <= n`
* `words` consists of **distinct** strings.
* `words[i]` consists of lowercase English letters.

## Solution

```kotlin
class Solution {
fun getWordsInLongestSubsequence(n: Int, words: Array<String>, groups: IntArray): List<String> {
val check = IntArray(groups.size)
val before = IntArray(groups.size)
check.fill(1)
before.fill(-1)
var index = 0
var max = 1
for (i in 1 until n) {
for (j in i - 1 downTo 0) {
if (groups[i] != groups[j] && ham(words[i], words[j]) && check[j] + 1 > check[i]) {
check[i] = check[j] + 1
before[i] = j
if (check[i] > max) {
max = check[i]
index = i
}
}
}
}
val ans: MutableList<String> = ArrayList()
while (index >= 0) {
ans.add(words[index])
index = before[index]
}
ans.reverse()
return ans
}

private fun ham(s1: String, s2: String): Boolean {
if (s1.length != s2.length) {
return false
}
var count = 0
for (i in s1.indices) {
if (s1[i] != s2[i]) {
count++
}
if (count > 1) {
return false
}
}
return count == 1
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
[![](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)

## 2902\. Count of Sub-Multisets With Bounded Sum

Hard

You are given a **0-indexed** array `nums` of non-negative integers, and two integers `l` and `r`.

Return _the **count of sub-multisets** within_ `nums` _where the sum of elements in each subset falls within the inclusive range of_ `[l, r]`.

Since the answer may be large, return it modulo <code>10<sup>9</sup> + 7</code>.

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.

**Note** that:

* Two **sub-multisets** are the same if sorting both sub-multisets results in identical multisets.
* The sum of an **empty** multiset is `0`.

**Example 1:**

**Input:** nums = [1,2,2,3], l = 6, r = 6

**Output:** 1

**Explanation:** The only subset of nums that has a sum of 6 is {1, 2, 3}.

**Example 2:**

**Input:** nums = [2,1,4,2,7], l = 1, r = 5

**Output:** 7

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

**Example 3:**

**Input:** nums = [1,2,1,3,5,2], l = 3, r = 5

**Output:** 9

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

**Constraints:**

* <code>1 <= nums.length <= 2 * 10<sup>4</sup></code>
* <code>0 <= nums[i] <= 2 * 10<sup>4</sup></code>
* Sum of `nums` does not exceed <code>2 * 10<sup>4</sup></code>.
* <code>0 <= l <= r <= 2 * 10<sup>4</sup></code>

## Solution

```kotlin
class Solution {
private var map: HashMap<Int, Int>? = null
private lateinit var dp: Array<IntArray>

private fun solve(al: List<Int>, l: Int, r: Int, index: Int, sum: Int): Int {
if (sum > r) {
return 0
}
var ans: Long = 0
if (index >= al.size) {
return ans.toInt()
}
if (dp[index][sum] != -1) {
return dp[index][sum]
}
val cur = al[index]
val count = map!![cur]!!
for (i in 0..count) {
val curSum = sum + cur * i
if (curSum > r) {
break
}
ans += solve(al, l, r, index + 1, curSum)
if (i != 0 && curSum >= l) {
ans += 1
}
ans %= MOD
}
dp[index][sum] = ans.toInt()
return ans.toInt()
}

fun countSubMultisets(nums: List<Int>, l: Int, r: Int): Int {
map = HashMap()
val al: MutableList<Int> = ArrayList()
for (cur in nums) {
val count = map!!.getOrDefault(cur, 0) + 1
map!![cur] = count
if (count == 1) {
al.add(cur)
}
}
val n = al.size
dp = Array(n) { IntArray(r + 1) }
for (i in dp.indices) {
for (j in dp[0].indices) {
dp[i][j] = -1
}
}
al.sort()
var ans = solve(al, l, r, 0, 0)
if (l == 0) {
ans += 1
}
ans %= MOD
return ans
}

companion object {
private const val MOD = 1e9.toInt() + 7
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
[![](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)

## 2903\. Find Indices With Index and Value Difference I

Easy

You are given a **0-indexed** integer array `nums` having length `n`, an integer `indexDifference`, and an integer `valueDifference`.

Your task is to find **two** indices `i` and `j`, both in the range `[0, n - 1]`, that satisfy the following conditions:

* `abs(i - j) >= indexDifference`, and
* `abs(nums[i] - nums[j]) >= valueDifference`

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

**Note:** `i` and `j` may be **equal**.

**Example 1:**

**Input:** nums = [5,1,4,1], indexDifference = 2, valueDifference = 4

**Output:** [0,3]

**Explanation:** In this example, i = 0 and j = 3 can be selected.

abs(0 - 3) >= 2 and abs(nums[0] - nums[3]) >= 4.

Hence, a valid answer is [0,3]. [3,0] is also a valid answer.

**Example 2:**

**Input:** nums = [2,1], indexDifference = 0, valueDifference = 0

**Output:** [0,0]

**Explanation:** In this example, i = 0 and j = 0 can be selected.

abs(0 - 0) >= 0 and abs(nums[0] - nums[0]) >= 0.

Hence, a valid answer is [0,0].

Other valid answers are [0,1], [1,0], and [1,1].

**Example 3:**

**Input:** nums = [1,2,3], indexDifference = 2, valueDifference = 4

**Output:** [-1,-1]

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

**Constraints:**

* `1 <= n == nums.length <= 100`
* `0 <= nums[i] <= 50`
* `0 <= indexDifference <= 100`
* `0 <= valueDifference <= 50`

## Solution

```kotlin
import kotlin.math.abs

class Solution {
fun findIndices(nums: IntArray, indexDifference: Int, valueDifference: Int): IntArray {
for (i in nums.indices) {
for (j in i until nums.size) {
if (j - i >= indexDifference && abs((nums[i] - nums[j]).toDouble()) >= valueDifference) {
return intArrayOf(i, j)
}
}
}
return intArrayOf(-1, -1)
}
}
```
Loading