Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 39794f0

Browse files
Added tasks 2951-2956
1 parent bc858fa commit 39794f0

File tree

15 files changed

+473
-0
lines changed

15 files changed

+473
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2901_3000.s2951_find_the_peaks;
2+
3+
// #Easy #Array #Enumeration #2024_01_15_Time_1_ms_(100.00%)_Space_43.8_MB_(79.61%)
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class Solution {
9+
public List<Integer> findPeaks(int[] mountain) {
10+
List<Integer> list = new ArrayList<>();
11+
for (int i = 1; i < mountain.length - 1; i++) {
12+
if ((mountain[i - 1] < mountain[i]) && (mountain[i] > mountain[i + 1])) {
13+
list.add(i);
14+
}
15+
}
16+
return list;
17+
}
18+
}
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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g2901_3000.s2952_minimum_number_of_coins_to_be_added;
2+
3+
// #Medium #Array #Sorting #Greedy #2024_01_15_Time_21_ms_(98.49%)_Space_61.9_MB_(5.41%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public int minimumAddedCoins(int[] coins, int target) {
9+
int res = 0;
10+
int num = 0;
11+
int i = 0;
12+
Arrays.sort(coins);
13+
while (num < target) {
14+
if (i < coins.length && coins[i] <= num + 1) {
15+
num += coins[i];
16+
i++;
17+
} else {
18+
res += 1;
19+
num += num + 1;
20+
}
21+
}
22+
return res;
23+
}
24+
}
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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package g2901_3000.s2953_count_complete_substrings;
2+
3+
// #Hard #String #Hash_Table #Sliding_Window #2024_01_15_Time_107_ms_(89.47%)_Space_47_MB_(42.61%)
4+
5+
public class Solution {
6+
public int countCompleteSubstrings(String word, int k) {
7+
char[] arr = word.toCharArray();
8+
int n = arr.length;
9+
int result = 0;
10+
int last = 0;
11+
for (int i = 1; i <= n; i++) {
12+
if (i == n || Math.abs(arr[i] - arr[i - 1]) > 2) {
13+
result += getCount(arr, k, last, i - 1);
14+
last = i;
15+
}
16+
}
17+
return result;
18+
}
19+
20+
private int getCount(char[] arr, int k, int start, int end) {
21+
int result = 0;
22+
for (int i = 1; i <= 26 && i * k <= end - start + 1; i++) {
23+
int[] cnt = new int[26];
24+
int good = 0;
25+
for (int j = start; j <= end; j++) {
26+
char cR = arr[j];
27+
cnt[cR - 'a']++;
28+
if (cnt[cR - 'a'] == k) {
29+
good++;
30+
}
31+
if (cnt[cR - 'a'] == k + 1) {
32+
good--;
33+
}
34+
if (j >= start + i * k) {
35+
char cL = arr[j - i * k];
36+
if (cnt[cL - 'a'] == k) {
37+
good--;
38+
}
39+
if (cnt[cL - 'a'] == k + 1) {
40+
good++;
41+
}
42+
cnt[cL - 'a']--;
43+
}
44+
if (good == i) {
45+
result++;
46+
}
47+
}
48+
}
49+
return result;
50+
}
51+
}
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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package g2901_3000.s2954_count_the_number_of_infection_sequences;
2+
3+
// #Hard #Array #Math #Combinatorics #2024_01_15_Time_11_ms_(99.61%)_Space_46.3_MB_(29.46%)
4+
5+
public class Solution {
6+
private static final int M = 100000;
7+
private final long[] fact = new long[M + 1];
8+
private final long[] invFact = new long[M + 1];
9+
private static final int MOD = 1000000007;
10+
private long init = 0;
11+
12+
private int modPow(int x, int y, int mod) {
13+
if (y == 0) {
14+
return 1;
15+
}
16+
long p = modPow(x, y / 2, mod) % mod;
17+
p = (p * p) % mod;
18+
return y % 2 == 1 ? (int) (p * x % mod) : (int) p;
19+
}
20+
21+
private long binomCoeff(int n, int k) {
22+
return Math.max(1L, fact[n] * invFact[k] % MOD * invFact[n - k] % MOD);
23+
}
24+
25+
public int numberOfSequence(int n, int[] sick) {
26+
if (init == 0) {
27+
init = 1;
28+
fact[0] = 1;
29+
for (int i = 1; i <= M; ++i) {
30+
fact[i] = fact[i - 1] * i % MOD;
31+
}
32+
invFact[M] = modPow((int) fact[M], MOD - 2, MOD);
33+
for (int i = M - 1; i > 0; --i) {
34+
invFact[i] = invFact[i + 1] * (i + 1) % MOD;
35+
}
36+
}
37+
long res = 1;
38+
for (int i = 1; i < sick.length; ++i) {
39+
int group = sick[i] - sick[i - 1] - 1;
40+
res = res * modPow(2, Math.max(0, group - 1), MOD) % MOD;
41+
res = res * binomCoeff(sick[i] - i, group) % MOD;
42+
}
43+
return (int) (res * binomCoeff(n - sick.length, n - sick[sick.length - 1] - 1) % MOD);
44+
}
45+
}
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_15_Time_1_ms_(100.00%)_Space_45.6_MB_(11.23%)
4+
5+
public class Solution {
6+
public int[] findIntersectionValues(int[] nums1, int[] nums2) {
7+
int[] freq2 = new int[101];
8+
int[] freq1 = new int[101];
9+
int[] ans = new int[2];
10+
for (int j : nums2) {
11+
freq2[j] = 1;
12+
}
13+
for (int j : nums1) {
14+
freq1[j] = 1;
15+
ans[0] = ans[0] + freq2[j];
16+
}
17+
for (int j : 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)

AltStyle によって変換されたページ (->オリジナル) /