Skip to content

Commit 2725ffc

Browse files
authored
Added tasks 2778-2784
1 parent 50c4b42 commit 2725ffc

File tree

15 files changed

+477
-0
lines changed

15 files changed

+477
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package g2701_2800.s2778_sum_of_squares_of_special_elements
2+
3+
// #Easy #Array #Simulation #2023_08_09_Time_183_ms_(86.44%)_Space_36.9_MB_(84.75%)
4+
5+
class Solution {
6+
fun sumOfSquares(nums: IntArray): Int {
7+
var sum = 0
8+
for (i in nums.indices) {
9+
if (nums.size % (i + 1) == 0) sum += nums[i] * nums[i]
10+
}
11+
return sum
12+
}
13+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2778\. Sum of Squares of Special Elements
2+
3+
Easy
4+
5+
You are given a **1-indexed** integer array `nums` of length `n`.
6+
7+
An element `nums[i]` of `nums` is called **special** if `i` divides `n`, i.e. `n % i == 0`.
8+
9+
Return _the **sum of the squares** of all **special** elements of_ `nums`.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [1,2,3,4]
14+
15+
**Output:** 21
16+
17+
**Explanation:**
18+
19+
There are exactly 3 special elements in nums: nums[1] since 1 divides 4, nums[2] since 2 divides 4, and nums[4] since 4 divides 4.
20+
21+
Hence, the sum of the squares of all special elements of nums is nums[1] \* nums[1] + nums[2] \* nums[2] + nums[4] \* nums[4] = 1 \* 1 + 2 \* 2 + 4 \* 4 = 21.
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [2,7,1,19,18,3]
26+
27+
**Output:** 63
28+
29+
**Explanation:**
30+
31+
There are exactly 4 special elements in nums: nums[1] since 1 divides 6, nums[2] since 2 divides 6, nums[3] since 3 divides 6, and nums[6] since 6 divides 6.
32+
33+
Hence, the sum of the squares of all special elements of nums is nums[1] \* nums[1] + nums[2] \* nums[2] + nums[3] \* nums[3] + nums[6] \* nums[6] = 2 \* 2 + 7 \* 7 + 1 \* 1 + 3 \* 3 = 63.
34+
35+
**Constraints:**
36+
37+
* `1 <= nums.length == n <= 50`
38+
* `1 <= nums[i] <= 50`
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g2701_2800.s2779_maximum_beauty_of_an_array_after_applying_operation
2+
3+
// #Medium #Array #Sorting #Binary_Search #Sliding_Window
4+
// #2023_08_09_Time_649_ms_(96.97%)_Space_58.8_MB_(81.82%)
5+
6+
class Solution {
7+
fun maximumBeauty(nums: IntArray, k: Int): Int {
8+
nums.sort()
9+
var i = 0
10+
val n = nums.size
11+
var j = 0
12+
while (j < n) {
13+
if (nums[j] - nums[i] > k * 2) {
14+
i++
15+
}
16+
++j
17+
}
18+
return j - i
19+
}
20+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2779\. Maximum Beauty of an Array After Applying Operation
2+
3+
Medium
4+
5+
You are given a **0-indexed** array `nums` and a **non-negative** integer `k`.
6+
7+
In one operation, you can do the following:
8+
9+
* Choose an index `i` that **hasn't been chosen before** from the range `[0, nums.length - 1]`.
10+
* Replace `nums[i]` with any integer from the range `[nums[i] - k, nums[i] + k]`.
11+
12+
The **beauty** of the array is the length of the longest subsequence consisting of equal elements.
13+
14+
Return _the **maximum** possible beauty of the array_ `nums` _after applying the operation any number of times._
15+
16+
**Note** that you can apply the operation to each index **only once**.
17+
18+
A **subsequence** of an array is a new array generated from the original array by deleting some elements (possibly none) without changing the order of the remaining elements.
19+
20+
**Example 1:**
21+
22+
**Input:** nums = [4,6,1,2], k = 2
23+
24+
**Output:** 3
25+
26+
**Explanation:**
27+
28+
In this example, we apply the following operations:
29+
30+
- Choose index 1, replace it with 4 (from range [4,8]), nums = [4,4,1,2].
31+
32+
- Choose index 3, replace it with 4 (from range [0,4]), nums = [4,4,1,4].
33+
34+
After the applied operations, the beauty of the array nums is 3 (subsequence consisting of indices 0, 1, and 3).
35+
36+
It can be proven that 3 is the maximum possible length we can achieve.
37+
38+
**Example 2:**
39+
40+
**Input:** nums = [1,1,1,1], k = 10
41+
42+
**Output:** 4
43+
44+
**Explanation:**
45+
46+
In this example we don't have to apply any operations.
47+
48+
The beauty of the array nums is 4 (whole array).
49+
50+
**Constraints:**
51+
52+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
53+
* <code>0 <= nums[i], k <= 10<sup>5</sup></code>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g2701_2800.s2780_minimum_index_of_a_valid_split
2+
3+
// #Medium #Array #Hash_Table #Sorting #2023_08_09_Time_640_ms_(91.67%)_Space_61.6_MB_(95.83%)
4+
5+
class Solution {
6+
fun minimumIndex(nums: List<Int>): Int {
7+
val map = HashMap<Int, Int>()
8+
map[0] = 0
9+
var max = 0
10+
val m = nums.size
11+
for (n in nums) {
12+
map[n] = map.getOrDefault(n, 0) + 1
13+
if (map[n]!! > map[max]!!) {
14+
max = n
15+
}
16+
}
17+
var freq = 0
18+
for (i in 0 until m) {
19+
if (nums[i] == max) {
20+
freq++
21+
}
22+
if (freq * 2 > i + 1 && (map[max]!! - freq) * 2 > m - i - 1) {
23+
return i
24+
}
25+
}
26+
return -1
27+
}
28+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2780\. Minimum Index of a Valid Split
2+
3+
Medium
4+
5+
An element `x` of an integer array `arr` of length `m` is **dominant** if `freq(x) * 2 > m`, where `freq(x)` is the number of occurrences of `x` in `arr`. Note that this definition implies that `arr` can have **at most one** dominant element.
6+
7+
You are given a **0-indexed** integer array `nums` of length `n` with one dominant element.
8+
9+
You can split `nums` at an index `i` into two arrays `nums[0, ..., i]` and `nums[i + 1, ..., n - 1]`, but the split is only **valid** if:
10+
11+
* `0 <= i < n - 1`
12+
* `nums[0, ..., i]`, and `nums[i + 1, ..., n - 1]` have the same dominant element.
13+
14+
Here, `nums[i, ..., j]` denotes the subarray of `nums` starting at index `i` and ending at index `j`, both ends being inclusive. Particularly, if `j < i` then `nums[i, ..., j]` denotes an empty subarray.
15+
16+
Return _the **minimum** index of a **valid split**_. If no valid split exists, return `-1`.
17+
18+
**Example 1:**
19+
20+
**Input:** nums = [1,2,2,2]
21+
22+
**Output:** 2
23+
24+
**Explanation:**
25+
26+
We can split the array at index 2 to obtain arrays [1,2,2] and [2].
27+
28+
In array [1,2,2], element 2 is dominant since it occurs twice in the array and 2 \* 2 > 3.
29+
30+
In array [2], element 2 is dominant since it occurs once in the array and 1 \* 2 > 1.
31+
32+
Both [1,2,2] and [2] have the same dominant element as nums, so this is a valid split.
33+
34+
It can be shown that index 2 is the minimum index of a valid split.
35+
36+
**Example 2:**
37+
38+
**Input:** nums = [2,1,3,1,1,1,7,1,2,1]
39+
40+
**Output:** 4
41+
42+
**Explanation:**
43+
44+
We can split the array at index 4 to obtain arrays [2,1,3,1,1] and [1,7,1,2,1].
45+
46+
In array [2,1,3,1,1], element 1 is dominant since it occurs thrice in the array and 3 \* 2 > 5.
47+
48+
In array [1,7,1,2,1], element 1 is dominant since it occurs thrice in the array and 3 \* 2 > 5.
49+
50+
Both [2,1,3,1,1] and [1,7,1,2,1] have the same dominant element as nums, so this is a valid split.
51+
52+
It can be shown that index 4 is the minimum index of a valid split.
53+
54+
**Example 3:**
55+
56+
**Input:** nums = [3,3,3,3,7,2,2]
57+
58+
**Output:** -1
59+
60+
**Explanation:**
61+
62+
It can be shown that there is no valid split.
63+
64+
**Constraints:**
65+
66+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
67+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
68+
* `nums` has exactly one dominant element.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g2701_2800.s2781_length_of_the_longest_valid_substring
2+
3+
// #Hard #Array #String #Hash_Table #Sliding_Window
4+
// #2023_08_09_Time_647_ms_(100.00%)_Space_60.3_MB_(100.00%)
5+
6+
class Solution {
7+
fun longestValidSubstring(word: String, forbidden: List<String>): Int {
8+
val set = HashSet<String>()
9+
for (s in forbidden) {
10+
set.add(s)
11+
}
12+
val n = word.length
13+
var ans = 0
14+
var i = 0
15+
var j = 0
16+
while (j < n) {
17+
var k = j
18+
while (k > j - 10 && k >= i) {
19+
if (set.contains(word.substring(k, j + 1))) {
20+
i = k + 1
21+
break
22+
}
23+
k--
24+
}
25+
ans = Math.max(j - i + 1, ans)
26+
j++
27+
}
28+
return ans
29+
}
30+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2781\. Length of the Longest Valid Substring
2+
3+
Hard
4+
5+
You are given a string `word` and an array of strings `forbidden`.
6+
7+
A string is called **valid** if none of its substrings are present in `forbidden`.
8+
9+
Return _the length of the **longest valid substring** of the string_ `word`.
10+
11+
A **substring** is a contiguous sequence of characters in a string, possibly empty.
12+
13+
**Example 1:**
14+
15+
**Input:** word = "cbaaaabc", forbidden = ["aaa","cb"]
16+
17+
**Output:** 4
18+
19+
**Explanation:**
20+
21+
There are 9 valid substrings in word: "c", "b", "a", "ba", "aa", "bc", "baa", "aab", "ab", "abc"and "aabc". The length of the longest valid substring is 4.
22+
23+
It can be shown that all other substrings contain either "aaa" or "cb" as a substring.
24+
25+
**Example 2:**
26+
27+
**Input:** word = "leetcode", forbidden = ["de","le","e"]
28+
29+
**Output:** 4
30+
31+
**Explanation:**
32+
33+
There are 11 valid substrings in word: "l", "t", "c", "o", "d", "tc", "co", "od", "tco", "cod", and "tcod". The length of the longest valid substring is 4.
34+
35+
It can be shown that all other substrings contain either "de", "le", or "e" as a substring.
36+
37+
**Constraints:**
38+
39+
* <code>1 <= word.length <= 10<sup>5</sup></code>
40+
* `word` consists only of lowercase English letters.
41+
* <code>1 <= forbidden.length <= 10<sup>5</sup></code>
42+
* `1 <= forbidden[i].length <= 10`
43+
* `forbidden[i]` consists only of lowercase English letters.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g2701_2800.s2784_check_if_array_is_good
2+
3+
// #Easy #Array #Hash_Table #Sorting #2023_08_09_Time_177_ms_(88.89%)_Space_36.3_MB_(93.33%)
4+
5+
class Solution {
6+
fun isGood(nums: IntArray): Boolean {
7+
var max = Int.MIN_VALUE
8+
var sum = 0
9+
for (i in nums) {
10+
if (i > max) {
11+
max = i
12+
}
13+
sum += i
14+
}
15+
if (max != nums.size - 1) {
16+
return false
17+
}
18+
val newSum = max * (max + 1) / 2 + max
19+
if (sum != newSum) {
20+
return false
21+
}
22+
var count = 0
23+
for (i in nums) {
24+
if (i == max) {
25+
count++
26+
}
27+
}
28+
return count == 2
29+
}
30+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2784\. Check if Array is Good
2+
3+
Easy
4+
5+
You are given an integer array `nums`. We consider an array **good** if it is a permutation of an array `base[n]`.
6+
7+
`base[n] = [1, 2, ..., n - 1, n, n]` (in other words, it is an array of length `n + 1` which contains `1` to `n - 1` exactly once, plus two occurrences of `n`). For example, `base[1] = [1, 1]` and `base[3] = [1, 2, 3, 3]`.
8+
9+
Return `true` _if the given array is good, otherwise return_ `false`.
10+
11+
**Note:** A permutation of integers represents an arrangement of these numbers.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [2, 1, 3]
16+
17+
**Output:** false
18+
19+
**Explanation:** Since the maximum element of the array is 3, the only candidate n for which this array could be a permutation of base[n], is n = 3. However, base[3] has four elements but array nums has three. Therefore, it can not be a permutation of base[3] = [1, 2, 3, 3]. So the answer is false.
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [1, 3, 3, 2]
24+
25+
**Output:** true
26+
27+
**Explanation:** Since the maximum element of the array is 3, the only candidate n for which this array could be a permutation of base[n], is n = 3. It can be seen that nums is a permutation of base[3] = [1, 2, 3, 3] (by swapping the second and fourth elements in nums, we reach base[3]). Therefore, the answer is true.
28+
29+
**Example 3:**
30+
31+
**Input:** nums = [1, 1]
32+
33+
**Output:** true
34+
35+
**Explanation:** Since the maximum element of the array is 1, the only candidate n for which this array could be a permutation of base[n], is n = 1. It can be seen that nums is a permutation of base[1] = [1, 1]. Therefore, the answer is true.
36+
37+
**Example 4:**
38+
39+
**Input:** nums = [3, 4, 4, 1, 2, 1]
40+
41+
**Output:** false
42+
43+
**Explanation:** Since the maximum element of the array is 4, the only candidate n for which this array could be a permutation of base[n], is n = 4. However, base[4] has five elements but array nums has six. Therefore, it can not be a permutation of base[4] = [1, 2, 3, 4, 4]. So the answer is false.
44+
45+
**Constraints:**
46+
47+
* `1 <= nums.length <= 100`
48+
* `1 <= num[i] <= 200`

0 commit comments

Comments
 (0)