Skip to content

Commit afa00d9

Browse files
authored
Added tasks 3190-3197
1 parent 3ea4d2f commit afa00d9

File tree

24 files changed

+890
-0
lines changed

24 files changed

+890
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package g3101_3200.s3190_find_minimum_operations_to_make_all_elements_divisible_by_three
2+
3+
// #Easy #Array #Math #2024_06_29_Time_153_ms_(87.95%)_Space_34.7_MB_(74.70%)
4+
5+
class Solution {
6+
fun minimumOperations(nums: IntArray): Int {
7+
var count = 0
8+
for (i in nums.indices) {
9+
if (nums[i] % 3 != 0) {
10+
count++
11+
}
12+
}
13+
return count
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
3190\. Find Minimum Operations to Make All Elements Divisible by Three
2+
3+
Easy
4+
5+
You are given an integer array `nums`. In one operation, you can add or subtract 1 from **any** element of `nums`.
6+
7+
Return the **minimum** number of operations to make all elements of `nums` divisible by 3.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,2,3,4]
12+
13+
**Output:** 3
14+
15+
**Explanation:**
16+
17+
All array elements can be made divisible by 3 using 3 operations:
18+
19+
* Subtract 1 from 1.
20+
* Add 1 to 2.
21+
* Subtract 1 from 4.
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [3,6,9]
26+
27+
**Output:** 0
28+
29+
**Constraints:**
30+
31+
* `1 <= nums.length <= 50`
32+
* `1 <= nums[i] <= 50`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g3101_3200.s3191_minimum_operations_to_make_binary_array_elements_equal_to_one_i
2+
3+
// #Medium #Array #Bit_Manipulation #Prefix_Sum #Sliding_Window #Queue
4+
// #2024_06_29_Time_653_ms_(57.35%)_Space_73.6_MB_(30.88%)
5+
6+
class Solution {
7+
fun minOperations(nums: IntArray): Int {
8+
var ans = 0
9+
// Iterate through the array up to the third-last element
10+
for (i in 0 until nums.size - 2) {
11+
// If the current element is 0, perform an operation
12+
if (nums[i] == 0) {
13+
ans++
14+
// Flip the current element and the next two elements
15+
nums[i] = 1
16+
nums[i + 1] = if (nums[i + 1] == 0) 1 else 0
17+
nums[i + 2] = if (nums[i + 2] == 0) 1 else 0
18+
}
19+
}
20+
// Check the last two elements if they are 0, return -1 as they cannot be flipped
21+
for (i in nums.size - 2 until nums.size) {
22+
if (nums[i] == 0) {
23+
return -1
24+
}
25+
}
26+
return ans
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
3191\. Minimum Operations to Make Binary Array Elements Equal to One I
2+
3+
Medium
4+
5+
You are given a binary array `nums`.
6+
7+
You can do the following operation on the array **any** number of times (possibly zero):
8+
9+
* Choose **any** 3 **consecutive** elements from the array and **flip** **all** of them.
10+
11+
**Flipping** an element means changing its value from 0 to 1, and from 1 to 0.
12+
13+
Return the **minimum** number of operations required to make all elements in `nums` equal to 1. If it is impossible, return -1.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [0,1,1,1,0,0]
18+
19+
**Output:** 3
20+
21+
**Explanation:**
22+
We can do the following operations:
23+
24+
* 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>.
25+
* 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>.
26+
* 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>.
27+
28+
**Example 2:**
29+
30+
**Input:** nums = [0,1,1,1]
31+
32+
**Output:** \-1
33+
34+
**Explanation:**
35+
It is impossible to make all elements equal to 1.
36+
37+
**Constraints:**
38+
39+
* <code>3 <= nums.length <= 10<sup>5</sup></code>
40+
* `0 <= nums[i] <= 1`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3101_3200.s3192_minimum_operations_to_make_binary_array_elements_equal_to_one_ii
2+
3+
// #Medium #Array #Dynamic_Programming #Greedy
4+
// #2024_06_29_Time_684_ms_(64.29%)_Space_70.9_MB_(62.50%)
5+
6+
class Solution {
7+
fun minOperations(nums: IntArray): Int {
8+
var a = 0
9+
var c = 1
10+
for (x in nums) {
11+
if (x != c) {
12+
a++
13+
c = c xor 1
14+
}
15+
}
16+
return a
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
3192\. Minimum Operations to Make Binary Array Elements Equal to One II
2+
3+
Medium
4+
5+
You are given a binary array `nums`.
6+
7+
You can do the following operation on the array **any** number of times (possibly zero):
8+
9+
* Choose **any** index `i` from the array and **flip** **all** the elements from index `i` to the end of the array.
10+
11+
**Flipping** an element means changing its value from 0 to 1, and from 1 to 0.
12+
13+
Return the **minimum** number of operations required to make all elements in `nums` equal to 1.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [0,1,1,0,1]
18+
19+
**Output:** 4
20+
21+
**Explanation:**
22+
We can do the following operations:
23+
24+
* 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>.
25+
* 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>.
26+
* Choose the index `i = 4`. The resulting array will be <code>nums = [1,1,1,0,<ins>**0**</ins>]</code>.
27+
* Choose the index `i = 3`. The resulting array will be <code>nums = [1,1,1,<ins>**1**</ins>,<ins>**1**</ins>]</code>.
28+
29+
**Example 2:**
30+
31+
**Input:** nums = [1,0,0,0]
32+
33+
**Output:** 1
34+
35+
**Explanation:**
36+
We can do the following operation:
37+
38+
* Choose the index `i = 1`. The resulting array will be <code>nums = [1,<ins>**1**</ins>,<ins>**1**</ins>,<ins>**1**</ins>]</code>.
39+
40+
**Constraints:**
41+
42+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
43+
* `0 <= nums[i] <= 1`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package g3101_3200.s3193_count_the_number_of_inversions
2+
3+
// #Hard #Array #Dynamic_Programming #2024_06_29_Time_243_ms_(94.74%)_Space_45.5_MB_(100.00%)
4+
5+
class Solution {
6+
fun numberOfPermutations(n: Int, r: Array<IntArray>): Int {
7+
r.sortWith { o1: IntArray, o2: IntArray -> o1[0] - o2[0] }
8+
if (r[0][0] == 0 && r[0][1] > 0) {
9+
return 0
10+
}
11+
var ri = if (r[0][0] == 0) 1 else 0
12+
var a: Long = 1
13+
var t: Long
14+
val m = Array(n) { IntArray(401) }
15+
m[0][0] = 1
16+
for (i in 1 until m.size) {
17+
m[i][0] = m[i - 1][0]
18+
for (j in 1..i) {
19+
m[i][j] = (m[i][j] + m[i][j - 1]) % MOD
20+
m[i][j] = (m[i][j] + m[i - 1][j]) % MOD
21+
}
22+
for (j in i + 1..r[ri][1]) {
23+
m[i][j] = (m[i][j] + m[i][j - 1]) % MOD
24+
m[i][j] = (m[i][j] + m[i - 1][j]) % MOD
25+
m[i][j] = (m[i][j] - m[i - 1][j - i - 1])
26+
if (m[i][j] < 0) {
27+
m[i][j] += MOD
28+
}
29+
}
30+
if (r[ri][0] == i) {
31+
t = m[i][r[ri][1]].toLong()
32+
if (t == 0L) {
33+
return 0
34+
}
35+
m[i].fill(0)
36+
m[i][r[ri][1]] = 1
37+
a = (a * t) % MOD
38+
ri++
39+
}
40+
}
41+
return a.toInt()
42+
}
43+
44+
companion object {
45+
private const val MOD = 1000000007
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
3193\. Count the Number of Inversions
2+
3+
Hard
4+
5+
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.
6+
7+
A pair of indices `(i, j)` from an integer array `nums` is called an **inversion** if:
8+
9+
* `i < j` and `nums[i] > nums[j]`
10+
11+
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.
12+
13+
Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
14+
15+
**Example 1:**
16+
17+
**Input:** n = 3, requirements = [[2,2],[0,0]]
18+
19+
**Output:** 2
20+
21+
**Explanation:**
22+
23+
The two permutations are:
24+
25+
* `[2, 0, 1]`
26+
* Prefix `[2, 0, 1]` has inversions `(0, 1)` and `(0, 2)`.
27+
* Prefix `[2]` has 0 inversions.
28+
* `[1, 2, 0]`
29+
* Prefix `[1, 2, 0]` has inversions `(0, 2)` and `(1, 2)`.
30+
* Prefix `[1]` has 0 inversions.
31+
32+
**Example 2:**
33+
34+
**Input:** n = 3, requirements = [[2,2],[1,1],[0,0]]
35+
36+
**Output:** 1
37+
38+
**Explanation:**
39+
40+
The only satisfying permutation is `[2, 0, 1]`:
41+
42+
* Prefix `[2, 0, 1]` has inversions `(0, 1)` and `(0, 2)`.
43+
* Prefix `[2, 0]` has an inversion `(0, 1)`.
44+
* Prefix `[2]` has 0 inversions.
45+
46+
**Example 3:**
47+
48+
**Input:** n = 2, requirements = [[0,0],[1,0]]
49+
50+
**Output:** 1
51+
52+
**Explanation:**
53+
54+
The only satisfying permutation is `[0, 1]`:
55+
56+
* Prefix `[0]` has 0 inversions.
57+
* Prefix `[0, 1]` has an inversion `(0, 1)`.
58+
59+
**Constraints:**
60+
61+
* `2 <= n <= 300`
62+
* `1 <= requirements.length <= n`
63+
* <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code>
64+
* <code>0 <= end<sub>i</sub> <= n - 1</code>
65+
* <code>0 <= cnt<sub>i</sub> <= 400</code>
66+
* The input is generated such that there is at least one `i` such that <code>end<sub>i</sub> == n - 1</code>.
67+
* The input is generated such that all <code>end<sub>i</sub></code> are unique.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g3101_3200.s3194_minimum_average_of_smallest_and_largest_elements
2+
3+
// #Easy #Array #Sorting #Two_Pointers #2024_06_29_Time_192_ms_(94.25%)_Space_41_MB_(49.43%)
4+
5+
import kotlin.math.min
6+
7+
class Solution {
8+
fun minimumAverage(nums: IntArray): Double {
9+
nums.sort()
10+
var m = 102.0
11+
var i = 0
12+
val l = nums.size
13+
while (i < l / 2) {
14+
m = min(m, nums[i] + nums[l - i - 1].toDouble())
15+
i++
16+
}
17+
return m / 2.0
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
3194\. Minimum Average of Smallest and Largest Elements
2+
3+
Easy
4+
5+
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.
6+
7+
You repeat the following procedure `n / 2` times:
8+
9+
* Remove the **smallest** element, `minElement`, and the **largest** element `maxElement`, from `nums`.
10+
* Add `(minElement + maxElement) / 2` to `averages`.
11+
12+
Return the **minimum** element in `averages`.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [7,8,3,4,15,13,4,1]
17+
18+
**Output:** 5.5
19+
20+
**Explanation:**
21+
22+
| Step | nums | averages |
23+
|------|------------------|------------|
24+
| 0 | [7,8,3,4,15,13,4,1] | [] |
25+
| 1 | [7,8,3,4,13,4] | [8] |
26+
| 2 | [7,8,4,4] | [8, 8] |
27+
| 3 | [7,4] | [8, 8, 6] |
28+
| 4 | [] | [8, 8, 6, 5.5] |
29+
30+
The smallest element of averages, 5.5, is returned.
31+
32+
**Example 2:**
33+
34+
**Input:** nums = [1,9,8,3,10,5]
35+
36+
**Output:** 5.5
37+
38+
**Explanation:**
39+
40+
| Step | nums | averages |
41+
|------|----------------|------------|
42+
| 0 | [1,9,8,3,10,5] | [] |
43+
| 1 | [9,8,3,5] | [5.5] |
44+
| 2 | [8,5] | [5.5, 6] |
45+
| 3 | [] | [5.5, 6, 6.5] |
46+
47+
**Example 3:**
48+
49+
**Input:** nums = [1,2,3,7,8,9]
50+
51+
**Output:** 5.0
52+
53+
**Explanation:**
54+
55+
| Step | nums | averages |
56+
|------|----------------|------------|
57+
| 0 | [1,2,3,7,8,9] | [] |
58+
| 1 | [2,3,7,8] | [5] |
59+
| 2 | [3,7] | [5, 5] |
60+
| 3 | [] | [5, 5, 5] |
61+
62+
**Constraints:**
63+
64+
* `2 <= n == nums.length <= 50`
65+
* `n` is even.
66+
* `1 <= nums[i] <= 50`

0 commit comments

Comments
 (0)