Skip to content

Added tasks 3082-3095 #627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 18, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package g3001_3100.s3082_find_the_sum_of_the_power_of_all_subsequences

// #Hard #Array #Dynamic_Programming #2024_04_18_Time_176_ms_(90.00%)_Space_35.3_MB_(100.00%)

class Solution {
fun sumOfPower(nums: IntArray, k: Int): Int {
val kMod = 1000000007
val dp = IntArray(k + 1)
dp[0] = 1
for (num in nums) {
for (i in k downTo 0) {
if (i < num) {
dp[i] = ((dp[i] * 2L) % kMod).toInt()
} else {
dp[i] = ((dp[i] * 2L + dp[i - num]) % kMod).toInt()
}
}
}
return dp[k]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
3082\. Find the Sum of the Power of All Subsequences

Hard

You are given an integer array `nums` of length `n` and a **positive** integer `k`.

The **power** of an array of integers is defined as the number of subsequences with their sum **equal** to `k`.

Return _the **sum** of **power** of all subsequences of_ `nums`_._

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

**Example 1:**

**Input:** nums = [1,2,3], k = 3

**Output:** 6

**Explanation:**

There are `5` subsequences of nums with non-zero power:

* 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>.
* The subsequence <code>[<ins>**1**</ins>,2,<ins>**3**</ins>]</code> has `1` subsequence with `sum == 3`: <code>[1,2,<ins>3</ins>]</code>.
* The subsequence <code>[1,<ins>**2**</ins>,<ins>**3**</ins>]</code> has `1` subsequence with `sum == 3`: <code>[1,2,<ins>3</ins>]</code>.
* 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>.
* The subsequence <code>[1,2,<ins>**3**</ins>]</code> has `1` subsequence with `sum == 3`: <code>[1,2,<ins>3</ins>]</code>.

Hence the answer is `2 + 1 + 1 + 1 + 1 = 6`.

**Example 2:**

**Input:** nums = [2,3,3], k = 5

**Output:** 4

**Explanation:**

There are `3` subsequences of nums with non-zero power:

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

Hence the answer is `2 + 1 + 1 = 4`.

**Example 3:**

**Input:** nums = [1,2,3], k = 7

**Output:** 0

**Explanation: **There exists no subsequence with sum `7`. Hence all subsequences of nums have `power = 0`.

**Constraints:**

* `1 <= n <= 100`
* <code>1 <= nums[i] <= 10<sup>4</sup></code>
* `1 <= k <= 100`
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package g3001_3100.s3083_existence_of_a_substring_in_a_string_and_its_reverse

// #Easy #String #Hash_Table #2024_04_18_Time_168_ms_(79.49%)_Space_37.5_MB_(24.36%)

class Solution {
fun isSubstringPresent(s: String): Boolean {
if (s.length == 1) {
return false
}
val revSb = StringBuilder()
for (i in s.length - 1 downTo 0) {
revSb.append(s[i])
}
val rev = revSb.toString()
for (i in 0 until s.length - 1) {
val sub = s.substring(i, i + 2)
if (rev.contains(sub)) {
return true
}
}
return false
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
3083\. Existence of a Substring in a String and Its Reverse

Easy

Given a string `s`, find any substring of length `2` which is also present in the reverse of `s`.

Return `true` _if such a substring exists, and_ `false` _otherwise._

**Example 1:**

**Input:** s = "leetcode"

**Output:** true

**Explanation:** Substring `"ee"` is of length `2` which is also present in `reverse(s) == "edocteel"`.

**Example 2:**

**Input:** s = "abcba"

**Output:** true

**Explanation:** All of the substrings of length `2` `"ab"`, `"bc"`, `"cb"`, `"ba"` are also present in `reverse(s) == "abcba"`.

**Example 3:**

**Input:** s = "abcd"

**Output:** false

**Explanation:** There is no substring of length `2` in `s`, which is also present in the reverse of `s`.

**Constraints:**

* `1 <= s.length <= 100`
* `s` consists only of lowercase English letters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package g3001_3100.s3084_count_substrings_starting_and_ending_with_given_character

// #Medium #String #Math #Counting #2024_04_18_Time_177_ms_(98.55%)_Space_38.1_MB_(37.68%)

class Solution {
fun countSubstrings(s: String, c: Char): Long {
var count: Long = 0
for (element in s) {
if (element == c) {
count++
}
}
return count * (count + 1) / 2
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
3084\. Count Substrings Starting and Ending with Given Character

Medium

You are given a string `s` and a character `c`. Return _the total number of substrings of_ `s` _that start and end with_ `c`_._

**Example 1:**

**Input:** s = "abada", c = "a"

**Output:** 6

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

**Example 2:**

**Input:** s = "zzz", c = "z"

**Output:** 6

**Explanation:** There are a total of `6` substrings in `s` and all start and end with `"z"`.

**Constraints:**

* <code>1 <= s.length <= 10<sup>5</sup></code>
* `s` and `c` consist only of lowercase English letters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package g3001_3100.s3085_minimum_deletions_to_make_string_k_special

// #Medium #String #Hash_Table #Sorting #Greedy #Counting
// #2024_04_18_Time_221_ms_(93.33%)_Space_38.3_MB_(93.33%)

import kotlin.math.min

class Solution {
fun minimumDeletions(word: String, k: Int): Int {
val arr = IntArray(26)
for (element in word) {
arr[element.code - 'a'.code]++
}
var min = Int.MAX_VALUE
for (value in arr) {
if (value != 0) {
val u = value + k
var res = 0
for (i in arr) {
if (i < value) {
res += i
} else if (i > u) {
res += (i - u)
}
}
min = min(res, min)
}
}
return min
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
3085\. Minimum Deletions to Make String K-Special

Medium

You are given a string `word` and an integer `k`.

We consider `word` to be **k-special** if `|freq(word[i]) - freq(word[j])| <= k` for all indices `i` and `j` in the string.

Here, `freq(x)` denotes the frequency of the character `x` in `word`, and `|y|` denotes the absolute value of `y`.

Return _the **minimum** number of characters you need to delete to make_ `word` **_k-special_**.

**Example 1:**

**Input:** word = "aabcaba", k = 0

**Output:** 3

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

**Example 2:**

**Input:** word = "dabdcbdcdcd", k = 2

**Output:** 2

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

**Example 3:**

**Input:** word = "aaabaaa", k = 2

**Output:** 1

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

**Constraints:**

* <code>1 <= word.length <= 10<sup>5</sup></code>
* <code>0 <= k <= 10<sup>5</sup></code>
* `word` consists only of lowercase English letters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package g3001_3100.s3086_minimum_moves_to_pick_k_ones

// #Hard #Array #Greedy #Prefix_Sum #Sliding_Window
// #2024_04_18_Time_368_ms_(100.00%)_Space_56.8_MB_(100.00%)

import kotlin.math.max
import kotlin.math.min

class Solution {
fun minimumMoves(nums: IntArray, k: Int, maxChanges: Int): Long {
var maxAdjLen = 0
val n = nums.size
var numOne = 0
var l = 0
var r = 0
while (r < n) {
if (nums[r] != 1) {
maxAdjLen = max(maxAdjLen, (r - l))
l = r + 1
} else {
numOne++
}
r++
}
maxAdjLen = min(3, max(maxAdjLen, (r - l)))
if (maxAdjLen + maxChanges >= k) {
return if (maxAdjLen >= k) {
k - 1L
} else {
max(0, (maxAdjLen - 1)) + (k - maxAdjLen) * 2L
}
}
val ones = IntArray(numOne)
var ind = 0
for (i in 0 until n) {
if (nums[i] == 1) {
ones[ind++] = i
}
}
val preSum = LongArray(ones.size + 1)
for (i in 1 until preSum.size) {
preSum[i] = preSum[i - 1] + ones[i - 1]
}
val target = k - maxChanges
l = 0
var res = Long.MAX_VALUE
while (l <= ones.size - target) {
r = l + target - 1
val mid = (l + r) / 2
val median = ones[mid]
val sum1 = preSum[mid + 1] - preSum[l]
val sum2 = preSum[r + 1] - preSum[mid + 1]
val area1 = (mid - l + 1).toLong() * median
val area2 = (r - mid).toLong() * median
val curRes = area1 - sum1 + sum2 - area2
res = min(res.toDouble(), curRes.toDouble()).toLong()
l++
}
res += 2L * maxChanges
return res
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
3086\. Minimum Moves to Pick K Ones

Hard

You are given a binary array `nums` of length `n`, a **positive** integer `k` and a **non-negative** integer `maxChanges`.

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:

* Select any index `j != aliceIndex` such that `nums[j] == 0` and set `nums[j] = 1`. This action can be performed **at** **most** `maxChanges` times.
* 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`.

Return _the **minimum** number of moves required by Alice to pick **exactly**_ `k` _ones_.

**Example 1:**

**Input:** nums = [1,1,0,0,0,1,1,0,0,1], k = 3, maxChanges = 1

**Output:** 3

**Explanation:** Alice can pick up `3` ones in `3` moves, if Alice performs the following actions in each move when standing at `aliceIndex == 1`:

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

Note that it may be possible for Alice to pick up `3` ones using some other sequence of `3` moves.

**Example 2:**

**Input:** nums = [0,0,0,0], k = 2, maxChanges = 3

**Output:** 4

**Explanation:** Alice can pick up `2` ones in `4` moves, if Alice performs the following actions in each move when standing at `aliceIndex == 0`:

* Select `j == 1` and perform an action of the first type. `nums` becomes <code>[**<ins>0</ins>**,1,0,0]</code>.
* 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>.
* Select `j == 1` again and perform an action of the first type. `nums` becomes <code>[**<ins>0</ins>**,1,0,0]</code>.
* 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>.

**Constraints:**

* <code>2 <= n <= 10<sup>5</sup></code>
* `0 <= nums[i] <= 1`
* <code>1 <= k <= 10<sup>5</sup></code>
* <code>0 <= maxChanges <= 10<sup>5</sup></code>
* `maxChanges + sum(nums) >= k`
Loading