Skip to content

Commit 471d45c

Browse files
authored
Added tasks 3314-3321
1 parent d0b942a commit 471d45c

File tree

24 files changed

+1012
-0
lines changed

24 files changed

+1012
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g3301_3400.s3314_construct_the_minimum_bitwise_array_i
2+
3+
// #Easy #Array #Bit_Manipulation #2024_10_15_Time_226_ms_(57.14%)_Space_38_MB_(25.71%)
4+
5+
class Solution {
6+
fun minBitwiseArray(nums: List<Int>): IntArray {
7+
val l = nums.size
8+
val r = IntArray(l)
9+
for (i in 0 until l) {
10+
r[i] = check(nums[i])
11+
}
12+
return r
13+
}
14+
15+
private fun check(v: Int): Int {
16+
if (v % 2 == 0) {
17+
return -1
18+
}
19+
for (j in 1 until v) {
20+
if ((j or (j + 1)) == v) {
21+
return j
22+
}
23+
}
24+
return -1
25+
}
26+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
3314\. Construct the Minimum Bitwise Array I
2+
3+
Easy
4+
5+
You are given an array `nums` consisting of `n` prime integers.
6+
7+
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]`.
8+
9+
Additionally, you must **minimize** each value of `ans[i]` in the resulting array.
10+
11+
If it is _not possible_ to find such a value for `ans[i]` that satisfies the **condition**, then set `ans[i] = -1`.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [2,3,5,7]
16+
17+
**Output:** [-1,1,4,3]
18+
19+
**Explanation:**
20+
21+
* For `i = 0`, as there is no value for `ans[0]` that satisfies `ans[0] OR (ans[0] + 1) = 2`, so `ans[0] = -1`.
22+
* For `i = 1`, the smallest `ans[1]` that satisfies `ans[1] OR (ans[1] + 1) = 3` is `1`, because `1 OR (1 + 1) = 3`.
23+
* For `i = 2`, the smallest `ans[2]` that satisfies `ans[2] OR (ans[2] + 1) = 5` is `4`, because `4 OR (4 + 1) = 5`.
24+
* For `i = 3`, the smallest `ans[3]` that satisfies `ans[3] OR (ans[3] + 1) = 7` is `3`, because `3 OR (3 + 1) = 7`.
25+
26+
**Example 2:**
27+
28+
**Input:** nums = [11,13,31]
29+
30+
**Output:** [9,12,15]
31+
32+
**Explanation:**
33+
34+
* For `i = 0`, the smallest `ans[0]` that satisfies `ans[0] OR (ans[0] + 1) = 11` is `9`, because `9 OR (9 + 1) = 11`.
35+
* For `i = 1`, the smallest `ans[1]` that satisfies `ans[1] OR (ans[1] + 1) = 13` is `12`, because `12 OR (12 + 1) = 13`.
36+
* For `i = 2`, the smallest `ans[2]` that satisfies `ans[2] OR (ans[2] + 1) = 31` is `15`, because `15 OR (15 + 1) = 31`.
37+
38+
**Constraints:**
39+
40+
* `1 <= nums.length <= 100`
41+
* `2 <= nums[i] <= 1000`
42+
* `nums[i]` is a prime number.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g3301_3400.s3315_construct_the_minimum_bitwise_array_ii
2+
3+
// #Medium #Array #Bit_Manipulation #2024_10_15_Time_231_ms_(77.27%)_Space_37.1_MB_(93.18%)
4+
5+
class Solution {
6+
fun minBitwiseArray(nums: List<Int>): IntArray {
7+
val n = nums.size
8+
val result = IntArray(n)
9+
for (i in 0 until n) {
10+
val num: Int = nums[i]
11+
result[i] = -1
12+
var p = 0
13+
while (p < 31) {
14+
if (((num shr p) and 1) == 0) {
15+
break
16+
}
17+
p++
18+
}
19+
if (p > 0) {
20+
result[i] = ((num shr p) shl p) or ((1 shl (p - 1)) - 1)
21+
}
22+
}
23+
return result
24+
}
25+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
3315\. Construct the Minimum Bitwise Array II
2+
3+
Medium
4+
5+
You are given an array `nums` consisting of `n` prime integers.
6+
7+
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]`.
8+
9+
Additionally, you must **minimize** each value of `ans[i]` in the resulting array.
10+
11+
If it is _not possible_ to find such a value for `ans[i]` that satisfies the **condition**, then set `ans[i] = -1`.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [2,3,5,7]
16+
17+
**Output:** [-1,1,4,3]
18+
19+
**Explanation:**
20+
21+
* For `i = 0`, as there is no value for `ans[0]` that satisfies `ans[0] OR (ans[0] + 1) = 2`, so `ans[0] = -1`.
22+
* For `i = 1`, the smallest `ans[1]` that satisfies `ans[1] OR (ans[1] + 1) = 3` is `1`, because `1 OR (1 + 1) = 3`.
23+
* For `i = 2`, the smallest `ans[2]` that satisfies `ans[2] OR (ans[2] + 1) = 5` is `4`, because `4 OR (4 + 1) = 5`.
24+
* For `i = 3`, the smallest `ans[3]` that satisfies `ans[3] OR (ans[3] + 1) = 7` is `3`, because `3 OR (3 + 1) = 7`.
25+
26+
**Example 2:**
27+
28+
**Input:** nums = [11,13,31]
29+
30+
**Output:** [9,12,15]
31+
32+
**Explanation:**
33+
34+
* For `i = 0`, the smallest `ans[0]` that satisfies `ans[0] OR (ans[0] + 1) = 11` is `9`, because `9 OR (9 + 1) = 11`.
35+
* For `i = 1`, the smallest `ans[1]` that satisfies `ans[1] OR (ans[1] + 1) = 13` is `12`, because `12 OR (12 + 1) = 13`.
36+
* For `i = 2`, the smallest `ans[2]` that satisfies `ans[2] OR (ans[2] + 1) = 31` is `15`, because `15 OR (15 + 1) = 31`.
37+
38+
**Constraints:**
39+
40+
* `1 <= nums.length <= 100`
41+
* <code>2 <= nums[i] <= 10<sup>9</sup></code>
42+
* `nums[i]` is a prime number.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package g3301_3400.s3316_find_maximum_removals_from_source_string
2+
3+
// #Medium #Array #String #Hash_Table #Dynamic_Programming #Two_Pointers
4+
// #2024_10_15_Time_220_ms_(100.00%)_Space_37.5_MB_(45.45%)
5+
6+
import kotlin.math.max
7+
8+
class Solution {
9+
fun maxRemovals(source: String, pattern: String, targetIndices: IntArray): Int {
10+
val sChars = source.toCharArray()
11+
val sn = sChars.size
12+
val pChars = ("$pattern#").toCharArray()
13+
val pn = pattern.length
14+
var tn = targetIndices.size
15+
val maxPat = IntArray(tn + 1)
16+
var i = 0
17+
var di = 0
18+
var nextTI = targetIndices[0]
19+
while (i < sn) {
20+
val c = sChars[i]
21+
if (i == nextTI) {
22+
maxPat[di + 1] = maxPat[di]
23+
var p = maxPat[di + 1]
24+
for (j in di downTo 1) {
25+
val q = maxPat[j - 1]
26+
maxPat[j] = if (c != pChars[p]) q else max((p + 1), q)
27+
p = q
28+
}
29+
if (c == pChars[p]) {
30+
maxPat[0] = p + 1
31+
}
32+
nextTI = if (++di < tn) targetIndices[di] else -1
33+
} else {
34+
for (j in 0..di) {
35+
val p = maxPat[j]
36+
if (c == pChars[p]) {
37+
maxPat[j] = p + 1
38+
}
39+
}
40+
}
41+
i++
42+
}
43+
while (maxPat[tn] < pn) {
44+
tn--
45+
}
46+
return tn
47+
}
48+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
3316\. Find Maximum Removals From Source String
2+
3+
Medium
4+
5+
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]`.
6+
7+
We define an **operation** as removing a character at an index `idx` from `source` such that:
8+
9+
* `idx` is an element of `targetIndices`.
10+
* `pattern` remains a subsequence of `source` after removing the character.
11+
12+
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'`.
13+
14+
Return the **maximum** number of _operations_ that can be performed.
15+
16+
**Example 1:**
17+
18+
**Input:** source = "abbaa", pattern = "aba", targetIndices = [0,1,2]
19+
20+
**Output:** 1
21+
22+
**Explanation:**
23+
24+
We can't remove `source[0]` but we can do either of these two operations:
25+
26+
* Remove `source[1]`, so that `source` becomes `"a_baa"`.
27+
* Remove `source[2]`, so that `source` becomes `"ab_aa"`.
28+
29+
**Example 2:**
30+
31+
**Input:** source = "bcda", pattern = "d", targetIndices = [0,3]
32+
33+
**Output:** 2
34+
35+
**Explanation:**
36+
37+
We can remove `source[0]` and `source[3]` in two operations.
38+
39+
**Example 3:**
40+
41+
**Input:** source = "dda", pattern = "dda", targetIndices = [0,1,2]
42+
43+
**Output:** 0
44+
45+
**Explanation:**
46+
47+
We can't remove any character from `source`.
48+
49+
**Example 4:**
50+
51+
**Input:** source = "yeyeykyded", pattern = "yeyyd", targetIndices = [0,2,3,4]
52+
53+
**Output:** 2
54+
55+
**Explanation:**
56+
57+
We can remove `source[2]` and `source[3]` in two operations.
58+
59+
**Constraints:**
60+
61+
* <code>1 <= n == source.length <= 3 * 10<sup>3</sup></code>
62+
* `1 <= pattern.length <= n`
63+
* `1 <= targetIndices.length <= n`
64+
* `targetIndices` is sorted in ascending order.
65+
* The input is generated such that `targetIndices` contains distinct elements in the range `[0, n - 1]`.
66+
* `source` and `pattern` consist only of lowercase English letters.
67+
* The input is generated such that `pattern` appears as a subsequence in `source`.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package g3301_3400.s3317_find_the_number_of_possible_ways_for_an_event
2+
3+
// #Hard #Dynamic_Programming #Math #Combinatorics
4+
// #2024_10_15_Time_166_ms_(100.00%)_Space_35_MB_(100.00%)
5+
6+
import kotlin.math.min
7+
8+
class Solution {
9+
fun numberOfWays(n: Int, x: Int, y: Int): Int {
10+
val fact = LongArray(x + 1)
11+
fact[0] = 1
12+
for (i in 1..x) {
13+
fact[i] = fact[i - 1] * i % MOD
14+
}
15+
val invFact = LongArray(x + 1)
16+
invFact[x] = powMod(fact[x], MOD - 2L)
17+
for (i in x - 1 downTo 0) {
18+
invFact[i] = invFact[i + 1] * (i + 1) % MOD
19+
}
20+
val powY = LongArray(x + 1)
21+
powY[0] = 1
22+
for (k in 1..x) {
23+
powY[k] = powY[k - 1] * y % MOD
24+
}
25+
val localArray = LongArray(x + 1)
26+
localArray[0] = 1
27+
for (i in 1..n) {
28+
val kMax: Int = min(i, x)
29+
for (k in kMax downTo 1) {
30+
localArray[k] = (k * localArray[k] + localArray[k - 1]) % MOD
31+
}
32+
localArray[0] = 0
33+
}
34+
var sum: Long = 0
35+
val kLimit: Int = min(n, x)
36+
for (k in 1..kLimit) {
37+
val localValue: Long = fact[x] * invFact[x - k] % MOD
38+
var term: Long = localValue * localArray[k] % MOD
39+
term = term * powY[k] % MOD
40+
sum = (sum + term) % MOD
41+
}
42+
return sum.toInt()
43+
}
44+
45+
private fun powMod(a: Long, b: Long): Long {
46+
var a = a
47+
var b = b
48+
var res: Long = 1
49+
a = a % MOD
50+
while (b > 0) {
51+
if ((b and 1L) == 1L) {
52+
res = res * a % MOD
53+
}
54+
a = a * a % MOD
55+
b = b shr 1
56+
}
57+
return res
58+
}
59+
60+
companion object {
61+
private const val MOD = 1000000007
62+
}
63+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
3317\. Find the Number of Possible Ways for an Event
2+
3+
Hard
4+
5+
You are given three integers `n`, `x`, and `y`.
6+
7+
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**.
8+
9+
After all performances are completed, the jury will **award** each band a score in the range `[1, y]`.
10+
11+
Return the **total** number of possible ways the event can take place.
12+
13+
Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
14+
15+
**Note** that two events are considered to have been held **differently** if **either** of the following conditions is satisfied:
16+
17+
* **Any** performer is _assigned_ a different stage.
18+
* **Any** band is _awarded_ a different score.
19+
20+
**Example 1:**
21+
22+
**Input:** n = 1, x = 2, y = 3
23+
24+
**Output:** 6
25+
26+
**Explanation:**
27+
28+
* There are 2 ways to assign a stage to the performer.
29+
* The jury can award a score of either 1, 2, or 3 to the only band.
30+
31+
**Example 2:**
32+
33+
**Input:** n = 5, x = 2, y = 1
34+
35+
**Output:** 32
36+
37+
**Explanation:**
38+
39+
* Each performer will be assigned either stage 1 or stage 2.
40+
* All bands will be awarded a score of 1.
41+
42+
**Example 3:**
43+
44+
**Input:** n = 3, x = 3, y = 4
45+
46+
**Output:** 684
47+
48+
**Constraints:**
49+
50+
* `1 <= n, x, y <= 1000`

0 commit comments

Comments
 (0)