Skip to content

Commit b84ccda

Browse files
authored
Added tasks 3314-3321
1 parent 36f3768 commit b84ccda

File tree

9 files changed

+833
-0
lines changed
  • src/main/kotlin/g3301_3400
    • s3314_construct_the_minimum_bitwise_array_i
    • s3315_construct_the_minimum_bitwise_array_ii
    • s3316_find_maximum_removals_from_source_string
    • s3317_find_the_number_of_possible_ways_for_an_event
    • s3318_find_x_sum_of_all_k_long_subarrays_i
    • s3319_k_th_largest_perfect_subtree_size_in_binary_tree
    • s3320_count_the_number_of_winning_sequences
    • s3321_find_x_sum_of_all_k_long_subarrays_ii

9 files changed

+833
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,6 +1816,14 @@
18161816

18171817
| # | Title | Difficulty | Tag | Time, ms | Time, %
18181818
|------|----------------|-------------|-------------|----------|--------
1819+
| 3321 |[Find X-Sum of All K-Long Subarrays II](src/main/kotlin/g3301_3400/s3321_find_x_sum_of_all_k_long_subarrays_ii)| Hard | Array, Hash_Table, Heap_Priority_Queue, Sliding_Window | 1660 | 100.00
1820+
| 3320 |[Count The Number of Winning Sequences](src/main/kotlin/g3301_3400/s3320_count_the_number_of_winning_sequences)| Hard | String, Dynamic_Programming | 335 | 100.00
1821+
| 3319 |[K-th Largest Perfect Subtree Size in Binary Tree](src/main/kotlin/g3301_3400/s3319_k_th_largest_perfect_subtree_size_in_binary_tree)| Medium | Sorting, Depth_First_Search, Tree, Binary_Tree | 332 | 45.45
1822+
| 3318 |[Find X-Sum of All K-Long Subarrays I](src/main/kotlin/g3301_3400/s3318_find_x_sum_of_all_k_long_subarrays_i)| Easy | Array, Hash_Table, Heap_Priority_Queue, Sliding_Window | 262 | 86.21
1823+
| 3317 |[Find the Number of Possible Ways for an Event](src/main/kotlin/g3301_3400/s3317_find_the_number_of_possible_ways_for_an_event)| Hard | Dynamic_Programming, Math, Combinatorics | 166 | 100.00
1824+
| 3316 |[Find Maximum Removals From Source String](src/main/kotlin/g3301_3400/s3316_find_maximum_removals_from_source_string)| Medium | Array, String, Hash_Table, Dynamic_Programming, Two_Pointers | 220 | 100.00
1825+
| 3315 |[Construct the Minimum Bitwise Array II](src/main/kotlin/g3301_3400/s3315_construct_the_minimum_bitwise_array_ii)| Medium | Array, Bit_Manipulation | 231 | 77.27
1826+
| 3314 |[Construct the Minimum Bitwise Array I](src/main/kotlin/g3301_3400/s3314_construct_the_minimum_bitwise_array_i)| Easy | Array, Bit_Manipulation | 226 | 57.14
18191827
| 3312 |[Sorted GCD Pair Queries](src/main/kotlin/g3301_3400/s3312_sorted_gcd_pair_queries)| Hard | Array, Hash_Table, Math, Binary_Search, Prefix_Sum, Counting, Number_Theory, Combinatorics | 734 | 100.00
18201828
| 3311 |[Construct 2D Grid Matching Graph Layout](src/main/kotlin/g3301_3400/s3311_construct_2d_grid_matching_graph_layout)| Hard | Array, Hash_Table, Matrix, Graph | 1423 | 100.00
18211829
| 3310 |[Remove Methods From Project](src/main/kotlin/g3301_3400/s3310_remove_methods_from_project)| Medium | Depth_First_Search, Breadth_First_Search, Graph | 1465 | 100.00
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3314\. Construct the Minimum Bitwise Array I
5+
6+
Easy
7+
8+
You are given an array `nums` consisting of `n` prime integers.
9+
10+
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]`.
11+
12+
Additionally, you must **minimize** each value of `ans[i]` in the resulting array.
13+
14+
If it is _not possible_ to find such a value for `ans[i]` that satisfies the **condition**, then set `ans[i] = -1`.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [2,3,5,7]
19+
20+
**Output:** [-1,1,4,3]
21+
22+
**Explanation:**
23+
24+
* For `i = 0`, as there is no value for `ans[0]` that satisfies `ans[0] OR (ans[0] + 1) = 2`, so `ans[0] = -1`.
25+
* For `i = 1`, the smallest `ans[1]` that satisfies `ans[1] OR (ans[1] + 1) = 3` is `1`, because `1 OR (1 + 1) = 3`.
26+
* For `i = 2`, the smallest `ans[2]` that satisfies `ans[2] OR (ans[2] + 1) = 5` is `4`, because `4 OR (4 + 1) = 5`.
27+
* For `i = 3`, the smallest `ans[3]` that satisfies `ans[3] OR (ans[3] + 1) = 7` is `3`, because `3 OR (3 + 1) = 7`.
28+
29+
**Example 2:**
30+
31+
**Input:** nums = [11,13,31]
32+
33+
**Output:** [9,12,15]
34+
35+
**Explanation:**
36+
37+
* For `i = 0`, the smallest `ans[0]` that satisfies `ans[0] OR (ans[0] + 1) = 11` is `9`, because `9 OR (9 + 1) = 11`.
38+
* For `i = 1`, the smallest `ans[1]` that satisfies `ans[1] OR (ans[1] + 1) = 13` is `12`, because `12 OR (12 + 1) = 13`.
39+
* For `i = 2`, the smallest `ans[2]` that satisfies `ans[2] OR (ans[2] + 1) = 31` is `15`, because `15 OR (15 + 1) = 31`.
40+
41+
**Constraints:**
42+
43+
* `1 <= nums.length <= 100`
44+
* `2 <= nums[i] <= 1000`
45+
* `nums[i]` is a prime number.
46+
47+
## Solution
48+
49+
```kotlin
50+
class Solution {
51+
fun minBitwiseArray(nums: List<Int>): IntArray {
52+
val l = nums.size
53+
val r = IntArray(l)
54+
for (i in 0 until l) {
55+
r[i] = check(nums[i])
56+
}
57+
return r
58+
}
59+
60+
private fun check(v: Int): Int {
61+
if (v % 2 == 0) {
62+
return -1
63+
}
64+
for (j in 1 until v) {
65+
if ((j or (j + 1)) == v) {
66+
return j
67+
}
68+
}
69+
return -1
70+
}
71+
}
72+
```
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3315\. Construct the Minimum Bitwise Array II
5+
6+
Medium
7+
8+
You are given an array `nums` consisting of `n` prime integers.
9+
10+
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]`.
11+
12+
Additionally, you must **minimize** each value of `ans[i]` in the resulting array.
13+
14+
If it is _not possible_ to find such a value for `ans[i]` that satisfies the **condition**, then set `ans[i] = -1`.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [2,3,5,7]
19+
20+
**Output:** [-1,1,4,3]
21+
22+
**Explanation:**
23+
24+
* For `i = 0`, as there is no value for `ans[0]` that satisfies `ans[0] OR (ans[0] + 1) = 2`, so `ans[0] = -1`.
25+
* For `i = 1`, the smallest `ans[1]` that satisfies `ans[1] OR (ans[1] + 1) = 3` is `1`, because `1 OR (1 + 1) = 3`.
26+
* For `i = 2`, the smallest `ans[2]` that satisfies `ans[2] OR (ans[2] + 1) = 5` is `4`, because `4 OR (4 + 1) = 5`.
27+
* For `i = 3`, the smallest `ans[3]` that satisfies `ans[3] OR (ans[3] + 1) = 7` is `3`, because `3 OR (3 + 1) = 7`.
28+
29+
**Example 2:**
30+
31+
**Input:** nums = [11,13,31]
32+
33+
**Output:** [9,12,15]
34+
35+
**Explanation:**
36+
37+
* For `i = 0`, the smallest `ans[0]` that satisfies `ans[0] OR (ans[0] + 1) = 11` is `9`, because `9 OR (9 + 1) = 11`.
38+
* For `i = 1`, the smallest `ans[1]` that satisfies `ans[1] OR (ans[1] + 1) = 13` is `12`, because `12 OR (12 + 1) = 13`.
39+
* For `i = 2`, the smallest `ans[2]` that satisfies `ans[2] OR (ans[2] + 1) = 31` is `15`, because `15 OR (15 + 1) = 31`.
40+
41+
**Constraints:**
42+
43+
* `1 <= nums.length <= 100`
44+
* <code>2 <= nums[i] <= 10<sup>9</sup></code>
45+
* `nums[i]` is a prime number.
46+
47+
## Solution
48+
49+
```kotlin
50+
class Solution {
51+
fun minBitwiseArray(nums: List<Int>): IntArray {
52+
val n = nums.size
53+
val result = IntArray(n)
54+
for (i in 0 until n) {
55+
val num: Int = nums[i]
56+
result[i] = -1
57+
var p = 0
58+
while (p < 31) {
59+
if (((num shr p) and 1) == 0) {
60+
break
61+
}
62+
p++
63+
}
64+
if (p > 0) {
65+
result[i] = ((num shr p) shl p) or ((1 shl (p - 1)) - 1)
66+
}
67+
}
68+
return result
69+
}
70+
}
71+
```
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3316\. Find Maximum Removals From Source String
5+
6+
Medium
7+
8+
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]`.
9+
10+
We define an **operation** as removing a character at an index `idx` from `source` such that:
11+
12+
* `idx` is an element of `targetIndices`.
13+
* `pattern` remains a subsequence of `source` after removing the character.
14+
15+
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'`.
16+
17+
Return the **maximum** number of _operations_ that can be performed.
18+
19+
**Example 1:**
20+
21+
**Input:** source = "abbaa", pattern = "aba", targetIndices = [0,1,2]
22+
23+
**Output:** 1
24+
25+
**Explanation:**
26+
27+
We can't remove `source[0]` but we can do either of these two operations:
28+
29+
* Remove `source[1]`, so that `source` becomes `"a_baa"`.
30+
* Remove `source[2]`, so that `source` becomes `"ab_aa"`.
31+
32+
**Example 2:**
33+
34+
**Input:** source = "bcda", pattern = "d", targetIndices = [0,3]
35+
36+
**Output:** 2
37+
38+
**Explanation:**
39+
40+
We can remove `source[0]` and `source[3]` in two operations.
41+
42+
**Example 3:**
43+
44+
**Input:** source = "dda", pattern = "dda", targetIndices = [0,1,2]
45+
46+
**Output:** 0
47+
48+
**Explanation:**
49+
50+
We can't remove any character from `source`.
51+
52+
**Example 4:**
53+
54+
**Input:** source = "yeyeykyded", pattern = "yeyyd", targetIndices = [0,2,3,4]
55+
56+
**Output:** 2
57+
58+
**Explanation:**
59+
60+
We can remove `source[2]` and `source[3]` in two operations.
61+
62+
**Constraints:**
63+
64+
* <code>1 <= n == source.length <= 3 * 10<sup>3</sup></code>
65+
* `1 <= pattern.length <= n`
66+
* `1 <= targetIndices.length <= n`
67+
* `targetIndices` is sorted in ascending order.
68+
* The input is generated such that `targetIndices` contains distinct elements in the range `[0, n - 1]`.
69+
* `source` and `pattern` consist only of lowercase English letters.
70+
* The input is generated such that `pattern` appears as a subsequence in `source`.
71+
72+
## Solution
73+
74+
```kotlin
75+
import kotlin.math.max
76+
77+
class Solution {
78+
fun maxRemovals(source: String, pattern: String, targetIndices: IntArray): Int {
79+
val sChars = source.toCharArray()
80+
val sn = sChars.size
81+
val pChars = ("$pattern#").toCharArray()
82+
val pn = pattern.length
83+
var tn = targetIndices.size
84+
val maxPat = IntArray(tn + 1)
85+
var i = 0
86+
var di = 0
87+
var nextTI = targetIndices[0]
88+
while (i < sn) {
89+
val c = sChars[i]
90+
if (i == nextTI) {
91+
maxPat[di + 1] = maxPat[di]
92+
var p = maxPat[di + 1]
93+
for (j in di downTo 1) {
94+
val q = maxPat[j - 1]
95+
maxPat[j] = if (c != pChars[p]) q else max((p + 1), q)
96+
p = q
97+
}
98+
if (c == pChars[p]) {
99+
maxPat[0] = p + 1
100+
}
101+
nextTI = if (++di < tn) targetIndices[di] else -1
102+
} else {
103+
for (j in 0..di) {
104+
val p = maxPat[j]
105+
if (c == pChars[p]) {
106+
maxPat[j] = p + 1
107+
}
108+
}
109+
}
110+
i++
111+
}
112+
while (maxPat[tn] < pn) {
113+
tn--
114+
}
115+
return tn
116+
}
117+
}
118+
```
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3317\. Find the Number of Possible Ways for an Event
5+
6+
Hard
7+
8+
You are given three integers `n`, `x`, and `y`.
9+
10+
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**.
11+
12+
After all performances are completed, the jury will **award** each band a score in the range `[1, y]`.
13+
14+
Return the **total** number of possible ways the event can take place.
15+
16+
Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
17+
18+
**Note** that two events are considered to have been held **differently** if **either** of the following conditions is satisfied:
19+
20+
* **Any** performer is _assigned_ a different stage.
21+
* **Any** band is _awarded_ a different score.
22+
23+
**Example 1:**
24+
25+
**Input:** n = 1, x = 2, y = 3
26+
27+
**Output:** 6
28+
29+
**Explanation:**
30+
31+
* There are 2 ways to assign a stage to the performer.
32+
* The jury can award a score of either 1, 2, or 3 to the only band.
33+
34+
**Example 2:**
35+
36+
**Input:** n = 5, x = 2, y = 1
37+
38+
**Output:** 32
39+
40+
**Explanation:**
41+
42+
* Each performer will be assigned either stage 1 or stage 2.
43+
* All bands will be awarded a score of 1.
44+
45+
**Example 3:**
46+
47+
**Input:** n = 3, x = 3, y = 4
48+
49+
**Output:** 684
50+
51+
**Constraints:**
52+
53+
* `1 <= n, x, y <= 1000`
54+
55+
## Solution
56+
57+
```kotlin
58+
import kotlin.math.min
59+
60+
class Solution {
61+
fun numberOfWays(n: Int, x: Int, y: Int): Int {
62+
val fact = LongArray(x + 1)
63+
fact[0] = 1
64+
for (i in 1..x) {
65+
fact[i] = fact[i - 1] * i % MOD
66+
}
67+
val invFact = LongArray(x + 1)
68+
invFact[x] = powMod(fact[x], MOD - 2L)
69+
for (i in x - 1 downTo 0) {
70+
invFact[i] = invFact[i + 1] * (i + 1) % MOD
71+
}
72+
val powY = LongArray(x + 1)
73+
powY[0] = 1
74+
for (k in 1..x) {
75+
powY[k] = powY[k - 1] * y % MOD
76+
}
77+
val localArray = LongArray(x + 1)
78+
localArray[0] = 1
79+
for (i in 1..n) {
80+
val kMax: Int = min(i, x)
81+
for (k in kMax downTo 1) {
82+
localArray[k] = (k * localArray[k] + localArray[k - 1]) % MOD
83+
}
84+
localArray[0] = 0
85+
}
86+
var sum: Long = 0
87+
val kLimit: Int = min(n, x)
88+
for (k in 1..kLimit) {
89+
val localValue: Long = fact[x] * invFact[x - k] % MOD
90+
var term: Long = localValue * localArray[k] % MOD
91+
term = term * powY[k] % MOD
92+
sum = (sum + term) % MOD
93+
}
94+
return sum.toInt()
95+
}
96+
97+
private fun powMod(a: Long, b: Long): Long {
98+
var a = a
99+
var b = b
100+
var res: Long = 1
101+
a = a % MOD
102+
while (b > 0) {
103+
if ((b and 1L) == 1L) {
104+
res = res * a % MOD
105+
}
106+
a = a * a % MOD
107+
b = b shr 1
108+
}
109+
return res
110+
}
111+
112+
companion object {
113+
private const val MOD = 1000000007
114+
}
115+
}
116+
```

0 commit comments

Comments
 (0)