Skip to content

Added tasks 3190-3197 #1777

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

Merged
merged 4 commits into from
Jun 27, 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,15 @@
package g3101_3200.s3190_find_minimum_operations_to_make_all_elements_divisible_by_three;

// #Easy #Array #Math #2024_06_26_Time_0_ms_(100.00%)_Space_41.6_MB_(78.56%)

public class Solution {
public int minimumOperations(int[] nums) {
int count = 0;
for (int i = 0; i < nums.length; i++) {
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_26_Time_6_ms_(99.99%)_Space_57.2_MB_(62.07%)

public class Solution {
public int minOperations(int[] nums) {
int ans = 0;
// Iterate through the array up to the third-last element
for (int i = 0; i < nums.length - 2; i++) {
// 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] = nums[i + 1] == 0 ? 1 : 0;
nums[i + 2] = nums[i + 2] == 0 ? 1 : 0;
}
}
// Check the last two elements if they are 0, return -1 as they cannot be flipped
for (int i = nums.length - 2; i < nums.length; i++) {
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,17 @@
package g3101_3200.s3192_minimum_operations_to_make_binary_array_elements_equal_to_one_ii;

// #Medium #Array #Dynamic_Programming #Greedy #2024_06_26_Time_6_ms_(99.64%)_Space_62.9_MB_(17.52%)

public class Solution {
public int minOperations(int[] nums) {
int a = 0;
int c = 1;
for (int x : nums) {
if (x != c) {
a++;
c ^= 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_26_Time_11_ms_(96.54%)_Space_45.5_MB_(37.54%)

import java.util.Arrays;

public class Solution {
private static final int MOD = 1_000_000_007;

public int numberOfPermutations(int n, int[][] r) {
Arrays.sort(r, (o1, o2) -> o1[0] - o2[0]);
if (r[0][0] == 0 && r[0][1] > 0) {
return 0;
}
int ri = r[0][0] == 0 ? 1 : 0;
long a = 1;
long t;
int[][] m = new int[n][401];
m[0][0] = 1;
for (int i = 1; i < m.length; i++) {
m[i][0] = m[i - 1][0];
for (int j = 1; j <= i; j++) {
m[i][j] = (m[i][j] + m[i][j - 1]) % MOD;
m[i][j] = (m[i][j] + m[i - 1][j]) % MOD;
}
for (int j = i + 1; j <= r[ri][1]; j++) {
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]];
if (t == 0) {
return 0;
}
Arrays.fill(m[i], 0);
m[i][r[ri][1]] = 1;
a = (a * t) % MOD;
ri++;
}
}
return (int) a;
}
}
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,16 @@
package g3101_3200.s3194_minimum_average_of_smallest_and_largest_elements;

// #Easy #Array #Sorting #Two_Pointers #2024_06_26_Time_2_ms_(98.88%)_Space_43.5_MB_(88.12%)

import java.util.Arrays;

public class Solution {
public double minimumAverage(int[] nums) {
Arrays.sort(nums);
double m = 102.0;
for (int i = 0, l = nums.length; i < l / 2; i++) {
m = Math.min(m, nums[i] + (double) nums[l - i - 1]);
}
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
Loading