Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tasks 2962-3000 #594

Merged
merged 2 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package g2901_3000.s2962_count_subarrays_where_max_element_appears_at_least_k_times

// #Medium #Array #Sliding_Window #2024_01_19_Time_587_ms_(88.37%)_Space_57_MB_(93.02%)

class Solution {
fun countSubarrays(nums: IntArray, k: Int): Long {
val st = IntArray(nums.size + 1)
var si = 0
var m = 0
for (i in nums.indices) {
if (m < nums[i]) {
m = nums[i]
si = 0
}
if (m == nums[i]) {
st[si++] = i
}
}
if (si < k) {
return 0
}
var r: Long = 0
st[si] = nums.size
for (i in k..si) {
r += (st[i - k] + 1).toLong() * (st[i] - st[i - 1])
}
return r
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
2962\. Count Subarrays Where Max Element Appears at Least K Times

Medium

You are given an integer array `nums` and a **positive** integer `k`.

Return _the number of subarrays where the **maximum** element of_ `nums` _appears **at least**_ `k` _times in that subarray._

A **subarray** is a contiguous sequence of elements within an array.

**Example 1:**

**Input:** nums = [1,3,2,3,3], k = 2

**Output:** 6

**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].

**Example 2:**

**Input:** nums = [1,4,2,1], k = 3

**Output:** 0

**Explanation:** No subarray contains the element 4 at least 3 times.

**Constraints:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
* <code>1 <= k <= 10<sup>5</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package g2901_3000.s2963_count_the_number_of_good_partitions

// #Hard #Array #Hash_Table #Math #Combinatorics
// #2024_01_19_Time_600_ms_(100.00%)_Space_58.4_MB_(95.24%)

import kotlin.math.max

class Solution {
fun numberOfGoodPartitions(nums: IntArray): Int {
val mp: MutableMap<Int, Int> = HashMap()
val n = nums.size
for (i in 0 until n) {
mp[nums[i]] = i
}
var i = 0
var j = 0
var cnt = 0
while (i < n) {
j = max(j, mp[nums[i]]!!)
if (i == j) {
cnt++
}
i++
}
var res = 1
for (k in 1 until cnt) {
res *= 2
val mod = 1000000007
res %= mod
}
return res
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
2963\. Count the Number of Good Partitions

Hard

You are given a **0-indexed** array `nums` consisting of **positive** integers.

A partition of an array into one or more **contiguous** subarrays is called **good** if no two subarrays contain the same number.

Return _the **total number** of good partitions of_ `nums`.

Since the answer may be large, return it **modulo** <code>10<sup>9</sup> + 7</code>.

**Example 1:**

**Input:** nums = [1,2,3,4]

**Output:** 8

**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]).

**Example 2:**

**Input:** nums = [1,1,1,1]

**Output:** 1

**Explanation:** The only possible good partition is: ([1,1,1,1]).

**Example 3:**

**Input:** nums = [1,2,1,3]

**Output:** 2

**Explanation:** The 2 possible good partitions are: ([1,2,1], [3]) and ([1,2,1,3]).

**Constraints:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package g2901_3000.s2965_find_missing_and_repeated_values

// #Easy #Array #Hash_Table #Math #Matrix #2024_01_19_Time_235_ms_(91.67%)_Space_40.3_MB_(80.00%)

class Solution {
fun findMissingAndRepeatedValues(grid: Array<IntArray>): IntArray {
val nSquare = grid.size * grid.size
var sum = nSquare * (nSquare + 1) / 2
val found = BooleanArray(nSquare + 1)
var repeated = 1
for (row in grid) {
for (n in row) {
sum -= n
if (found[n]) {
repeated = n
}
found[n] = true
}
}
return intArrayOf(repeated, sum + repeated)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
2965\. Find Missing and Repeated Values

Easy

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`.

Return _a **0-indexed** integer array_ `ans` _of size_ `2` _where_ `ans[0]` _equals to_ `a` _and_ `ans[1]` _equals to_ `b`_._

**Example 1:**

**Input:** grid = [[1,3],[2,2]]

**Output:** [2,4]

**Explanation:** Number 2 is repeated and number 4 is missing so the answer is [2,4].

**Example 2:**

**Input:** grid = [[9,1,7],[8,9,2],[3,4,6]]

**Output:** [9,5]

**Explanation:** Number 9 is repeated and number 5 is missing so the answer is [9,5].

**Constraints:**

* `2 <= n == grid.length == grid[i].length <= 50`
* `1 <= grid[i][j] <= n * n`
* For all `x` that `1 <= x <= n * n` there is exactly one `x` that is not equal to any of the grid members.
* For all `x` that `1 <= x <= n * n` there is exactly one `x` that is equal to exactly two of the grid members.
* 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`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package g2901_3000.s2966_divide_array_into_arrays_with_max_difference

// #Medium #Array #Sorting #Greedy #2024_01_19_Time_977_ms_(60.00%)_Space_76.7_MB_(24.00%)

class Solution {
fun divideArray(nums: IntArray, k: Int): Array<IntArray> {
nums.sort()
val n = nums.size
val triplets = n / 3
val result = Array(triplets) { intArrayOf() }
var i = 0
var j = 0
while (i < n) {
val first = nums[i]
val third = nums[i + 2]
if (third - first > k) {
return Array(0) { intArrayOf() }
}
result[j] = intArrayOf(first, nums[i + 1], third)
i += 3
j++
}
return result
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
2966\. Divide Array Into Arrays With Max Difference

Medium

You are given an integer array `nums` of size `n` and a positive integer `k`.

Divide the array into one or more arrays of size `3` satisfying the following conditions:

* **Each** element of `nums` should be in **exactly** one array.
* The difference between **any** two elements in one array is less than or equal to `k`.

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._

**Example 1:**

**Input:** nums = [1,3,4,8,7,9,3,5,1], k = 2

**Output:** [[1,1,3],[3,4,5],[7,8,9]]

**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.

**Example 2:**

**Input:** nums = [1,3,3,2,7,3], k = 3

**Output:** []

**Explanation:** It is not possible to divide the array satisfying all the conditions.

**Constraints:**

* `n == nums.length`
* <code>1 <= n <= 10<sup>5</sup></code>
* `n` is a multiple of `3`.
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
* <code>1 <= k <= 10<sup>5</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package g2901_3000.s2967_minimum_cost_to_make_array_equalindromic

// #Medium #Array #Math #Sorting #Greedy #2024_01_19_Time_363_ms_(100.00%)_Space_56_MB_(86.49%)

import kotlin.math.abs
import kotlin.math.min

@Suppress("NAME_SHADOWING")
class Solution {
fun minimumCost(nums: IntArray): Long {
nums.sort()
val len = nums.size
val m = if (len % 2 != 0) len / 2 else len / 2 - 1
val previousPalindrome = getPreviousPalindrome(nums[m])
val nextPalindrome = getNextPalindrome(nums[m])
var ans1: Long = 0
var ans2: Long = 0
for (num in nums) {
ans1 += abs((previousPalindrome - num))
ans2 += abs((nextPalindrome - num))
}
return min(ans1, ans2)
}

private fun getPreviousPalindrome(num: Int): Int {
var previousPalindrome = num
while (!isPalindrome(previousPalindrome)) {
previousPalindrome--
}
return previousPalindrome
}

private fun getNextPalindrome(num: Int): Int {
var nextPalindrome = num
while (!isPalindrome(nextPalindrome)) {
nextPalindrome++
}
return nextPalindrome
}

private fun isPalindrome(num: Int): Boolean {
var num = num
val copyNum = num
var reverseNum = 0
while (num > 0) {
reverseNum = reverseNum * 10 + num % 10
num /= 10
}
return copyNum == reverseNum
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
2967\. Minimum Cost to Make Array Equalindromic

Medium

You are given a **0-indexed** integer array `nums` having length `n`.

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**:

* Choose an index `i` in the range `[0, n - 1]`, and a **positive** integer `x`.
* Add `|nums[i] - x|` to the total cost.
* Change the value of `nums[i]` to `x`.

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.

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>.

Return _an integer denoting the **minimum** possible total cost to make_ `nums` _**equalindromic** by performing any number of special moves._

**Example 1:**

**Input:** nums = [1,2,3,4,5]

**Output:** 6

**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.

**Example 2:**

**Input:** nums = [10,12,13,14,15]

**Output:** 11

**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.

**Example 3:**

**Input:** nums = [22,33,22,33,22]

**Output:** 22

**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.

**Constraints:**

* <code>1 <= n <= 10<sup>5</sup></code>
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package g2901_3000.s2968_apply_operations_to_maximize_frequency_score

// #Hard #Array #Sorting #Binary_Search #Prefix_Sum #Sliding_Window
// #2024_01_19_Time_566_ms_(90.00%)_Space_64.8_MB_(85.00%)

import kotlin.math.abs
import kotlin.math.max

class Solution {
fun maxFrequencyScore(nums: IntArray, k: Long): Int {
nums.sort()
var left = 0
var cost = 0L
var median = nums[0]
var maxLen = 1
for (right in 1 until nums.size) {
cost += abs(median - nums[right])
median = nums[(right + left + 1) / 2]
while (cost> k) {
cost -= abs(median - nums[left])
left++
median = nums[(right + left + 1) / 2]
}
maxLen = max(maxLen, right - left + 1)
}
return maxLen
}
}
Loading