Skip to content

Commit 262c052

Browse files
authored
Added tasks 2951-2961
1 parent c0a23d5 commit 262c052

File tree

30 files changed

+1085
-0
lines changed

30 files changed

+1085
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package g2901_3000.s2951_find_the_peaks
2+
3+
// #Easy #Array #Enumeration #2024_01_16_Time_188_ms_(93.75%)_Space_37.5_MB_(72.50%)
4+
5+
class Solution {
6+
fun findPeaks(mountain: IntArray): List<Int> {
7+
val list: MutableList<Int> = ArrayList()
8+
for (i in 1 until mountain.size - 1) {
9+
if ((mountain[i - 1] < mountain[i]) && (mountain[i] > mountain[i + 1])) {
10+
list.add(i)
11+
}
12+
}
13+
return list
14+
}
15+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2951\. Find the Peaks
2+
3+
Easy
4+
5+
You are given a **0-indexed** array `mountain`. Your task is to find all the **peaks** in the `mountain` array.
6+
7+
Return _an array that consists of_ indices _of **peaks** in the given array in **any order**._
8+
9+
**Notes:**
10+
11+
* A **peak** is defined as an element that is **strictly greater** than its neighboring elements.
12+
* The first and last elements of the array are **not** a peak.
13+
14+
**Example 1:**
15+
16+
**Input:** mountain = [2,4,4]
17+
18+
**Output:** []
19+
20+
**Explanation:** mountain[0] and mountain[2] can not be a peak because they are first and last elements of the array.
21+
22+
mountain[1] also can not be a peak because it is not strictly greater than mountain[2].
23+
24+
So the answer is [].
25+
26+
**Example 2:**
27+
28+
**Input:** mountain = [1,4,3,8,5]
29+
30+
**Output:** [1,3]
31+
32+
**Explanation:** mountain[0] and mountain[4] can not be a peak because they are first and last elements of the array.
33+
34+
mountain[2] also can not be a peak because it is not strictly greater than mountain[3] and mountain[1].
35+
36+
But mountain [1] and mountain[3] are strictly greater than their neighboring elements. So the answer is [1,3].
37+
38+
**Constraints:**
39+
40+
* `3 <= mountain.length <= 100`
41+
* `1 <= mountain[i] <= 100`
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g2901_3000.s2952_minimum_number_of_coins_to_be_added
2+
3+
// #Medium #Array #Sorting #Greedy #2024_01_16_Time_439_ms_(87.10%)_Space_59.3_MB_(61.29%)
4+
5+
class Solution {
6+
fun minimumAddedCoins(coins: IntArray, target: Int): Int {
7+
var res = 0
8+
var num = 0
9+
var i = 0
10+
coins.sort()
11+
while (num < target) {
12+
if (i < coins.size && coins[i] <= num + 1) {
13+
num += coins[i]
14+
i++
15+
} else {
16+
res += 1
17+
num += num + 1
18+
}
19+
}
20+
return res
21+
}
22+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2952\. Minimum Number of Coins to be Added
2+
3+
Medium
4+
5+
You are given a **0-indexed** integer array `coins`, representing the values of the coins available, and an integer `target`.
6+
7+
An integer `x` is **obtainable** if there exists a subsequence of `coins` that sums to `x`.
8+
9+
Return _the **minimum** number of coins **of any value** that need to be added to the array so that every integer in the range_ `[1, target]` _is **obtainable**_.
10+
11+
A **subsequence** of an array is a new **non-empty** array that is formed from the original array by deleting some (**possibly none**) of the elements without disturbing the relative positions of the remaining elements.
12+
13+
**Example 1:**
14+
15+
**Input:** coins = [1,4,10], target = 19
16+
17+
**Output:** 2
18+
19+
**Explanation:** We need to add coins 2 and 8. The resulting array will be [1,2,4,8,10].
20+
21+
It can be shown that all integers from 1 to 19 are obtainable from the resulting array, and that 2 is the minimum number of coins that need to be added to the array.
22+
23+
**Example 2:**
24+
25+
**Input:** coins = [1,4,10,5,7,19], target = 19
26+
27+
**Output:** 1
28+
29+
**Explanation:** We only need to add the coin 2. The resulting array will be [1,2,4,5,7,10,19].
30+
31+
It can be shown that all integers from 1 to 19 are obtainable from the resulting array, and that 1 is the minimum number of coins that need to be added to the array.
32+
33+
**Example 3:**
34+
35+
**Input:** coins = [1,1,1], target = 20
36+
37+
**Output:** 3
38+
39+
**Explanation:** We need to add coins 4, 8, and 16. The resulting array will be [1,1,1,4,8,16].
40+
41+
It can be shown that all integers from 1 to 20 are obtainable from the resulting array, and that 3 is the minimum number of coins that need to be added to the array.
42+
43+
**Constraints:**
44+
45+
* <code>1 <= target <= 10<sup>5</sup></code>
46+
* <code>1 <= coins.length <= 10<sup>5</sup></code>
47+
* `1 <= coins[i] <= target`
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package g2901_3000.s2953_count_complete_substrings
2+
3+
// #Hard #String #Hash_Table #Sliding_Window
4+
// #2024_01_16_Time_315_ms_(100.00%)_Space_38.7_MB_(100.00%)
5+
6+
import kotlin.math.abs
7+
8+
class Solution {
9+
fun countCompleteSubstrings(word: String, k: Int): Int {
10+
val arr = word.toCharArray()
11+
val n = arr.size
12+
var result = 0
13+
var last = 0
14+
for (i in 1..n) {
15+
if (i == n || abs((arr[i].code - arr[i - 1].code).toDouble()) > 2) {
16+
result += getCount(arr, k, last, i - 1)
17+
last = i
18+
}
19+
}
20+
return result
21+
}
22+
23+
private fun getCount(arr: CharArray, k: Int, start: Int, end: Int): Int {
24+
var result = 0
25+
var i = 1
26+
while (i <= 26 && i * k <= end - start + 1) {
27+
val cnt = IntArray(26)
28+
var good = 0
29+
for (j in start..end) {
30+
val cR = arr[j]
31+
cnt[cR.code - 'a'.code]++
32+
if (cnt[cR.code - 'a'.code] == k) {
33+
good++
34+
}
35+
if (cnt[cR.code - 'a'.code] == k + 1) {
36+
good--
37+
}
38+
if (j >= start + i * k) {
39+
val cL = arr[j - i * k]
40+
if (cnt[cL.code - 'a'.code] == k) {
41+
good--
42+
}
43+
if (cnt[cL.code - 'a'.code] == k + 1) {
44+
good++
45+
}
46+
cnt[cL.code - 'a'.code]--
47+
}
48+
if (good == i) {
49+
result++
50+
}
51+
}
52+
i++
53+
}
54+
return result
55+
}
56+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2953\. Count Complete Substrings
2+
3+
Hard
4+
5+
You are given a string `word` and an integer `k`.
6+
7+
A substring `s` of `word` is **complete** if:
8+
9+
* Each character in `s` occurs **exactly** `k` times.
10+
* The difference between two adjacent characters is **at most** `2`. That is, for any two adjacent characters `c1` and `c2` in `s`, the absolute difference in their positions in the alphabet is **at most** `2`.
11+
12+
Return _the number of **complete** substrings of_ `word`.
13+
14+
A **substring** is a **non-empty** contiguous sequence of characters in a string.
15+
16+
**Example 1:**
17+
18+
**Input:** word = "igigee", k = 2
19+
20+
**Output:** 3
21+
22+
**Explanation:** The complete substrings where each character appears exactly twice and the difference between adjacent characters is at most 2 are: <ins>**igig**</ins>ee, igig<ins>**ee**</ins>, <ins>**igigee**</ins>.
23+
24+
**Example 2:**
25+
26+
**Input:** word = "aaabbbccc", k = 3
27+
28+
**Output:** 6
29+
30+
**Explanation:** The complete substrings where each character appears exactly three times and the difference between adjacent characters is at most 2 are: **<ins>aaa</ins>**bbbccc, aaa<ins>**bbb**</ins>ccc, aaabbb<ins>**ccc**</ins>, **<ins>aaabbb</ins>**ccc, aaa<ins>**bbbccc**</ins>, <ins>**aaabbbccc**</ins>.
31+
32+
**Constraints:**
33+
34+
* <code>1 <= word.length <= 10<sup>5</sup></code>
35+
* `word` consists only of lowercase English letters.
36+
* `1 <= k <= word.length`
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package g2901_3000.s2954_count_the_number_of_infection_sequences
2+
3+
// #Hard #Array #Math #Combinatorics #2024_01_16_Time_1446_ms_(14.29%)_Space_69.1_MB_(14.29%)
4+
5+
import kotlin.math.max
6+
7+
class Solution {
8+
private val fact = LongArray(M + 1)
9+
private val invFact = LongArray(M + 1)
10+
private var init: Long = 0
11+
12+
private fun modPow(x: Int, y: Int, mod: Int): Int {
13+
if (y == 0) {
14+
return 1
15+
}
16+
var p = (modPow(x, y / 2, mod) % mod).toLong()
17+
p = (p * p) % mod
18+
return if (y % 2 == 1) (p * x % mod).toInt() else p.toInt()
19+
}
20+
21+
private fun binomCoeff(n: Int, k: Int): Long {
22+
return max(
23+
1.0,
24+
(fact[n] * invFact[k] % MOD * invFact[n - k] % MOD).toDouble()
25+
).toLong()
26+
}
27+
28+
fun numberOfSequence(n: Int, sick: IntArray): Int {
29+
if (init == 0L) {
30+
init = 1
31+
fact[0] = 1
32+
for (i in 1..M) {
33+
fact[i] = fact[i - 1] * i % MOD
34+
}
35+
invFact[M] = modPow(fact[M].toInt(), MOD - 2, MOD).toLong()
36+
for (i in M - 1 downTo 1) {
37+
invFact[i] = invFact[i + 1] * (i + 1) % MOD
38+
}
39+
}
40+
var res: Long = 1
41+
for (i in 1 until sick.size) {
42+
val group = sick[i] - sick[i - 1] - 1
43+
res = res * modPow(2, max(0.0, (group - 1).toDouble()).toInt(), MOD) % MOD
44+
res = res * binomCoeff(sick[i] - i, group) % MOD
45+
}
46+
return (res * binomCoeff(n - sick.size, n - sick[sick.size - 1] - 1) % MOD).toInt()
47+
}
48+
49+
companion object {
50+
private const val M = 100000
51+
private const val MOD = 1000000007
52+
}
53+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2954\. Count the Number of Infection Sequences
2+
3+
Hard
4+
5+
You are given an integer `n` and a **0-indexed** integer array `sick` which is **sorted** in **increasing** order.
6+
7+
There are `n` children standing in a queue with positions `0` to `n - 1` assigned to them. The array `sick` contains the positions of the children who are infected with an infectious disease. An infected child at position `i` can spread the disease to either of its immediate neighboring children at positions `i - 1` and `i + 1` **if** they exist and are currently not infected. **At most one** child who was previously not infected can get infected with the disease in one second.
8+
9+
It can be shown that after a finite number of seconds, all the children in the queue will get infected with the disease. An **infection sequence** is the sequential order of positions in which **all** of the non-infected children get infected with the disease. Return _the total number of possible infection sequences_.
10+
11+
Since the answer may be large, return it modulo <code>10<sup>9</sup> + 7</code>.
12+
13+
**Note** that an infection sequence **does not** contain positions of children who were already infected with the disease in the beginning.
14+
15+
**Example 1:**
16+
17+
**Input:** n = 5, sick = [0,4]
18+
19+
**Output:** 4
20+
21+
**Explanation:** Children at positions 1, 2, and 3 are not infected in the beginning. There are 4 possible infection sequences:
22+
- The children at positions 1 and 3 can get infected since their positions are adjacent to the infected children 0 and 4. The child at position 1 gets infected first.
23+
24+
Now, the child at position 2 is adjacent to the child at position 1 who is infected and the child at position 3 is adjacent to the child at position 4 who is infected, hence either of them can get infected. The child at position 2 gets infected. Finally, the child at position 3 gets infected because it is adjacent to children at positions 2 and 4 who are infected. The infection sequence is [1,2,3].
25+
- The children at positions 1 and 3 can get infected because their positions are adjacent to the infected children 0 and 4. The child at position 1 gets infected first.
26+
27+
Now, the child at position 2 is adjacent to the child at position 1 who is infected and the child at position 3 is adjacent to the child at position 4 who is infected, hence either of them can get infected. The child at position 3 gets infected.
28+
29+
Finally, the child at position 2 gets infected because it is adjacent to children at positions 1 and 3 who are infected. The infection sequence is [1,3,2].
30+
- The infection sequence is [3,1,2]. The order of infection of disease in the children can be seen as: [<ins>0</ins>,1,2,3,<ins>4</ins>] => [<ins>0</ins>,1,2,<ins>3</ins>,<ins>4</ins>] => [<ins>0</ins>,<ins>1</ins>,2,<ins>3</ins>,<ins>4</ins>] => [<ins>0</ins>,<ins>1</ins>,<ins>2</ins>,<ins>3</ins>,<ins>4</ins>].
31+
- The infection sequence is [3,2,1]. The order of infection of disease in the children can be seen as: [<ins>0</ins>,1,2,3,<ins>4</ins>] => [<ins>0</ins>,1,2,<ins>3</ins>,<ins>4</ins>] => [<ins>0</ins>,1,<ins>2</ins>,<ins>3</ins>,<ins>4</ins>] => [<ins>0</ins>,<ins>1</ins>,<ins>2</ins>,<ins>3</ins>,<ins>4</ins>].
32+
33+
**Example 2:**
34+
35+
**Input:** n = 4, sick = [1]
36+
37+
**Output:** 3
38+
39+
**Explanation:** Children at positions 0, 2, and 3 are not infected in the beginning. There are 3 possible infection sequences:
40+
- The infection sequence is [0,2,3]. The order of infection of disease in the children can be seen as: [0,<ins>1</ins>,2,3] => [<ins>0</ins>,<ins>1</ins>,2,3] => [<ins>0</ins>,<ins>1</ins>,<ins>2</ins>,3] => [<ins>0</ins>,<ins>1</ins>,<ins>2</ins>,<ins>3</ins>].
41+
- The infection sequence is [2,0,3]. The order of infection of disease in the children can be seen as: [0,<ins>1</ins>,2,3] => [0,<ins>1</ins>,<ins>2</ins>,3] => [<ins>0</ins>,<ins>1</ins>,<ins>2</ins>,3] => [<ins>0</ins>,<ins>1</ins>,<ins>2</ins>,<ins>3</ins>].
42+
- The infection sequence is [2,3,0]. The order of infection of disease in the children can be seen as: [0,<ins>1</ins>,2,3] => [0,<ins>1</ins>,<ins>2</ins>,3] => [0,<ins>1</ins>,<ins>2</ins>,<ins>3</ins>] => [<ins>0</ins>,<ins>1</ins>,<ins>2</ins>,<ins>3</ins>].
43+
44+
**Constraints:**
45+
46+
* <code>2 <= n <= 10<sup>5</sup></code>
47+
* `1 <= sick.length <= n - 1`
48+
* `0 <= sick[i] <= n - 1`
49+
* `sick` is sorted in increasing order.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g2901_3000.s2956_find_common_elements_between_two_arrays
2+
3+
// #Easy #Array #Hash_Table #2024_01_16_Time_271_ms_(94.20%)_Space_40.7_MB_(95.65%)
4+
5+
class Solution {
6+
fun findIntersectionValues(nums1: IntArray, nums2: IntArray): IntArray {
7+
val freq2 = IntArray(101)
8+
val freq1 = IntArray(101)
9+
val ans = IntArray(2)
10+
for (j in nums2) {
11+
freq2[j] = 1
12+
}
13+
for (j in nums1) {
14+
freq1[j] = 1
15+
ans[0] = ans[0] + freq2[j]
16+
}
17+
for (j in nums2) {
18+
ans[1] = ans[1] + freq1[j]
19+
}
20+
return ans
21+
}
22+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2956\. Find Common Elements Between Two Arrays
2+
3+
Easy
4+
5+
You are given two **0-indexed** integer arrays `nums1` and `nums2` of sizes `n` and `m`, respectively.
6+
7+
Consider calculating the following values:
8+
9+
* The number of indices `i` such that `0 <= i < n` and `nums1[i]` occurs **at least** once in `nums2`.
10+
* The number of indices `i` such that `0 <= i < m` and `nums2[i]` occurs **at least** once in `nums1`.
11+
12+
Return _an integer array_ `answer` _of size_ `2` _containing the two values **in the above order**_.
13+
14+
**Example 1:**
15+
16+
**Input:** nums1 = [4,3,2,3,1], nums2 = [2,2,5,2,3,6]
17+
18+
**Output:** [3,4]
19+
20+
**Explanation:** We calculate the values as follows:
21+
- The elements at indices 1, 2, and 3 in nums1 occur at least once in nums2. So the first value is 3.
22+
- The elements at indices 0, 1, 3, and 4 in nums2 occur at least once in nums1. So the second value is 4.
23+
24+
**Example 2:**
25+
26+
**Input:** nums1 = [3,4,2,3], nums2 = [1,5]
27+
28+
**Output:** [0,0]
29+
30+
**Explanation:** There are no common elements between the two arrays, so the two values will be 0.
31+
32+
**Constraints:**
33+
34+
* `n == nums1.length`
35+
* `m == nums2.length`
36+
* `1 <= n, m <= 100`
37+
* `1 <= nums1[i], nums2[i] <= 100`

0 commit comments

Comments
 (0)