Skip to content

Commit f5c573a

Browse files
authored
Added tasks 2901-2913
1 parent 23c2759 commit f5c573a

File tree

33 files changed

+1341
-0
lines changed

33 files changed

+1341
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package g2901_3000.s2901_longest_unequal_adjacent_groups_subsequence_ii
2+
3+
// #Medium #Array #String #Dynamic_Programming
4+
// #2023_12_27_Time_305_ms_(100.00%)_Space_47.6_MB_(75.00%)
5+
6+
class Solution {
7+
fun getWordsInLongestSubsequence(n: Int, words: Array<String>, groups: IntArray): List<String> {
8+
val check = IntArray(groups.size)
9+
val before = IntArray(groups.size)
10+
check.fill(1)
11+
before.fill(-1)
12+
var index = 0
13+
var max = 1
14+
for (i in 1 until n) {
15+
for (j in i - 1 downTo 0) {
16+
if (groups[i] != groups[j] && ham(words[i], words[j]) && check[j] + 1 > check[i]) {
17+
check[i] = check[j] + 1
18+
before[i] = j
19+
if (check[i] > max) {
20+
max = check[i]
21+
index = i
22+
}
23+
}
24+
}
25+
}
26+
val ans: MutableList<String> = ArrayList()
27+
while (index >= 0) {
28+
ans.add(words[index])
29+
index = before[index]
30+
}
31+
ans.reverse()
32+
return ans
33+
}
34+
35+
private fun ham(s1: String, s2: String): Boolean {
36+
if (s1.length != s2.length) {
37+
return false
38+
}
39+
var count = 0
40+
for (i in s1.indices) {
41+
if (s1[i] != s2[i]) {
42+
count++
43+
}
44+
if (count > 1) {
45+
return false
46+
}
47+
}
48+
return count == 1
49+
}
50+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2901\. Longest Unequal Adjacent Groups Subsequence II
2+
3+
Medium
4+
5+
You are given an integer `n`, a **0-indexed** string array `words`, and a **0-indexed** array `groups`, both arrays having length `n`.
6+
7+
The **hamming distance** between two strings of equal length is the number of positions at which the corresponding characters are **different**.
8+
9+
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:
10+
11+
* 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`.
12+
* <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.
13+
14+
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_.
15+
16+
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.
17+
18+
**Note:** strings in `words` may be **unequal** in length.
19+
20+
**Example 1:**
21+
22+
**Input:** n = 3, words = ["bab","dab","cab"], groups = [1,2,2]
23+
24+
**Output:** ["bab","cab"]
25+
26+
**Explanation:** A subsequence that can be selected is [0,2].
27+
- groups[0] != groups[2]
28+
- words[0].length == words[2].length, and the hamming distance between them is 1.
29+
30+
So, a valid answer is [words[0],words[2]] = ["bab","cab"]. Another subsequence that can be selected is [0,1].
31+
- groups[0] != groups[1]
32+
- words[0].length == words[1].length, and the hamming distance between them is 1.
33+
34+
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.
35+
36+
**Example 2:**
37+
38+
**Input:** n = 4, words = ["a","b","c","d"], groups = [1,2,3,4]
39+
40+
**Output:** ["a","b","c","d"]
41+
42+
**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.
43+
44+
**Constraints:**
45+
46+
* `1 <= n == words.length == groups.length <= 1000`
47+
* `1 <= words[i].length <= 10`
48+
* `1 <= groups[i] <= n`
49+
* `words` consists of **distinct** strings.
50+
* `words[i]` consists of lowercase English letters.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package g2901_3000.s2902_count_of_sub_multisets_with_bounded_sum
2+
3+
// #Hard #Array #Hash_Table #Dynamic_Programming #Sliding_Window
4+
// #2023_12_27_Time_2416_ms_(100.00%)_Space_87.8_MB_(100.00%)
5+
6+
class Solution {
7+
private var map: HashMap<Int, Int>? = null
8+
private lateinit var dp: Array<IntArray>
9+
10+
private fun solve(al: List<Int>, l: Int, r: Int, index: Int, sum: Int): Int {
11+
if (sum > r) {
12+
return 0
13+
}
14+
var ans: Long = 0
15+
if (index >= al.size) {
16+
return ans.toInt()
17+
}
18+
if (dp[index][sum] != -1) {
19+
return dp[index][sum]
20+
}
21+
val cur = al[index]
22+
val count = map!![cur]!!
23+
for (i in 0..count) {
24+
val curSum = sum + cur * i
25+
if (curSum > r) {
26+
break
27+
}
28+
ans += solve(al, l, r, index + 1, curSum)
29+
if (i != 0 && curSum >= l) {
30+
ans += 1
31+
}
32+
ans %= MOD
33+
}
34+
dp[index][sum] = ans.toInt()
35+
return ans.toInt()
36+
}
37+
38+
fun countSubMultisets(nums: List<Int>, l: Int, r: Int): Int {
39+
map = HashMap()
40+
val al: MutableList<Int> = ArrayList()
41+
for (cur in nums) {
42+
val count = map!!.getOrDefault(cur, 0) + 1
43+
map!![cur] = count
44+
if (count == 1) {
45+
al.add(cur)
46+
}
47+
}
48+
val n = al.size
49+
dp = Array(n) { IntArray(r + 1) }
50+
for (i in dp.indices) {
51+
for (j in dp[0].indices) {
52+
dp[i][j] = -1
53+
}
54+
}
55+
al.sort()
56+
var ans = solve(al, l, r, 0, 0)
57+
if (l == 0) {
58+
ans += 1
59+
}
60+
ans %= MOD
61+
return ans
62+
}
63+
64+
companion object {
65+
private const val MOD = 1e9.toInt() + 7
66+
}
67+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2902\. Count of Sub-Multisets With Bounded Sum
2+
3+
Hard
4+
5+
You are given a **0-indexed** array `nums` of non-negative integers, and two integers `l` and `r`.
6+
7+
Return _the **count of sub-multisets** within_ `nums` _where the sum of elements in each subset falls within the inclusive range of_ `[l, r]`.
8+
9+
Since the answer may be large, return it modulo <code>10<sup>9</sup> + 7</code>.
10+
11+
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.
12+
13+
**Note** that:
14+
15+
* Two **sub-multisets** are the same if sorting both sub-multisets results in identical multisets.
16+
* The sum of an **empty** multiset is `0`.
17+
18+
**Example 1:**
19+
20+
**Input:** nums = [1,2,2,3], l = 6, r = 6
21+
22+
**Output:** 1
23+
24+
**Explanation:** The only subset of nums that has a sum of 6 is {1, 2, 3}.
25+
26+
**Example 2:**
27+
28+
**Input:** nums = [2,1,4,2,7], l = 1, r = 5
29+
30+
**Output:** 7
31+
32+
**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}.
33+
34+
**Example 3:**
35+
36+
**Input:** nums = [1,2,1,3,5,2], l = 3, r = 5
37+
38+
**Output:** 9
39+
40+
**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}.
41+
42+
**Constraints:**
43+
44+
* <code>1 <= nums.length <= 2 * 10<sup>4</sup></code>
45+
* <code>0 <= nums[i] <= 2 * 10<sup>4</sup></code>
46+
* Sum of `nums` does not exceed <code>2 * 10<sup>4</sup></code>.
47+
* <code>0 <= l <= r <= 2 * 10<sup>4</sup></code>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2901_3000.s2903_find_indices_with_index_and_value_difference_i
2+
3+
// #Easy #Array #2023_12_27_Time_192_ms_(100.00%)_Space_37_MB_(100.00%)
4+
5+
import kotlin.math.abs
6+
7+
class Solution {
8+
fun findIndices(nums: IntArray, indexDifference: Int, valueDifference: Int): IntArray {
9+
for (i in nums.indices) {
10+
for (j in i until nums.size) {
11+
if (j - i >= indexDifference && abs((nums[i] - nums[j]).toDouble()) >= valueDifference) {
12+
return intArrayOf(i, j)
13+
}
14+
}
15+
}
16+
return intArrayOf(-1, -1)
17+
}
18+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2903\. Find Indices With Index and Value Difference I
2+
3+
Easy
4+
5+
You are given a **0-indexed** integer array `nums` having length `n`, an integer `indexDifference`, and an integer `valueDifference`.
6+
7+
Your task is to find **two** indices `i` and `j`, both in the range `[0, n - 1]`, that satisfy the following conditions:
8+
9+
* `abs(i - j) >= indexDifference`, and
10+
* `abs(nums[i] - nums[j]) >= valueDifference`
11+
12+
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_.
13+
14+
**Note:** `i` and `j` may be **equal**.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [5,1,4,1], indexDifference = 2, valueDifference = 4
19+
20+
**Output:** [0,3]
21+
22+
**Explanation:** In this example, i = 0 and j = 3 can be selected.
23+
24+
abs(0 - 3) >= 2 and abs(nums[0] - nums[3]) >= 4.
25+
26+
Hence, a valid answer is [0,3]. [3,0] is also a valid answer.
27+
28+
**Example 2:**
29+
30+
**Input:** nums = [2,1], indexDifference = 0, valueDifference = 0
31+
32+
**Output:** [0,0]
33+
34+
**Explanation:** In this example, i = 0 and j = 0 can be selected.
35+
36+
abs(0 - 0) >= 0 and abs(nums[0] - nums[0]) >= 0.
37+
38+
Hence, a valid answer is [0,0].
39+
40+
Other valid answers are [0,1], [1,0], and [1,1].
41+
42+
**Example 3:**
43+
44+
**Input:** nums = [1,2,3], indexDifference = 2, valueDifference = 4
45+
46+
**Output:** [-1,-1]
47+
48+
**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.
49+
50+
**Constraints:**
51+
52+
* `1 <= n == nums.length <= 100`
53+
* `0 <= nums[i] <= 50`
54+
* `0 <= indexDifference <= 100`
55+
* `0 <= valueDifference <= 50`
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package g2901_3000.s2904_shortest_and_lexicographically_smallest_beautiful_string
2+
3+
// #Medium #String #Sliding_Window #2023_12_27_Time_169_ms_(66.67%)_Space_35.1_MB_(83.33%)
4+
5+
@Suppress("NAME_SHADOWING")
6+
class Solution {
7+
private var n = 0
8+
9+
private fun nextOne(s: String, i: Int): Int {
10+
var i = i
11+
i++
12+
while (i < n) {
13+
if (s[i] == '1') {
14+
return i
15+
}
16+
i++
17+
}
18+
return -1
19+
}
20+
21+
fun shortestBeautifulSubstring(s: String, k: Int): String {
22+
n = s.length
23+
var i = nextOne(s, -1)
24+
var j = i
25+
var c = 1
26+
while (c != k && j != -1) {
27+
j = nextOne(s, j)
28+
c++
29+
}
30+
if (c != k || j == -1) {
31+
return ""
32+
}
33+
var min = j - i + 1
34+
var r = s.substring(i, i + min)
35+
i = nextOne(s, i)
36+
j = nextOne(s, j)
37+
while (j != -1) {
38+
val temp = j - i + 1
39+
if (temp < min) {
40+
min = j - i + 1
41+
r = s.substring(i, i + min)
42+
} else if (temp == min) {
43+
val r1 = s.substring(i, i + min)
44+
if (r1.compareTo(r) < 0) {
45+
r = r1
46+
}
47+
}
48+
i = nextOne(s, i)
49+
j = nextOne(s, j)
50+
}
51+
return r
52+
}
53+
}

0 commit comments

Comments
 (0)