Skip to content

Commit

Permalink
Added tasks 3314-3321
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev authored Oct 15, 2024
1 parent f2b8ec1 commit 989e5b0
Show file tree
Hide file tree
Showing 24 changed files with 996 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package g3301_3400.s3314_construct_the_minimum_bitwise_array_i;

// #Easy #Array #Bit_Manipulation #2024_10_15_Time_3_ms_(92.32%)_Space_44.5_MB_(92.59%)

import java.util.List;

public class Solution {
public int[] minBitwiseArray(List<Integer> nums) {
int l = nums.size();
int[] r = new int[l];
for (int i = 0; i < l; i++) {
r[i] = check(nums.get(i));
}
return r;
}

private int check(int v) {
if (v % 2 == 0) {
return -1;
}
for (int j = 1; j < v; j++) {
if ((j | (j + 1)) == v) {
return j;
}
}
return -1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
3314\. Construct the Minimum Bitwise Array I

Easy

You are given an array `nums` consisting of `n` prime integers.

You need to construct an array `ans` of length `n`, such that, for each index `i`, the bitwise `OR` of `ans[i]` and `ans[i] + 1` is equal to `nums[i]`, i.e. `ans[i] OR (ans[i] + 1) == nums[i]`.

Additionally, you must **minimize** each value of `ans[i]` in the resulting array.

If it is _not possible_ to find such a value for `ans[i]` that satisfies the **condition**, then set `ans[i] = -1`.

**Example 1:**

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

**Output:** [-1,1,4,3]

**Explanation:**

* For `i = 0`, as there is no value for `ans[0]` that satisfies `ans[0] OR (ans[0] + 1) = 2`, so `ans[0] = -1`.
* For `i = 1`, the smallest `ans[1]` that satisfies `ans[1] OR (ans[1] + 1) = 3` is `1`, because `1 OR (1 + 1) = 3`.
* For `i = 2`, the smallest `ans[2]` that satisfies `ans[2] OR (ans[2] + 1) = 5` is `4`, because `4 OR (4 + 1) = 5`.
* For `i = 3`, the smallest `ans[3]` that satisfies `ans[3] OR (ans[3] + 1) = 7` is `3`, because `3 OR (3 + 1) = 7`.

**Example 2:**

**Input:** nums = [11,13,31]

**Output:** [9,12,15]

**Explanation:**

* For `i = 0`, the smallest `ans[0]` that satisfies `ans[0] OR (ans[0] + 1) = 11` is `9`, because `9 OR (9 + 1) = 11`.
* For `i = 1`, the smallest `ans[1]` that satisfies `ans[1] OR (ans[1] + 1) = 13` is `12`, because `12 OR (12 + 1) = 13`.
* For `i = 2`, the smallest `ans[2]` that satisfies `ans[2] OR (ans[2] + 1) = 31` is `15`, because `15 OR (15 + 1) = 31`.

**Constraints:**

* `1 <= nums.length <= 100`
* `2 <= nums[i] <= 1000`
* `nums[i]` is a prime number.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package g3301_3400.s3315_construct_the_minimum_bitwise_array_ii;

// #Medium #Array #Bit_Manipulation #2024_10_15_Time_1_ms_(100.00%)_Space_44.8_MB_(77.74%)

import java.util.List;

public class Solution {
public int[] minBitwiseArray(List<Integer> nums) {
final int n = nums.size();
int[] result = new int[n];
for (int i = 0; i < n; i++) {
int num = nums.get(i);
result[i] = -1;
int p = 0;
for (; p < 31; p++) {
if (((num >> p) & 1) == 0) {
break;
}
}
if (p > 0) {
result[i] = ((num >> p) << p) | ((1 << (p - 1)) - 1);
}
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
3315\. Construct the Minimum Bitwise Array II

Medium

You are given an array `nums` consisting of `n` prime integers.

You need to construct an array `ans` of length `n`, such that, for each index `i`, the bitwise `OR` of `ans[i]` and `ans[i] + 1` is equal to `nums[i]`, i.e. `ans[i] OR (ans[i] + 1) == nums[i]`.

Additionally, you must **minimize** each value of `ans[i]` in the resulting array.

If it is _not possible_ to find such a value for `ans[i]` that satisfies the **condition**, then set `ans[i] = -1`.

**Example 1:**

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

**Output:** [-1,1,4,3]

**Explanation:**

* For `i = 0`, as there is no value for `ans[0]` that satisfies `ans[0] OR (ans[0] + 1) = 2`, so `ans[0] = -1`.
* For `i = 1`, the smallest `ans[1]` that satisfies `ans[1] OR (ans[1] + 1) = 3` is `1`, because `1 OR (1 + 1) = 3`.
* For `i = 2`, the smallest `ans[2]` that satisfies `ans[2] OR (ans[2] + 1) = 5` is `4`, because `4 OR (4 + 1) = 5`.
* For `i = 3`, the smallest `ans[3]` that satisfies `ans[3] OR (ans[3] + 1) = 7` is `3`, because `3 OR (3 + 1) = 7`.

**Example 2:**

**Input:** nums = [11,13,31]

**Output:** [9,12,15]

**Explanation:**

* For `i = 0`, the smallest `ans[0]` that satisfies `ans[0] OR (ans[0] + 1) = 11` is `9`, because `9 OR (9 + 1) = 11`.
* For `i = 1`, the smallest `ans[1]` that satisfies `ans[1] OR (ans[1] + 1) = 13` is `12`, because `12 OR (12 + 1) = 13`.
* For `i = 2`, the smallest `ans[2]` that satisfies `ans[2] OR (ans[2] + 1) = 31` is `15`, because `15 OR (15 + 1) = 31`.

**Constraints:**

* `1 <= nums.length <= 100`
* <code>2 <= nums[i] <= 10<sup>9</sup></code>
* `nums[i]` is a prime number.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package g3301_3400.s3316_find_maximum_removals_from_source_string;

// #Medium #Array #String #Hash_Table #Dynamic_Programming #Two_Pointers
// #2024_10_15_Time_10_ms_(100.00%)_Space_44.6_MB_(41.97%)

public class Solution {
public int maxRemovals(String source, String pattern, int[] targetIndices) {
char[] sChars = source.toCharArray();
int sn = sChars.length;
char[] pChars = (pattern + '#').toCharArray();
int pn = pattern.length();
int tn = targetIndices.length;
int[] maxPat = new int[tn + 1];
int i = 0;
int di = 0;
int nextTI = targetIndices[0];
while (i < sn) {
char c = sChars[i];
if (i == nextTI) {
int p = maxPat[di + 1] = maxPat[di];
for (int j = di; j > 0; j--) {
int q = maxPat[j - 1];
maxPat[j] = c != pChars[p] ? q : Math.max(p + 1, q);
p = q;
}
if (c == pChars[p]) {
maxPat[0] = p + 1;
}
nextTI = ++di < tn ? targetIndices[di] : -1;
} else {
for (int j = 0; j <= di; j++) {
int p = maxPat[j];
if (c == pChars[p]) {
maxPat[j] = p + 1;
}
}
}
i++;
}
while (maxPat[tn] < pn) {
tn--;
}
return tn;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
3316\. Find Maximum Removals From Source String

Medium

You are given a string `source` of size `n`, a string `pattern` that is a subsequence of `source`, and a **sorted** integer array `targetIndices` that contains **distinct** numbers in the range `[0, n - 1]`.

We define an **operation** as removing a character at an index `idx` from `source` such that:

* `idx` is an element of `targetIndices`.
* `pattern` remains a subsequence of `source` after removing the character.

Performing an operation **does not** change the indices of the other characters in `source`. For example, if you remove `'c'` from `"acb"`, the character at index 2 would still be `'b'`.

Return the **maximum** number of _operations_ that can be performed.

**Example 1:**

**Input:** source = "abbaa", pattern = "aba", targetIndices = [0,1,2]

**Output:** 1

**Explanation:**

We can't remove `source[0]` but we can do either of these two operations:

* Remove `source[1]`, so that `source` becomes `"a_baa"`.
* Remove `source[2]`, so that `source` becomes `"ab_aa"`.

**Example 2:**

**Input:** source = "bcda", pattern = "d", targetIndices = [0,3]

**Output:** 2

**Explanation:**

We can remove `source[0]` and `source[3]` in two operations.

**Example 3:**

**Input:** source = "dda", pattern = "dda", targetIndices = [0,1,2]

**Output:** 0

**Explanation:**

We can't remove any character from `source`.

**Example 4:**

**Input:** source = "yeyeykyded", pattern = "yeyyd", targetIndices = [0,2,3,4]

**Output:** 2

**Explanation:**

We can remove `source[2]` and `source[3]` in two operations.

**Constraints:**

* <code>1 <= n == source.length <= 3 * 10<sup>3</sup></code>
* `1 <= pattern.length <= n`
* `1 <= targetIndices.length <= n`
* `targetIndices` is sorted in ascending order.
* The input is generated such that `targetIndices` contains distinct elements in the range `[0, n - 1]`.
* `source` and `pattern` consist only of lowercase English letters.
* The input is generated such that `pattern` appears as a subsequence in `source`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package g3301_3400.s3317_find_the_number_of_possible_ways_for_an_event;

// #Hard #Dynamic_Programming #Math #Combinatorics
// #2024_10_15_Time_20_ms_(97.08%)_Space_41.6_MB_(97.66%)

public class Solution {
private static final int MOD = 1_000_000_007;

public int numberOfWays(int n, int x, int y) {
long[] fact = new long[x + 1];
fact[0] = 1;
for (int i = 1; i <= x; i++) {
fact[i] = fact[i - 1] * i % MOD;
}
long[] invFact = new long[x + 1];
invFact[x] = powMod(fact[x], MOD - 2L);
for (int i = x - 1; i >= 0; i--) {
invFact[i] = invFact[i + 1] * (i + 1) % MOD;
}
long[] powY = new long[x + 1];
powY[0] = 1;
for (int k = 1; k <= x; k++) {
powY[k] = powY[k - 1] * y % MOD;
}
long[] localArray = new long[x + 1];
localArray[0] = 1;
for (int i = 1; i <= n; i++) {
int kMax = Math.min(i, x);
for (int k = kMax; k >= 1; k--) {
localArray[k] = (k * localArray[k] + localArray[k - 1]) % MOD;
}
localArray[0] = 0;
}
long sum = 0;
int kLimit = Math.min(n, x);
for (int k = 1; k <= kLimit; k++) {
long localValue = fact[x] * invFact[x - k] % MOD;
long term = localValue * localArray[k] % MOD;
term = term * powY[k] % MOD;
sum = (sum + term) % MOD;
}
return (int) sum;
}

private long powMod(long a, long b) {
long res = 1;
a = a % MOD;
while (b > 0) {
if ((b & 1) == 1) {
res = res * a % MOD;
}
a = a * a % MOD;
b >>= 1;
}
return res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
3317\. Find the Number of Possible Ways for an Event

Hard

You are given three integers `n`, `x`, and `y`.

An event is being held for `n` performers. When a performer arrives, they are **assigned** to one of the `x` stages. All performers assigned to the **same** stage will perform together as a band, though some stages _might_ remain **empty**.

After all performances are completed, the jury will **award** each band a score in the range `[1, y]`.

Return the **total** number of possible ways the event can take place.

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

**Note** that two events are considered to have been held **differently** if **either** of the following conditions is satisfied:

* **Any** performer is _assigned_ a different stage.
* **Any** band is _awarded_ a different score.

**Example 1:**

**Input:** n = 1, x = 2, y = 3

**Output:** 6

**Explanation:**

* There are 2 ways to assign a stage to the performer.
* The jury can award a score of either 1, 2, or 3 to the only band.

**Example 2:**

**Input:** n = 5, x = 2, y = 1

**Output:** 32

**Explanation:**

* Each performer will be assigned either stage 1 or stage 2.
* All bands will be awarded a score of 1.

**Example 3:**

**Input:** n = 3, x = 3, y = 4

**Output:** 684

**Constraints:**

* `1 <= n, x, y <= 1000`
Loading

0 comments on commit 989e5b0

Please sign in to comment.