Skip to content

Commit 5578f47

Browse files
authored
Added tasks 3082-3095
1 parent 6663cc4 commit 5578f47

File tree

30 files changed

+948
-0
lines changed

30 files changed

+948
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g3001_3100.s3082_find_the_sum_of_the_power_of_all_subsequences
2+
3+
// #Hard #Array #Dynamic_Programming #2024_04_18_Time_176_ms_(90.00%)_Space_35.3_MB_(100.00%)
4+
5+
class Solution {
6+
fun sumOfPower(nums: IntArray, k: Int): Int {
7+
val kMod = 1000000007
8+
val dp = IntArray(k + 1)
9+
dp[0] = 1
10+
for (num in nums) {
11+
for (i in k downTo 0) {
12+
if (i < num) {
13+
dp[i] = ((dp[i] * 2L) % kMod).toInt()
14+
} else {
15+
dp[i] = ((dp[i] * 2L + dp[i - num]) % kMod).toInt()
16+
}
17+
}
18+
}
19+
return dp[k]
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
3082\. Find the Sum of the Power of All Subsequences
2+
3+
Hard
4+
5+
You are given an integer array `nums` of length `n` and a **positive** integer `k`.
6+
7+
The **power** of an array of integers is defined as the number of subsequences with their sum **equal** to `k`.
8+
9+
Return _the **sum** of **power** of all subsequences of_ `nums`_._
10+
11+
Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1,2,3], k = 3
16+
17+
**Output:** 6
18+
19+
**Explanation:**
20+
21+
There are `5` subsequences of nums with non-zero power:
22+
23+
* The subsequence <code>[<ins>**1**</ins>,<ins>**2**</ins>,<ins>**3**</ins>]</code> has `2` subsequences with `sum == 3`: <code>[1,2,<ins>3</ins>]</code> and <code>[<ins>1</ins>,<ins>2</ins>,3]</code>.
24+
* The subsequence <code>[<ins>**1**</ins>,2,<ins>**3**</ins>]</code> has `1` subsequence with `sum == 3`: <code>[1,2,<ins>3</ins>]</code>.
25+
* The subsequence <code>[1,<ins>**2**</ins>,<ins>**3**</ins>]</code> has `1` subsequence with `sum == 3`: <code>[1,2,<ins>3</ins>]</code>.
26+
* The subsequence <code>[<ins>**1**</ins>,<ins>**2**</ins>,3]</code> has `1` subsequence with `sum == 3`: <code>[<ins>1</ins>,<ins>2</ins>,3]</code>.
27+
* The subsequence <code>[1,2,<ins>**3**</ins>]</code> has `1` subsequence with `sum == 3`: <code>[1,2,<ins>3</ins>]</code>.
28+
29+
Hence the answer is `2 + 1 + 1 + 1 + 1 = 6`.
30+
31+
**Example 2:**
32+
33+
**Input:** nums = [2,3,3], k = 5
34+
35+
**Output:** 4
36+
37+
**Explanation:**
38+
39+
There are `3` subsequences of nums with non-zero power:
40+
41+
* The subsequence <code>[<ins>**2**</ins>,<ins>**3**</ins>,<ins>**3**</ins>]</code> has 2 subsequences with `sum == 5`: <code>[<ins>2</ins>,3,<ins>3</ins>]</code> and <code>[<ins>2</ins>,<ins>3</ins>,3]</code>.
42+
* The subsequence <code>[<ins>**2**</ins>,3,<ins>**3**</ins>]</code> has 1 subsequence with `sum == 5`: <code>[<ins>2</ins>,3,<ins>3</ins>]</code>.
43+
* The subsequence <code>[<ins>**2**</ins>,<ins>**3**</ins>,3]</code> has 1 subsequence with `sum == 5`: <code>[<ins>2</ins>,<ins>3</ins>,3]</code>.
44+
45+
Hence the answer is `2 + 1 + 1 = 4`.
46+
47+
**Example 3:**
48+
49+
**Input:** nums = [1,2,3], k = 7
50+
51+
**Output:** 0
52+
53+
**Explanation: **There exists no subsequence with sum `7`. Hence all subsequences of nums have `power = 0`.
54+
55+
**Constraints:**
56+
57+
* `1 <= n <= 100`
58+
* <code>1 <= nums[i] <= 10<sup>4</sup></code>
59+
* `1 <= k <= 100`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3001_3100.s3083_existence_of_a_substring_in_a_string_and_its_reverse
2+
3+
// #Easy #String #Hash_Table #2024_04_18_Time_168_ms_(79.49%)_Space_37.5_MB_(24.36%)
4+
5+
class Solution {
6+
fun isSubstringPresent(s: String): Boolean {
7+
if (s.length == 1) {
8+
return false
9+
}
10+
val revSb = StringBuilder()
11+
for (i in s.length - 1 downTo 0) {
12+
revSb.append(s[i])
13+
}
14+
val rev = revSb.toString()
15+
for (i in 0 until s.length - 1) {
16+
val sub = s.substring(i, i + 2)
17+
if (rev.contains(sub)) {
18+
return true
19+
}
20+
}
21+
return false
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
3083\. Existence of a Substring in a String and Its Reverse
2+
3+
Easy
4+
5+
Given a string `s`, find any substring of length `2` which is also present in the reverse of `s`.
6+
7+
Return `true` _if such a substring exists, and_ `false` _otherwise._
8+
9+
**Example 1:**
10+
11+
**Input:** s = "leetcode"
12+
13+
**Output:** true
14+
15+
**Explanation:** Substring `"ee"` is of length `2` which is also present in `reverse(s) == "edocteel"`.
16+
17+
**Example 2:**
18+
19+
**Input:** s = "abcba"
20+
21+
**Output:** true
22+
23+
**Explanation:** All of the substrings of length `2` `"ab"`, `"bc"`, `"cb"`, `"ba"` are also present in `reverse(s) == "abcba"`.
24+
25+
**Example 3:**
26+
27+
**Input:** s = "abcd"
28+
29+
**Output:** false
30+
31+
**Explanation:** There is no substring of length `2` in `s`, which is also present in the reverse of `s`.
32+
33+
**Constraints:**
34+
35+
* `1 <= s.length <= 100`
36+
* `s` consists only of lowercase English letters.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package g3001_3100.s3084_count_substrings_starting_and_ending_with_given_character
2+
3+
// #Medium #String #Math #Counting #2024_04_18_Time_177_ms_(98.55%)_Space_38.1_MB_(37.68%)
4+
5+
class Solution {
6+
fun countSubstrings(s: String, c: Char): Long {
7+
var count: Long = 0
8+
for (element in s) {
9+
if (element == c) {
10+
count++
11+
}
12+
}
13+
return count * (count + 1) / 2
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
3084\. Count Substrings Starting and Ending with Given Character
2+
3+
Medium
4+
5+
You are given a string `s` and a character `c`. Return _the total number of substrings of_ `s` _that start and end with_ `c`_._
6+
7+
**Example 1:**
8+
9+
**Input:** s = "abada", c = "a"
10+
11+
**Output:** 6
12+
13+
**Explanation:** Substrings starting and ending with `"a"` are: <code>"<ins>**a**</ins>bada"</code>, <code>"<ins>**aba**</ins>da"</code>, <code>"<ins>**abada**</ins>"</code>, <code>"ab<ins>**a**</ins>da"</code>, <code>"ab<ins>**ada**</ins>"</code>, <code>"abad<ins>**a**</ins>"</code>.
14+
15+
**Example 2:**
16+
17+
**Input:** s = "zzz", c = "z"
18+
19+
**Output:** 6
20+
21+
**Explanation:** There are a total of `6` substrings in `s` and all start and end with `"z"`.
22+
23+
**Constraints:**
24+
25+
* <code>1 <= s.length <= 10<sup>5</sup></code>
26+
* `s` and `c` consist only of lowercase English letters.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g3001_3100.s3085_minimum_deletions_to_make_string_k_special
2+
3+
// #Medium #String #Hash_Table #Sorting #Greedy #Counting
4+
// #2024_04_18_Time_221_ms_(93.33%)_Space_38.3_MB_(93.33%)
5+
6+
import kotlin.math.min
7+
8+
class Solution {
9+
fun minimumDeletions(word: String, k: Int): Int {
10+
val arr = IntArray(26)
11+
for (element in word) {
12+
arr[element.code - 'a'.code]++
13+
}
14+
var min = Int.MAX_VALUE
15+
for (value in arr) {
16+
if (value != 0) {
17+
val u = value + k
18+
var res = 0
19+
for (i in arr) {
20+
if (i < value) {
21+
res += i
22+
} else if (i > u) {
23+
res += (i - u)
24+
}
25+
}
26+
min = min(res, min)
27+
}
28+
}
29+
return min
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
3085\. Minimum Deletions to Make String K-Special
2+
3+
Medium
4+
5+
You are given a string `word` and an integer `k`.
6+
7+
We consider `word` to be **k-special** if `|freq(word[i]) - freq(word[j])| <= k` for all indices `i` and `j` in the string.
8+
9+
Here, `freq(x)` denotes the frequency of the character `x` in `word`, and `|y|` denotes the absolute value of `y`.
10+
11+
Return _the **minimum** number of characters you need to delete to make_ `word` **_k-special_**.
12+
13+
**Example 1:**
14+
15+
**Input:** word = "aabcaba", k = 0
16+
17+
**Output:** 3
18+
19+
**Explanation:** We can make `word` `0`\-special by deleting `2` occurrences of `"a"` and `1` occurrence of `"c"`. Therefore, `word` becomes equal to `"baba"` where `freq('a') == freq('b') == 2`.
20+
21+
**Example 2:**
22+
23+
**Input:** word = "dabdcbdcdcd", k = 2
24+
25+
**Output:** 2
26+
27+
**Explanation:** We can make `word` `2`\-special by deleting `1` occurrence of `"a"` and `1` occurrence of `"d"`. Therefore, `word` becomes equal to "bdcbdcdcd" where `freq('b') == 2`, `freq('c') == 3`, and `freq('d') == 4`.
28+
29+
**Example 3:**
30+
31+
**Input:** word = "aaabaaa", k = 2
32+
33+
**Output:** 1
34+
35+
**Explanation:** We can make `word` `2`\-special by deleting `1` occurrence of `"b"`. Therefore, `word` becomes equal to `"aaaaaa"` where each letter's frequency is now uniformly `6`.
36+
37+
**Constraints:**
38+
39+
* <code>1 <= word.length <= 10<sup>5</sup></code>
40+
* <code>0 <= k <= 10<sup>5</sup></code>
41+
* `word` consists only of lowercase English letters.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package g3001_3100.s3086_minimum_moves_to_pick_k_ones
2+
3+
// #Hard #Array #Greedy #Prefix_Sum #Sliding_Window
4+
// #2024_04_18_Time_368_ms_(100.00%)_Space_56.8_MB_(100.00%)
5+
6+
import kotlin.math.max
7+
import kotlin.math.min
8+
9+
class Solution {
10+
fun minimumMoves(nums: IntArray, k: Int, maxChanges: Int): Long {
11+
var maxAdjLen = 0
12+
val n = nums.size
13+
var numOne = 0
14+
var l = 0
15+
var r = 0
16+
while (r < n) {
17+
if (nums[r] != 1) {
18+
maxAdjLen = max(maxAdjLen, (r - l))
19+
l = r + 1
20+
} else {
21+
numOne++
22+
}
23+
r++
24+
}
25+
maxAdjLen = min(3, max(maxAdjLen, (r - l)))
26+
if (maxAdjLen + maxChanges >= k) {
27+
return if (maxAdjLen >= k) {
28+
k - 1L
29+
} else {
30+
max(0, (maxAdjLen - 1)) + (k - maxAdjLen) * 2L
31+
}
32+
}
33+
val ones = IntArray(numOne)
34+
var ind = 0
35+
for (i in 0 until n) {
36+
if (nums[i] == 1) {
37+
ones[ind++] = i
38+
}
39+
}
40+
val preSum = LongArray(ones.size + 1)
41+
for (i in 1 until preSum.size) {
42+
preSum[i] = preSum[i - 1] + ones[i - 1]
43+
}
44+
val target = k - maxChanges
45+
l = 0
46+
var res = Long.MAX_VALUE
47+
while (l <= ones.size - target) {
48+
r = l + target - 1
49+
val mid = (l + r) / 2
50+
val median = ones[mid]
51+
val sum1 = preSum[mid + 1] - preSum[l]
52+
val sum2 = preSum[r + 1] - preSum[mid + 1]
53+
val area1 = (mid - l + 1).toLong() * median
54+
val area2 = (r - mid).toLong() * median
55+
val curRes = area1 - sum1 + sum2 - area2
56+
res = min(res.toDouble(), curRes.toDouble()).toLong()
57+
l++
58+
}
59+
res += 2L * maxChanges
60+
return res
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
3086\. Minimum Moves to Pick K Ones
2+
3+
Hard
4+
5+
You are given a binary array `nums` of length `n`, a **positive** integer `k` and a **non-negative** integer `maxChanges`.
6+
7+
Alice plays a game, where the goal is for Alice to pick up `k` ones from `nums` using the **minimum** number of **moves**. When the game starts, Alice picks up any index `aliceIndex` in the range `[0, n - 1]` and stands there. If `nums[aliceIndex] == 1` , Alice picks up the one and `nums[aliceIndex]` becomes `0`(this **does not** count as a move). After this, Alice can make **any** number of **moves** (**including** **zero**) where in each move Alice must perform **exactly** one of the following actions:
8+
9+
* Select any index `j != aliceIndex` such that `nums[j] == 0` and set `nums[j] = 1`. This action can be performed **at** **most** `maxChanges` times.
10+
* Select any two adjacent indices `x` and `y` (`|x - y| == 1`) such that `nums[x] == 1`, `nums[y] == 0`, then swap their values (set `nums[y] = 1` and `nums[x] = 0`). If `y == aliceIndex`, Alice picks up the one after this move and `nums[y]` becomes `0`.
11+
12+
Return _the **minimum** number of moves required by Alice to pick **exactly**_ `k` _ones_.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [1,1,0,0,0,1,1,0,0,1], k = 3, maxChanges = 1
17+
18+
**Output:** 3
19+
20+
**Explanation:** Alice can pick up `3` ones in `3` moves, if Alice performs the following actions in each move when standing at `aliceIndex == 1`:
21+
22+
* At the start of the game Alice picks up the one and `nums[1]` becomes `0`. `nums` becomes <code>[1,**<ins>1</ins>**,1,0,0,1,1,0,0,1]</code>.
23+
* Select `j == 2` and perform an action of the first type. `nums` becomes <code>[1,**<ins>0</ins>**,1,0,0,1,1,0,0,1]</code>
24+
* Select `x == 2` and `y == 1`, and perform an action of the second type. `nums` becomes <code>[1,**<ins>1</ins>**,0,0,0,1,1,0,0,1]</code>. As `y == aliceIndex`, Alice picks up the one and `nums` becomes <code>[1,**<ins>0</ins>**,0,0,0,1,1,0,0,1]</code>.
25+
* Select `x == 0` and `y == 1`, and perform an action of the second type. `nums` becomes <code>[0,**<ins>1</ins>**,0,0,0,1,1,0,0,1]</code>. As `y == aliceIndex`, Alice picks up the one and `nums` becomes <code>[0,**<ins>0</ins>**,0,0,0,1,1,0,0,1]</code>.
26+
27+
Note that it may be possible for Alice to pick up `3` ones using some other sequence of `3` moves.
28+
29+
**Example 2:**
30+
31+
**Input:** nums = [0,0,0,0], k = 2, maxChanges = 3
32+
33+
**Output:** 4
34+
35+
**Explanation:** Alice can pick up `2` ones in `4` moves, if Alice performs the following actions in each move when standing at `aliceIndex == 0`:
36+
37+
* Select `j == 1` and perform an action of the first type. `nums` becomes <code>[**<ins>0</ins>**,1,0,0]</code>.
38+
* Select `x == 1` and `y == 0`, and perform an action of the second type. `nums` becomes <code>[**<ins>1</ins>**,0,0,0]</code>. As `y == aliceIndex`, Alice picks up the one and `nums` becomes <code>[**<ins>0</ins>**,0,0,0]</code>.
39+
* Select `j == 1` again and perform an action of the first type. `nums` becomes <code>[**<ins>0</ins>**,1,0,0]</code>.
40+
* Select `x == 1` and `y == 0` again, and perform an action of the second type. `nums` becomes <code>[**<ins>1</ins>**,0,0,0]</code>. As `y == aliceIndex`, Alice picks up the one and `nums` becomes <code>[**<ins>0</ins>**,0,0,0]</code>.
41+
42+
**Constraints:**
43+
44+
* <code>2 <= n <= 10<sup>5</sup></code>
45+
* `0 <= nums[i] <= 1`
46+
* <code>1 <= k <= 10<sup>5</sup></code>
47+
* <code>0 <= maxChanges <= 10<sup>5</sup></code>
48+
* `maxChanges + sum(nums) >= k`

0 commit comments

Comments
 (0)