Skip to content

Commit 3f78b4e

Browse files
authored
Added tasks 2825-2856
1 parent d3c11e3 commit 3f78b4e

File tree

77 files changed

+2697
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+2697
-1
lines changed
File renamed without changes.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// #Easy #2023_08_02_Time_64_ms_(91.95%)_Space_42.3_MB_(96.51%)
1+
// #Easy #2023_09_15_Time_53_ms_(97.68%)_Space_43.2_MB_(25.10%)
22

33
function cancellable(fn: Function, args: any[], t: number): Function {
44
let cancelled: boolean = false
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g2801_2900.s2825_make_string_a_subsequence_using_cyclic_increments
2+
3+
// #Medium #String #Two_Pointers #2023_12_18_Time_227_ms_(83.33%)_Space_39.6_MB_(91.67%)
4+
5+
class Solution {
6+
fun canMakeSubsequence(str1: String, str2: String): Boolean {
7+
var str1ptr = 0
8+
for (element in str2) {
9+
val c2 = element
10+
var found = false
11+
while (str1ptr < str1.length) {
12+
val c1 = str1[str1ptr++]
13+
if (c1 == c2 || (c1.code - 'a'.code + 1) % 26 == c2.code - 'a'.code) {
14+
found = true
15+
break
16+
}
17+
}
18+
if (!found) {
19+
return false
20+
}
21+
}
22+
return true
23+
}
24+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2825\. Make String a Subsequence Using Cyclic Increments
2+
3+
Medium
4+
5+
You are given two **0-indexed** strings `str1` and `str2`.
6+
7+
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'`.
8+
9+
Return `true` _if it is possible to make_ `str2` _a subsequence of_ `str1` _by performing the operation **at most once**_, _and_ `false` _otherwise_.
10+
11+
**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.
12+
13+
**Example 1:**
14+
15+
**Input:** str1 = "abc", str2 = "ad"
16+
17+
**Output:** true
18+
19+
**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.
20+
21+
**Example 2:**
22+
23+
**Input:** str1 = "zc", str2 = "ad"
24+
25+
**Output:** true
26+
27+
**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.
28+
29+
**Example 3:**
30+
31+
**Input:** str1 = "ab", str2 = "d"
32+
33+
**Output:** false
34+
35+
**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.
36+
37+
**Constraints:**
38+
39+
* <code>1 <= str1.length <= 10<sup>5</sup></code>
40+
* <code>1 <= str2.length <= 10<sup>5</sup></code>
41+
* `str1` and `str2` consist of only lowercase English letters.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g2801_2900.s2826_sorting_three_groups
2+
3+
// #Medium #Array #Dynamic_Programming #2023_12_18_Time_250_ms_(100.00%)_Space_45_MB_(100.00%)
4+
5+
import kotlin.math.max
6+
7+
class Solution {
8+
fun minimumOperations(nums: List<Int>): Int {
9+
val n = nums.size
10+
val arr = IntArray(3)
11+
var max = 0
12+
for (num in nums) {
13+
var locMax = 0
14+
val value = num
15+
for (j in 0 until value) {
16+
locMax = max(locMax, arr[j])
17+
}
18+
locMax++
19+
arr[value - 1] = locMax
20+
if (locMax > max) {
21+
max = locMax
22+
}
23+
}
24+
return n - max
25+
}
26+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2826\. Sorting Three Groups
2+
3+
Medium
4+
5+
You are given a **0-indexed** integer array `nums` of length `n`.
6+
7+
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**.
8+
9+
You are allowed to perform this operation any number of times:
10+
11+
* Pick number `x` and change its group. More formally, change `nums[x]` to any number from `1` to `3`.
12+
13+
A new array `res` is constructed using the following procedure:
14+
15+
1. Sort the numbers in each group independently.
16+
2. Append the elements of groups `1`, `2`, and `3` to `res` **in this order**.
17+
18+
Array `nums` is called a **beautiful array** if the constructed array `res` is sorted in **non-decreasing** order.
19+
20+
Return _the **minimum** number of operations to make_ `nums` _a **beautiful array**_.
21+
22+
**Example 1:**
23+
24+
**Input:** nums = [2,1,3,2,1]
25+
26+
**Output:** 3
27+
28+
**Explanation:** It's optimal to perform three operations:
29+
1. change nums[0] to 1.
30+
2. change nums[2] to 1.
31+
3. change nums[3] to 1.
32+
33+
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.
34+
35+
It can be proven that there is no valid sequence of less than three operations.
36+
37+
**Example 2:**
38+
39+
**Input:** nums = [1,3,2,1,3,3]
40+
41+
**Output:** 2
42+
43+
**Explanation:** It's optimal to perform two operations:
44+
1. change nums[1] to 1.
45+
2. change nums[2] to 1.
46+
47+
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.
48+
49+
It can be proven that there is no valid sequence of less than two operations.
50+
51+
**Example 3:**
52+
53+
**Input:** nums = [2,2,2,2,3,3]
54+
55+
**Output:** 0
56+
57+
**Explanation:** It's optimal to not perform operations.
58+
59+
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.
60+
61+
**Constraints:**
62+
63+
* `1 <= nums.length <= 100`
64+
* `1 <= nums[i] <= 3`
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package g2801_2900.s2827_number_of_beautiful_integers_in_the_range
2+
3+
// #Hard #Dynamic_Programming #Math #2023_12_18_Time_169_ms_(100.00%)_Space_38.7_MB_(100.00%)
4+
5+
import kotlin.math.max
6+
7+
class Solution {
8+
private lateinit var dp: Array<Array<Array<Array<IntArray>>>>
9+
private var maxLength = 0
10+
11+
fun numberOfBeautifulIntegers(low: Int, high: Int, k: Int): Int {
12+
val num1 = low.toString()
13+
val num2 = high.toString()
14+
maxLength = max(num1.length.toDouble(), num2.length.toDouble()).toInt()
15+
dp = Array(4) { Array(maxLength) { Array(maxLength) { Array(maxLength) { IntArray(k) } } } }
16+
for (a in dp) {
17+
for (b in a) {
18+
for (c in b) {
19+
for (d in c) {
20+
d.fill(-1)
21+
}
22+
}
23+
}
24+
}
25+
return dp(num1, num2, 0, 3, 0, 0, 0, 0, k)
26+
}
27+
28+
private fun dp(
29+
low: String,
30+
high: String,
31+
i: Int,
32+
mode: Int,
33+
odd: Int,
34+
even: Int,
35+
num: Int,
36+
rem: Int,
37+
k: Int
38+
): Int {
39+
if (i == maxLength) {
40+
return if (num % k == 0 && odd == even) 1 else 0
41+
}
42+
if (dp[mode][i][odd][even][rem] != -1) {
43+
return dp[mode][i][odd][even][rem]
44+
}
45+
var res = 0
46+
val lowLimit = mode % 2 == 1
47+
val highLimit = mode / 2 == 1
48+
var start = 0
49+
var end = 9
50+
if (lowLimit) {
51+
start = digitAt(low, i)
52+
}
53+
if (highLimit) {
54+
end = digitAt(high, i)
55+
}
56+
for (j in start..end) {
57+
var newMode = 0
58+
if (j == start && lowLimit) {
59+
newMode += 1
60+
}
61+
if (j == end && highLimit) {
62+
newMode += 2
63+
}
64+
var newEven = even
65+
if (num != 0 || j != 0) {
66+
newEven += if (j % 2 == 0) 1 else 0
67+
}
68+
val newOdd = odd + (if (j % 2 == 1) 1 else 0)
69+
res +=
70+
dp(
71+
low,
72+
high,
73+
i + 1,
74+
newMode,
75+
newOdd,
76+
newEven,
77+
num * 10 + j,
78+
(num * 10 + j) % k,
79+
k
80+
)
81+
}
82+
dp[mode][i][odd][even][rem] = res
83+
return res
84+
}
85+
86+
private fun digitAt(num: String, i: Int): Int {
87+
val index = num.length - maxLength + i
88+
return if (index < 0) 0 else num[index].code - '0'.code
89+
}
90+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2827\. Number of Beautiful Integers in the Range
2+
3+
Hard
4+
5+
You are given positive integers `low`, `high`, and `k`.
6+
7+
A number is **beautiful** if it meets both of the following conditions:
8+
9+
* The count of even digits in the number is equal to the count of odd digits.
10+
* The number is divisible by `k`.
11+
12+
Return _the number of beautiful integers in the range_ `[low, high]`.
13+
14+
**Example 1:**
15+
16+
**Input:** low = 10, high = 20, k = 3
17+
18+
**Output:** 2
19+
20+
**Explanation:** There are 2 beautiful integers in the given range: [12,18].
21+
- 12 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 3.
22+
- 18 is beautiful because it contains 1 odd digit and 1 even digit, and is divisible by k = 3. Additionally we can see that:
23+
- 16 is not beautiful because it is not divisible by k = 3.
24+
- 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.
25+
26+
**Example 2:**
27+
28+
**Input:** low = 1, high = 10, k = 1
29+
30+
**Output:** 1
31+
32+
**Explanation:** There is 1 beautiful integer in the given range: [10].
33+
- 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.
34+
35+
**Example 3:**
36+
37+
**Input:** low = 5, high = 5, k = 2
38+
39+
**Output:** 0
40+
41+
**Explanation:** There are 0 beautiful integers in the given range.
42+
- 5 is not beautiful because it is not divisible by k = 2 and it does not contain equal even and odd digits.
43+
44+
**Constraints:**
45+
46+
* <code>0 < low <= high <= 10<sup>9</sup></code>
47+
* `0 < k <= 20`
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g2801_2900.s2828_check_if_a_string_is_an_acronym_of_words
2+
3+
// #Easy #Array #String #2023_12_18_Time_180_ms_(90.14%)_Space_37.7_MB_(45.07%)
4+
5+
class Solution {
6+
fun isAcronym(words: List<String>, s: String): Boolean {
7+
if (s.length != words.size) {
8+
return false
9+
}
10+
for (i in words.indices) {
11+
if (words[i][0] != s[i]) {
12+
return false
13+
}
14+
}
15+
return true
16+
}
17+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2828\. Check if a String Is an Acronym of Words
2+
3+
Easy
4+
5+
Given an array of strings `words` and a string `s`, determine if `s` is an **acronym** of words.
6+
7+
The string `s` is considered an acronym of `words` if it can be formed by concatenating the **first** character of each string in `words` **in order**. For example, `"ab"` can be formed from `["apple", "banana"]`, but it can't be formed from `["bear", "aardvark"]`.
8+
9+
Return `true` _if_ `s` _is an acronym of_ `words`_, and_ `false` _otherwise._
10+
11+
**Example 1:**
12+
13+
**Input:** words = ["alice","bob","charlie"], s = "abc"
14+
15+
**Output:** true
16+
17+
**Explanation:** The first character in the words "alice", "bob", and "charlie" are 'a', 'b', and 'c', respectively. Hence, s = "abc" is the acronym.
18+
19+
**Example 2:**
20+
21+
**Input:** words = ["an","apple"], s = "a"
22+
23+
**Output:** false
24+
25+
**Explanation:** The first character in the words "an" and "apple" are 'a' and 'a', respectively. The acronym formed by concatenating these characters is "aa". Hence, s = "a" is not the acronym.
26+
27+
**Example 3:**
28+
29+
**Input:** words = ["never","gonna","give","up","on","you"], s = "ngguoy"
30+
31+
**Output:** true
32+
33+
**Explanation:** By concatenating the first character of the words in the array, we get the string "ngguoy". Hence, s = "ngguoy" is the acronym.
34+
35+
**Constraints:**
36+
37+
* `1 <= words.length <= 100`
38+
* `1 <= words[i].length <= 10`
39+
* `1 <= s.length <= 100`
40+
* `words[i]` and `s` consist of lowercase English letters.

0 commit comments

Comments
 (0)