Skip to content

Commit 53c1181

Browse files
authored
Added tasks 2962-3000
1 parent 1ec2a29 commit 53c1181

File tree

69 files changed

+2626
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+2626
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g2901_3000.s2962_count_subarrays_where_max_element_appears_at_least_k_times
2+
3+
// #Medium #Array #Sliding_Window #2024_01_19_Time_587_ms_(88.37%)_Space_57_MB_(93.02%)
4+
5+
class Solution {
6+
fun countSubarrays(nums: IntArray, k: Int): Long {
7+
val st = IntArray(nums.size + 1)
8+
var si = 0
9+
var m = 0
10+
for (i in nums.indices) {
11+
if (m < nums[i]) {
12+
m = nums[i]
13+
si = 0
14+
}
15+
if (m == nums[i]) {
16+
st[si++] = i
17+
}
18+
}
19+
if (si < k) {
20+
return 0
21+
}
22+
var r: Long = 0
23+
st[si] = nums.size
24+
for (i in k..si) {
25+
r += (st[i - k] + 1).toLong() * (st[i] - st[i - 1])
26+
}
27+
return r
28+
}
29+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2962\. Count Subarrays Where Max Element Appears at Least K Times
2+
3+
Medium
4+
5+
You are given an integer array `nums` and a **positive** integer `k`.
6+
7+
Return _the number of subarrays where the **maximum** element of_ `nums` _appears **at least**_ `k` _times in that subarray._
8+
9+
A **subarray** is a contiguous sequence of elements within an array.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [1,3,2,3,3], k = 2
14+
15+
**Output:** 6
16+
17+
**Explanation:** The subarrays that contain the element 3 at least 2 times are: [1,3,2,3], [1,3,2,3,3], [3,2,3], [3,2,3,3], [2,3,3] and [3,3].
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [1,4,2,1], k = 3
22+
23+
**Output:** 0
24+
25+
**Explanation:** No subarray contains the element 4 at least 3 times.
26+
27+
**Constraints:**
28+
29+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
30+
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
31+
* <code>1 <= k <= 10<sup>5</sup></code>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g2901_3000.s2963_count_the_number_of_good_partitions
2+
3+
// #Hard #Array #Hash_Table #Math #Combinatorics
4+
// #2024_01_19_Time_600_ms_(100.00%)_Space_58.4_MB_(95.24%)
5+
6+
import kotlin.math.max
7+
8+
class Solution {
9+
fun numberOfGoodPartitions(nums: IntArray): Int {
10+
val mp: MutableMap<Int, Int> = HashMap()
11+
val n = nums.size
12+
for (i in 0 until n) {
13+
mp[nums[i]] = i
14+
}
15+
var i = 0
16+
var j = 0
17+
var cnt = 0
18+
while (i < n) {
19+
j = max(j, mp[nums[i]]!!)
20+
if (i == j) {
21+
cnt++
22+
}
23+
i++
24+
}
25+
var res = 1
26+
for (k in 1 until cnt) {
27+
res *= 2
28+
val mod = 1000000007
29+
res %= mod
30+
}
31+
return res
32+
}
33+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2963\. Count the Number of Good Partitions
2+
3+
Hard
4+
5+
You are given a **0-indexed** array `nums` consisting of **positive** integers.
6+
7+
A partition of an array into one or more **contiguous** subarrays is called **good** if no two subarrays contain the same number.
8+
9+
Return _the **total number** of good partitions of_ `nums`.
10+
11+
Since the answer may be large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1,2,3,4]
16+
17+
**Output:** 8
18+
19+
**Explanation:** The 8 possible good partitions are: ([1], [2], [3], [4]), ([1], [2], [3,4]), ([1], [2,3], [4]), ([1], [2,3,4]), ([1,2], [3], [4]), ([1,2], [3,4]), ([1,2,3], [4]), and ([1,2,3,4]).
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [1,1,1,1]
24+
25+
**Output:** 1
26+
27+
**Explanation:** The only possible good partition is: ([1,1,1,1]).
28+
29+
**Example 3:**
30+
31+
**Input:** nums = [1,2,1,3]
32+
33+
**Output:** 2
34+
35+
**Explanation:** The 2 possible good partitions are: ([1,2,1], [3]) and ([1,2,1,3]).
36+
37+
**Constraints:**
38+
39+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
40+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g2901_3000.s2965_find_missing_and_repeated_values
2+
3+
// #Easy #Array #Hash_Table #Math #Matrix #2024_01_19_Time_235_ms_(91.67%)_Space_40.3_MB_(80.00%)
4+
5+
class Solution {
6+
fun findMissingAndRepeatedValues(grid: Array<IntArray>): IntArray {
7+
val nSquare = grid.size * grid.size
8+
var sum = nSquare * (nSquare + 1) / 2
9+
val found = BooleanArray(nSquare + 1)
10+
var repeated = 1
11+
for (row in grid) {
12+
for (n in row) {
13+
sum -= n
14+
if (found[n]) {
15+
repeated = n
16+
}
17+
found[n] = true
18+
}
19+
}
20+
return intArrayOf(repeated, sum + repeated)
21+
}
22+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2965\. Find Missing and Repeated Values
2+
3+
Easy
4+
5+
You are given a **0-indexed** 2D integer matrix `grid` of size `n * n` with values in the range <code>[1, n<sup>2</sup>]</code>. Each integer appears **exactly once** except `a` which appears **twice** and `b` which is **missing**. The task is to find the repeating and missing numbers `a` and `b`.
6+
7+
Return _a **0-indexed** integer array_ `ans` _of size_ `2` _where_ `ans[0]` _equals to_ `a` _and_ `ans[1]` _equals to_ `b`_._
8+
9+
**Example 1:**
10+
11+
**Input:** grid = [[1,3],[2,2]]
12+
13+
**Output:** [2,4]
14+
15+
**Explanation:** Number 2 is repeated and number 4 is missing so the answer is [2,4].
16+
17+
**Example 2:**
18+
19+
**Input:** grid = [[9,1,7],[8,9,2],[3,4,6]]
20+
21+
**Output:** [9,5]
22+
23+
**Explanation:** Number 9 is repeated and number 5 is missing so the answer is [9,5].
24+
25+
**Constraints:**
26+
27+
* `2 <= n == grid.length == grid[i].length <= 50`
28+
* `1 <= grid[i][j] <= n * n`
29+
* For all `x` that `1 <= x <= n * n` there is exactly one `x` that is not equal to any of the grid members.
30+
* For all `x` that `1 <= x <= n * n` there is exactly one `x` that is equal to exactly two of the grid members.
31+
* For all `x` that `1 <= x <= n * n` except two of them there is exatly one pair of `i, j` that `0 <= i, j <= n - 1` and `grid[i][j] == x`.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g2901_3000.s2966_divide_array_into_arrays_with_max_difference
2+
3+
// #Medium #Array #Sorting #Greedy #2024_01_19_Time_977_ms_(60.00%)_Space_76.7_MB_(24.00%)
4+
5+
class Solution {
6+
fun divideArray(nums: IntArray, k: Int): Array<IntArray> {
7+
nums.sort()
8+
val n = nums.size
9+
val triplets = n / 3
10+
val result = Array(triplets) { intArrayOf() }
11+
var i = 0
12+
var j = 0
13+
while (i < n) {
14+
val first = nums[i]
15+
val third = nums[i + 2]
16+
if (third - first > k) {
17+
return Array(0) { intArrayOf() }
18+
}
19+
result[j] = intArrayOf(first, nums[i + 1], third)
20+
i += 3
21+
j++
22+
}
23+
return result
24+
}
25+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2966\. Divide Array Into Arrays With Max Difference
2+
3+
Medium
4+
5+
You are given an integer array `nums` of size `n` and a positive integer `k`.
6+
7+
Divide the array into one or more arrays of size `3` satisfying the following conditions:
8+
9+
* **Each** element of `nums` should be in **exactly** one array.
10+
* The difference between **any** two elements in one array is less than or equal to `k`.
11+
12+
Return _a_ **2D** _array containing all the arrays. If it is impossible to satisfy the conditions, return an empty array. And if there are multiple answers, return **any** of them._
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [1,3,4,8,7,9,3,5,1], k = 2
17+
18+
**Output:** [[1,1,3],[3,4,5],[7,8,9]]
19+
20+
**Explanation:** We can divide the array into the following arrays: [1,1,3], [3,4,5] and [7,8,9]. The difference between any two elements in each array is less than or equal to 2. Note that the order of elements is not important.
21+
22+
**Example 2:**
23+
24+
**Input:** nums = [1,3,3,2,7,3], k = 3
25+
26+
**Output:** []
27+
28+
**Explanation:** It is not possible to divide the array satisfying all the conditions.
29+
30+
**Constraints:**
31+
32+
* `n == nums.length`
33+
* <code>1 <= n <= 10<sup>5</sup></code>
34+
* `n` is a multiple of `3`.
35+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
36+
* <code>1 <= k <= 10<sup>5</sup></code>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package g2901_3000.s2967_minimum_cost_to_make_array_equalindromic
2+
3+
// #Medium #Array #Math #Sorting #Greedy #2024_01_19_Time_363_ms_(100.00%)_Space_56_MB_(86.49%)
4+
5+
import kotlin.math.abs
6+
import kotlin.math.min
7+
8+
@Suppress("NAME_SHADOWING")
9+
class Solution {
10+
fun minimumCost(nums: IntArray): Long {
11+
nums.sort()
12+
val len = nums.size
13+
val m = if (len % 2 != 0) len / 2 else len / 2 - 1
14+
val previousPalindrome = getPreviousPalindrome(nums[m])
15+
val nextPalindrome = getNextPalindrome(nums[m])
16+
var ans1: Long = 0
17+
var ans2: Long = 0
18+
for (num in nums) {
19+
ans1 += abs((previousPalindrome - num))
20+
ans2 += abs((nextPalindrome - num))
21+
}
22+
return min(ans1, ans2)
23+
}
24+
25+
private fun getPreviousPalindrome(num: Int): Int {
26+
var previousPalindrome = num
27+
while (!isPalindrome(previousPalindrome)) {
28+
previousPalindrome--
29+
}
30+
return previousPalindrome
31+
}
32+
33+
private fun getNextPalindrome(num: Int): Int {
34+
var nextPalindrome = num
35+
while (!isPalindrome(nextPalindrome)) {
36+
nextPalindrome++
37+
}
38+
return nextPalindrome
39+
}
40+
41+
private fun isPalindrome(num: Int): Boolean {
42+
var num = num
43+
val copyNum = num
44+
var reverseNum = 0
45+
while (num > 0) {
46+
reverseNum = reverseNum * 10 + num % 10
47+
num /= 10
48+
}
49+
return copyNum == reverseNum
50+
}
51+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2967\. Minimum Cost to Make Array Equalindromic
2+
3+
Medium
4+
5+
You are given a **0-indexed** integer array `nums` having length `n`.
6+
7+
You are allowed to perform a special move **any** number of times (**including zero**) on `nums`. In one **special** **move** you perform the following steps **in order**:
8+
9+
* Choose an index `i` in the range `[0, n - 1]`, and a **positive** integer `x`.
10+
* Add `|nums[i] - x|` to the total cost.
11+
* Change the value of `nums[i]` to `x`.
12+
13+
A **palindromic number** is a positive integer that remains the same when its digits are reversed. For example, `121`, `2552` and `65756` are palindromic numbers whereas `24`, `46`, `235` are not palindromic numbers.
14+
15+
An array is considered **equalindromic** if all the elements in the array are equal to an integer `y`, where `y` is a **palindromic number** less than <code>10<sup>9</sup></code>.
16+
17+
Return _an integer denoting the **minimum** possible total cost to make_ `nums` _**equalindromic** by performing any number of special moves._
18+
19+
**Example 1:**
20+
21+
**Input:** nums = [1,2,3,4,5]
22+
23+
**Output:** 6
24+
25+
**Explanation:** We can make the array equalindromic by changing all elements to 3 which is a palindromic number. The cost of changing the array to [3,3,3,3,3] using 4 special moves is given by |1 - 3| + |2 - 3| + |4 - 3| + |5 - 3| = 6. It can be shown that changing all elements to any palindromic number other than 3 cannot be achieved at a lower cost.
26+
27+
**Example 2:**
28+
29+
**Input:** nums = [10,12,13,14,15]
30+
31+
**Output:** 11
32+
33+
**Explanation:** We can make the array equalindromic by changing all elements to 11 which is a palindromic number. The cost of changing the array to [11,11,11,11,11] using 5 special moves is given by |10 - 11| + |12 - 11| + |13 - 11| + |14 - 11| + |15 - 11| = 11. It can be shown that changing all elements to any palindromic number other than 11 cannot be achieved at a lower cost.
34+
35+
**Example 3:**
36+
37+
**Input:** nums = [22,33,22,33,22]
38+
39+
**Output:** 22
40+
41+
**Explanation:** We can make the array equalindromic by changing all elements to 22 which is a palindromic number. The cost of changing the array to [22,22,22,22,22] using 2 special moves is given by |33 - 22| + |33 - 22| = 22. It can be shown that changing all elements to any palindromic number other than 22 cannot be achieved at a lower cost.
42+
43+
**Constraints:**
44+
45+
* <code>1 <= n <= 10<sup>5</sup></code>
46+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g2901_3000.s2968_apply_operations_to_maximize_frequency_score
2+
3+
// #Hard #Array #Sorting #Binary_Search #Prefix_Sum #Sliding_Window
4+
// #2024_01_19_Time_566_ms_(90.00%)_Space_64.8_MB_(85.00%)
5+
6+
import kotlin.math.abs
7+
import kotlin.math.max
8+
9+
class Solution {
10+
fun maxFrequencyScore(nums: IntArray, k: Long): Int {
11+
nums.sort()
12+
var left = 0
13+
var cost = 0L
14+
var median = nums[0]
15+
var maxLen = 1
16+
for (right in 1 until nums.size) {
17+
cost += abs(median - nums[right])
18+
median = nums[(right + left + 1) / 2]
19+
while (cost> k) {
20+
cost -= abs(median - nums[left])
21+
left++
22+
median = nums[(right + left + 1) / 2]
23+
}
24+
maxLen = max(maxLen, right - left + 1)
25+
}
26+
return maxLen
27+
}
28+
}

0 commit comments

Comments
 (0)