Skip to content

Added tasks 3314-3321 #704

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 2 commits into from
Oct 15, 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,26 @@
package g3301_3400.s3314_construct_the_minimum_bitwise_array_i

// #Easy #Array #Bit_Manipulation #2024_10_15_Time_226_ms_(57.14%)_Space_38_MB_(25.71%)

class Solution {
fun minBitwiseArray(nums: List<Int>): IntArray {
val l = nums.size
val r = IntArray(l)
for (i in 0 until l) {
r[i] = check(nums[i])
}
return r
}

private fun check(v: Int): Int {
if (v % 2 == 0) {
return -1
}
for (j in 1 until v) {
if ((j or (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,25 @@
package g3301_3400.s3315_construct_the_minimum_bitwise_array_ii

// #Medium #Array #Bit_Manipulation #2024_10_15_Time_231_ms_(77.27%)_Space_37.1_MB_(93.18%)

class Solution {
fun minBitwiseArray(nums: List<Int>): IntArray {
val n = nums.size
val result = IntArray(n)
for (i in 0 until n) {
val num: Int = nums[i]
result[i] = -1
var p = 0
while (p < 31) {
if (((num shr p) and 1) == 0) {
break
}
p++
}
if (p > 0) {
result[i] = ((num shr p) shl p) or ((1 shl (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,48 @@
package g3301_3400.s3316_find_maximum_removals_from_source_string

// #Medium #Array #String #Hash_Table #Dynamic_Programming #Two_Pointers
// #2024_10_15_Time_220_ms_(100.00%)_Space_37.5_MB_(45.45%)

import kotlin.math.max

class Solution {
fun maxRemovals(source: String, pattern: String, targetIndices: IntArray): Int {
val sChars = source.toCharArray()
val sn = sChars.size
val pChars = ("$pattern#").toCharArray()
val pn = pattern.length
var tn = targetIndices.size
val maxPat = IntArray(tn + 1)
var i = 0
var di = 0
var nextTI = targetIndices[0]
while (i < sn) {
val c = sChars[i]
if (i == nextTI) {
maxPat[di + 1] = maxPat[di]
var p = maxPat[di + 1]
for (j in di downTo 1) {
val q = maxPat[j - 1]
maxPat[j] = if (c != pChars[p]) q else max((p + 1), q)
p = q
}
if (c == pChars[p]) {
maxPat[0] = p + 1
}
nextTI = if (++di < tn) targetIndices[di] else -1
} else {
for (j in 0..di) {
val 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,63 @@
package g3301_3400.s3317_find_the_number_of_possible_ways_for_an_event

// #Hard #Dynamic_Programming #Math #Combinatorics
// #2024_10_15_Time_166_ms_(100.00%)_Space_35_MB_(100.00%)

import kotlin.math.min

class Solution {
fun numberOfWays(n: Int, x: Int, y: Int): Int {
val fact = LongArray(x + 1)
fact[0] = 1
for (i in 1..x) {
fact[i] = fact[i - 1] * i % MOD
}
val invFact = LongArray(x + 1)
invFact[x] = powMod(fact[x], MOD - 2L)
for (i in x - 1 downTo 0) {
invFact[i] = invFact[i + 1] * (i + 1) % MOD
}
val powY = LongArray(x + 1)
powY[0] = 1
for (k in 1..x) {
powY[k] = powY[k - 1] * y % MOD
}
val localArray = LongArray(x + 1)
localArray[0] = 1
for (i in 1..n) {
val kMax: Int = min(i, x)
for (k in kMax downTo 1) {
localArray[k] = (k * localArray[k] + localArray[k - 1]) % MOD
}
localArray[0] = 0
}
var sum: Long = 0
val kLimit: Int = min(n, x)
for (k in 1..kLimit) {
val localValue: Long = fact[x] * invFact[x - k] % MOD
var term: Long = localValue * localArray[k] % MOD
term = term * powY[k] % MOD
sum = (sum + term) % MOD
}
return sum.toInt()
}

private fun powMod(a: Long, b: Long): Long {
var a = a
var b = b
var res: Long = 1
a = a % MOD
while (b > 0) {
if ((b and 1L) == 1L) {
res = res * a % MOD
}
a = a * a % MOD
b = b shr 1
}
return res
}

companion object {
private const val MOD = 1000000007
}
}
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