Skip to content

Added tasks 3082-3086 #1739

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 4 commits into from
Apr 17, 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_17_Time_1_ms_(100.00%)_Space_42.5_MB_(98.13%)

public class Solution {
public int sumOfPower(int[] nums, int k) {
final int kMod = 1_000_000_007;
int[] dp = new int[k + 1];
dp[0] = 1;
for (final int num : nums) {
for (int i = k; i >= 0; --i) {
if (i < num) {
dp[i] = (int) ((dp[i] * 2L) % kMod);
} else {
dp[i] = (int) ((dp[i] * 2L + dp[i - num]) % kMod);
}
}
}
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_17_Time_1_ms_(99.84%)_Space_41.7_MB_(96.32%)

public class Solution {
public boolean isSubstringPresent(String s) {
if (s.length() == 1) {
return false;
}
StringBuilder revSb = new StringBuilder();
for (int i = s.length() - 1; i >= 0; i--) {
revSb.append(s.charAt(i));
}
String rev = revSb.toString();
for (int i = 0; i < s.length() - 1; i++) {
String 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_17_Time_1_ms_(100.00%)_Space_44.9_MB_(30.07%)

public class Solution {
public long countSubstrings(String s, char c) {
long count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 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,29 @@
package g3001_3100.s3085_minimum_deletions_to_make_string_k_special;

// #Medium #String #Hash_Table #Sorting #Greedy #Counting
// #2024_04_17_Time_4_ms_(100.00%)_Space_45.1_MB_(94.33%)

public class Solution {
public int minimumDeletions(String word, int k) {
int[] arr = new int[26];
for (int i = 0; i < word.length(); i++) {
arr[word.charAt(i) - 'a']++;
}
int min = Integer.MAX_VALUE;
for (int value : arr) {
if (value != 0) {
int u = value + k;
int res = 0;
for (int i : arr) {
if (i < value) {
res += i;
} else if (i > u) {
res += (i - u);
}
}
min = Math.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,57 @@
package g3001_3100.s3086_minimum_moves_to_pick_k_ones;

// #Hard #Array #Greedy #Prefix_Sum #Sliding_Window
// #2024_04_17_Time_4_ms_(97.63%)_Space_61_MB_(7.12%)

public class Solution {
public long minimumMoves(int[] nums, int k, int maxChanges) {
int maxAdjLen = 0;
int n = nums.length;
int numOne = 0;
int l = 0;
int r = 0;
for (; r < n; r++) {
if (nums[r] != 1) {
maxAdjLen = Math.max(maxAdjLen, r - l);
l = r + 1;
} else {
numOne++;
}
}
maxAdjLen = Math.min(3, Math.max(maxAdjLen, r - l));
if (maxAdjLen + maxChanges >= k) {
if (maxAdjLen >= k) {
return k - 1L;
} else {
return Math.max(0, maxAdjLen - 1) + (k - maxAdjLen) * 2L;
}
}
int[] ones = new int[numOne];
int ind = 0;
for (int i = 0; i < n; i++) {
if (nums[i] == 1) {
ones[ind++] = i;
}
}
long[] preSum = new long[ones.length + 1];
for (int i = 1; i < preSum.length; i++) {
preSum[i] = preSum[i - 1] + ones[i - 1];
}
int target = k - maxChanges;
l = 0;
long res = Long.MAX_VALUE;
for (; l <= ones.length - target; l++) {
r = l + target - 1;
int mid = (l + r) / 2;
int median = ones[mid];
long sum1 = preSum[mid + 1] - preSum[l];
long sum2 = preSum[r + 1] - preSum[mid + 1];
long area1 = (long) (mid - l + 1) * median;
long area2 = (long) (r - mid) * median;
long curRes = area1 - sum1 + sum2 - area2;
res = Math.min(res, curRes);
}
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`
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g3001_3100.s3082_find_the_sum_of_the_power_of_all_subsequences;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void sumOfPower() {
assertThat(new Solution().sumOfPower(new int[] {2, 3, 3}, 5), equalTo(4));
}

@Test
void sumOfPower2() {
assertThat(new Solution().sumOfPower(new int[] {1, 2, 3}, 7), equalTo(0));
}
}
Loading
Loading