Skip to content

Commit

Permalink
Added tasks 3190-3197
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev authored Jun 29, 2024
1 parent 3ea4d2f commit afa00d9
Show file tree
Hide file tree
Showing 24 changed files with 890 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package g3101_3200.s3190_find_minimum_operations_to_make_all_elements_divisible_by_three

// #Easy #Array #Math #2024_06_29_Time_153_ms_(87.95%)_Space_34.7_MB_(74.70%)

class Solution {
fun minimumOperations(nums: IntArray): Int {
var count = 0
for (i in nums.indices) {
if (nums[i] % 3 != 0) {
count++
}
}
return count
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
3190\. Find Minimum Operations to Make All Elements Divisible by Three

Easy

You are given an integer array `nums`. In one operation, you can add or subtract 1 from **any** element of `nums`.

Return the **minimum** number of operations to make all elements of `nums` divisible by 3.

**Example 1:**

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

**Output:** 3

**Explanation:**

All array elements can be made divisible by 3 using 3 operations:

* Subtract 1 from 1.
* Add 1 to 2.
* Subtract 1 from 4.

**Example 2:**

**Input:** nums = [3,6,9]

**Output:** 0

**Constraints:**

* `1 <= nums.length <= 50`
* `1 <= nums[i] <= 50`
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package g3101_3200.s3191_minimum_operations_to_make_binary_array_elements_equal_to_one_i

// #Medium #Array #Bit_Manipulation #Prefix_Sum #Sliding_Window #Queue
// #2024_06_29_Time_653_ms_(57.35%)_Space_73.6_MB_(30.88%)

class Solution {
fun minOperations(nums: IntArray): Int {
var ans = 0
// Iterate through the array up to the third-last element
for (i in 0 until nums.size - 2) {
// If the current element is 0, perform an operation
if (nums[i] == 0) {
ans++
// Flip the current element and the next two elements
nums[i] = 1
nums[i + 1] = if (nums[i + 1] == 0) 1 else 0
nums[i + 2] = if (nums[i + 2] == 0) 1 else 0
}
}
// Check the last two elements if they are 0, return -1 as they cannot be flipped
for (i in nums.size - 2 until nums.size) {
if (nums[i] == 0) {
return -1
}
}
return ans
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
3191\. Minimum Operations to Make Binary Array Elements Equal to One I

Medium

You are given a binary array `nums`.

You can do the following operation on the array **any** number of times (possibly zero):

* Choose **any** 3 **consecutive** elements from the array and **flip** **all** of them.

**Flipping** an element means changing its value from 0 to 1, and from 1 to 0.

Return the **minimum** number of operations required to make all elements in `nums` equal to 1. If it is impossible, return -1.

**Example 1:**

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

**Output:** 3

**Explanation:**
We can do the following operations:

* Choose the elements at indices 0, 1 and 2. The resulting array is <code>nums = [<ins>**1**</ins>,<ins>**0**</ins>,<ins>**0**</ins>,1,0,0]</code>.
* Choose the elements at indices 1, 2 and 3. The resulting array is <code>nums = [1,<ins>**1**</ins>,<ins>**1**</ins>,**<ins>0</ins>**,0,0]</code>.
* Choose the elements at indices 3, 4 and 5. The resulting array is <code>nums = [1,1,1,**<ins>1</ins>**,<ins>**1**</ins>,<ins>**1**</ins>]</code>.

**Example 2:**

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

**Output:** \-1

**Explanation:**
It is impossible to make all elements equal to 1.

**Constraints:**

* <code>3 <= nums.length <= 10<sup>5</sup></code>
* `0 <= nums[i] <= 1`
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g3101_3200.s3192_minimum_operations_to_make_binary_array_elements_equal_to_one_ii

// #Medium #Array #Dynamic_Programming #Greedy
// #2024_06_29_Time_684_ms_(64.29%)_Space_70.9_MB_(62.50%)

class Solution {
fun minOperations(nums: IntArray): Int {
var a = 0
var c = 1
for (x in nums) {
if (x != c) {
a++
c = c xor 1
}
}
return a
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
3192\. Minimum Operations to Make Binary Array Elements Equal to One II

Medium

You are given a binary array `nums`.

You can do the following operation on the array **any** number of times (possibly zero):

* Choose **any** index `i` from the array and **flip** **all** the elements from index `i` to the end of the array.

**Flipping** an element means changing its value from 0 to 1, and from 1 to 0.

Return the **minimum** number of operations required to make all elements in `nums` equal to 1.

**Example 1:**

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

**Output:** 4

**Explanation:**
We can do the following operations:

* Choose the index `i = 1`. The resulting array will be <code>nums = [0,<ins>**0**</ins>,<ins>**0**</ins>,<ins>**1**</ins>,<ins>**0**</ins>]</code>.
* Choose the index `i = 0`. The resulting array will be <code>nums = [<ins>**1**</ins>,<ins>**1**</ins>,<ins>**1**</ins>,<ins>**0**</ins>,<ins>**1**</ins>]</code>.
* Choose the index `i = 4`. The resulting array will be <code>nums = [1,1,1,0,<ins>**0**</ins>]</code>.
* Choose the index `i = 3`. The resulting array will be <code>nums = [1,1,1,<ins>**1**</ins>,<ins>**1**</ins>]</code>.

**Example 2:**

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

**Output:** 1

**Explanation:**
We can do the following operation:

* Choose the index `i = 1`. The resulting array will be <code>nums = [1,<ins>**1**</ins>,<ins>**1**</ins>,<ins>**1**</ins>]</code>.

**Constraints:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* `0 <= nums[i] <= 1`
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package g3101_3200.s3193_count_the_number_of_inversions

// #Hard #Array #Dynamic_Programming #2024_06_29_Time_243_ms_(94.74%)_Space_45.5_MB_(100.00%)

class Solution {
fun numberOfPermutations(n: Int, r: Array<IntArray>): Int {
r.sortWith { o1: IntArray, o2: IntArray -> o1[0] - o2[0] }
if (r[0][0] == 0 && r[0][1] > 0) {
return 0
}
var ri = if (r[0][0] == 0) 1 else 0
var a: Long = 1
var t: Long
val m = Array(n) { IntArray(401) }
m[0][0] = 1
for (i in 1 until m.size) {
m[i][0] = m[i - 1][0]
for (j in 1..i) {
m[i][j] = (m[i][j] + m[i][j - 1]) % MOD
m[i][j] = (m[i][j] + m[i - 1][j]) % MOD
}
for (j in i + 1..r[ri][1]) {
m[i][j] = (m[i][j] + m[i][j - 1]) % MOD
m[i][j] = (m[i][j] + m[i - 1][j]) % MOD
m[i][j] = (m[i][j] - m[i - 1][j - i - 1])
if (m[i][j] < 0) {
m[i][j] += MOD
}
}
if (r[ri][0] == i) {
t = m[i][r[ri][1]].toLong()
if (t == 0L) {
return 0
}
m[i].fill(0)
m[i][r[ri][1]] = 1
a = (a * t) % MOD
ri++
}
}
return a.toInt()
}

companion object {
private const val MOD = 1000000007
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
3193\. Count the Number of Inversions

Hard

You are given an integer `n` and a 2D array `requirements`, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the **inversion** count of each requirement.

A pair of indices `(i, j)` from an integer array `nums` is called an **inversion** if:

* `i < j` and `nums[i] > nums[j]`

Return the number of permutations `perm` of `[0, 1, 2, ..., n - 1]` such that for **all** `requirements[i]`, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.

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

**Example 1:**

**Input:** n = 3, requirements = [[2,2],[0,0]]

**Output:** 2

**Explanation:**

The two permutations are:

* `[2, 0, 1]`
* Prefix `[2, 0, 1]` has inversions `(0, 1)` and `(0, 2)`.
* Prefix `[2]` has 0 inversions.
* `[1, 2, 0]`
* Prefix `[1, 2, 0]` has inversions `(0, 2)` and `(1, 2)`.
* Prefix `[1]` has 0 inversions.

**Example 2:**

**Input:** n = 3, requirements = [[2,2],[1,1],[0,0]]

**Output:** 1

**Explanation:**

The only satisfying permutation is `[2, 0, 1]`:

* Prefix `[2, 0, 1]` has inversions `(0, 1)` and `(0, 2)`.
* Prefix `[2, 0]` has an inversion `(0, 1)`.
* Prefix `[2]` has 0 inversions.

**Example 3:**

**Input:** n = 2, requirements = [[0,0],[1,0]]

**Output:** 1

**Explanation:**

The only satisfying permutation is `[0, 1]`:

* Prefix `[0]` has 0 inversions.
* Prefix `[0, 1]` has an inversion `(0, 1)`.

**Constraints:**

* `2 <= n <= 300`
* `1 <= requirements.length <= n`
* <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code>
* <code>0 <= end<sub>i</sub> <= n - 1</code>
* <code>0 <= cnt<sub>i</sub> <= 400</code>
* The input is generated such that there is at least one `i` such that <code>end<sub>i</sub> == n - 1</code>.
* The input is generated such that all <code>end<sub>i</sub></code> are unique.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package g3101_3200.s3194_minimum_average_of_smallest_and_largest_elements

// #Easy #Array #Sorting #Two_Pointers #2024_06_29_Time_192_ms_(94.25%)_Space_41_MB_(49.43%)

import kotlin.math.min

class Solution {
fun minimumAverage(nums: IntArray): Double {
nums.sort()
var m = 102.0
var i = 0
val l = nums.size
while (i < l / 2) {
m = min(m, nums[i] + nums[l - i - 1].toDouble())
i++
}
return m / 2.0
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
3194\. Minimum Average of Smallest and Largest Elements

Easy

You have an array of floating point numbers `averages` which is initially empty. You are given an array `nums` of `n` integers where `n` is even.

You repeat the following procedure `n / 2` times:

* Remove the **smallest** element, `minElement`, and the **largest** element `maxElement`, from `nums`.
* Add `(minElement + maxElement) / 2` to `averages`.

Return the **minimum** element in `averages`.

**Example 1:**

**Input:** nums = [7,8,3,4,15,13,4,1]

**Output:** 5.5

**Explanation:**

| Step | nums | averages |
|------|------------------|------------|
| 0 | [7,8,3,4,15,13,4,1] | [] |
| 1 | [7,8,3,4,13,4] | [8] |
| 2 | [7,8,4,4] | [8, 8] |
| 3 | [7,4] | [8, 8, 6] |
| 4 | [] | [8, 8, 6, 5.5] |

The smallest element of averages, 5.5, is returned.

**Example 2:**

**Input:** nums = [1,9,8,3,10,5]

**Output:** 5.5

**Explanation:**

| Step | nums | averages |
|------|----------------|------------|
| 0 | [1,9,8,3,10,5] | [] |
| 1 | [9,8,3,5] | [5.5] |
| 2 | [8,5] | [5.5, 6] |
| 3 | [] | [5.5, 6, 6.5] |

**Example 3:**

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

**Output:** 5.0

**Explanation:**

| Step | nums | averages |
|------|----------------|------------|
| 0 | [1,2,3,7,8,9] | [] |
| 1 | [2,3,7,8] | [5] |
| 2 | [3,7] | [5, 5] |
| 3 | [] | [5, 5, 5] |

**Constraints:**

* `2 <= n == nums.length <= 50`
* `n` is even.
* `1 <= nums[i] <= 50`
Loading

0 comments on commit afa00d9

Please sign in to comment.