Skip to content

Commit

Permalink
Added tasks 2945-2949
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev authored Dec 26, 2023
1 parent f58bea0 commit 8a1030e
Show file tree
Hide file tree
Showing 15 changed files with 587 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package g2901_3000.s2945_find_maximum_non_decreasing_array_length;

// #Hard #Array #Dynamic_Programming #Binary_Search #Stack #Monotonic_Stack #Queue #Monotonic_Queue
// #2023_12_26_Time_11_ms_(98.10%)_Space_60_MB_(40.30%)

public class Solution {
public int findMaximumLength(int[] nums) {
int n = nums.length;
int[] que = new int[n + 1];
int write = 0;
int read = 0;
long[] prefixSum = new long[n + 1];
long[] sums = new long[n + 1];
int[] count = new int[n + 1];
for (int i = 1; i <= n; i++) {
prefixSum[i] = prefixSum[i - 1] + nums[i - 1];
while (read < write && prefixSum[i] >= sums[read + 1]) {
read++;
}
int j = que[read];
long subarraySum = prefixSum[i] - prefixSum[j];
count[i] = count[j] + 1;
long sum = prefixSum[i] + subarraySum;
while (sum <= sums[write]) {
write--;
}
que[++write] = i;
sums[write] = sum;
}
return count[n];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
2945\. Find Maximum Non-decreasing Array Length

Hard

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

You can perform any number of operations, where each operation involves selecting a **subarray** of the array and replacing it with the **sum** of its elements. For example, if the given array is `[1,3,5,6]` and you select subarray `[3,5]` the array will convert to `[1,8,6]`.

Return _the_ **_maximum_** _length of a_ **_non-decreasing_** _array that can be made after applying operations._

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

**Example 1:**

**Input:** nums = [5,2,2]

**Output:** 1

**Explanation:** This array with length 3 is not non-decreasing.

We have two ways to make the array length two.

First, choosing subarray [2,2] converts the array to [5,4].

Second, choosing subarray [5,2] converts the array to [7,2].

In these two ways the array is not non-decreasing.

And if we choose subarray [5,2,2] and replace it with [9] it becomes non-decreasing.

So the answer is 1.

**Example 2:**

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

**Output:** 4

**Explanation:** The array is non-decreasing. So the answer is 4.

**Example 3:**

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

**Output:** 3

**Explanation:** Replacing [3,2] with [5] converts the given array to [4,5,6] that is non-decreasing.

Because the given array is not non-decreasing, the maximum possible answer is 3.

**Constraints:**

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

// #Easy #Array #Math #Matrix #Simulation #2023_12_26_Time_1_ms_(100.00%)_Space_44.5_MB_(14.88%)

public class Solution {
public boolean areSimilar(int[][] mat, int k) {
int m = mat.length;
int n = mat[0].length;
k %= n;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if ((i & 1) != 0) {
if (mat[i][j] != mat[i][(j - k + n) % n]) {
return false;
}
} else {
if (mat[i][j] != mat[i][(j + k) % n]) {
return false;
}
}
}
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
2946\. Matrix Similarity After Cyclic Shifts

Easy

You are given a **0-indexed** `m x n` integer matrix `mat` and an integer `k`. You have to cyclically **right** shift **odd** indexed rows `k` times and cyclically **left** shift **even** indexed rows `k` times.

Return `true` _if the initial and final matrix are exactly the same and_ `false` _otherwise._

**Example 1:**

**Input:** mat = [[1,2,1,2],[5,5,5,5],[6,3,6,3]], k = 2

**Output:** true

**Explanation:** ![](https://assets.leetcode.com/uploads/2023/10/29/similarmatrix.png) Initially, the matrix looks like the first figure. Second figure represents the state of the matrix after one right and left cyclic shifts to even and odd indexed rows. Third figure is the final state of the matrix after two cyclic shifts which is similar to the initial matrix. Therefore, return true.

**Example 2:**

**Input:** mat = [[2,2],[2,2]], k = 3

**Output:** true

**Explanation:** As all the values are equal in the matrix, even after performing cyclic shifts the matrix will remain the same. Therefeore, we return true.

**Example 3:**

**Input:** mat = [[1,2]], k = 1

**Output:** false

**Explanation:** After one cyclic shift, mat = [[2,1]] which is not equal to the initial matrix. Therefore we return false.

**Constraints:**

* `1 <= mat.length <= 25`
* `1 <= mat[i].length <= 25`
* `1 <= mat[i][j] <= 25`
* `1 <= k <= 50`
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package g2901_3000.s2947_count_beautiful_substrings_i;

// #Medium #String #Prefix_Sum #Enumeration #2023_12_26_Time_2_ms_(100.00%)_Space_42.4_MB_(15.02%)

public class Solution {
public int beautifulSubstrings(String s, int k) {
int[] numVowels = new int[s.length() + 1];
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
numVowels[i + 1] = numVowels[i] + 1;
} else {
numVowels[i + 1] = numVowels[i];
}
}
int step = 1;
while (step < k) {
if ((step * step) % k == 0) {
break;
}
step++;
}
step = step * 2;
int count = 0;
for (int i = 0; i < s.length(); i++) {
for (int j = i + step; j <= s.length(); j += step) {
if ((numVowels[j] - numVowels[i]) * 2 == j - i) {
count++;
}
}
}
return count;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
2947\. Count Beautiful Substrings I

Medium

You are given a string `s` and a positive integer `k`.

Let `vowels` and `consonants` be the number of vowels and consonants in a string.

A string is **beautiful** if:

* `vowels == consonants`.
* `(vowels * consonants) % k == 0`, in other terms the multiplication of `vowels` and `consonants` is divisible by `k`.

Return _the number of **non-empty beautiful substrings** in the given string_ `s`.

A **substring** is a contiguous sequence of characters in a string.

**Vowel letters** in English are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`.

**Consonant letters** in English are every letter except vowels.

**Example 1:**

**Input:** s = "baeyh", k = 2

**Output:** 2

**Explanation:** There are 2 beautiful substrings in the given string.

- Substring "b<ins>aeyh</ins>", vowels = 2 (["a",e"]), consonants = 2 (["y","h"]).

You can see that string "aeyh" is beautiful as vowels == consonants and vowels \* consonants % k == 0.

- Substring "<ins>baey</ins>h", vowels = 2 (["a",e"]), consonants = 2 (["b","y"]).

You can see that string "baey" is beautiful as vowels == consonants and vowels \* consonants % k == 0.

It can be shown that there are only 2 beautiful substrings in the given string.

**Example 2:**

**Input:** s = "abba", k = 1

**Output:** 3

**Explanation:** There are 3 beautiful substrings in the given string.

- Substring "<ins>ab</ins>ba", vowels = 1 (["a"]), consonants = 1 (["b"]).

- Substring "ab<ins>ba</ins>", vowels = 1 (["a"]), consonants = 1 (["b"]).

- Substring "<ins>abba</ins>", vowels = 2 (["a","a"]), consonants = 2 (["b","b"]).

It can be shown that there are only 3 beautiful substrings in the given string.

**Example 3:**

**Input:** s = "bcdf", k = 1

**Output:** 0

**Explanation:** There are no beautiful substrings in the given string.

**Constraints:**

* `1 <= s.length <= 1000`
* `1 <= k <= 1000`
* `s` consists of only English lowercase letters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package g2901_3000.s2948_make_lexicographically_smallest_array_by_swapping_elements;

// #Medium #Array #Sorting #Union_Find #2023_12_26_Time_41_ms_(99.61%)_Space_63.4_MB_(71.68%)

import java.util.Arrays;

public class Solution {
public int[] lexicographicallySmallestArray(int[] nums, int limit) {
int n = nums.length;
Node[] nodes = new Node[n];
for (int i = 0; i < n; i++) {
nodes[i] = new Node(i, nums[i]);
}
Arrays.sort(nodes, (a, b) -> Integer.signum(a.value - b.value));
int group = 1;
nodes[0].group = group;
for (int i = 1; i < n; i++) {
if (Math.abs(nodes[i].value - nodes[i - 1].value) <= limit) {
nodes[i].group = group;
} else {
nodes[i].group = ++group;
}
}
int[] groupBase = new int[group + 1];
for (int i = n - 1; i >= 0; i--) {
groupBase[nodes[i].group] = i;
}
int[] groupIndex = new int[n];
for (Node node : nodes) {
groupIndex[node.id] = node.group;
}
int[] ans = new int[n];
for (int i = 0; i < n; i++) {
int index = groupBase[groupIndex[i]];
ans[i] = nodes[index].value;
groupBase[groupIndex[i]]++;
}
return ans;
}

private static class Node {
int id;
int value;
int group;

Node(int id, int value) {
this.id = id;
this.value = value;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
2948\. Make Lexicographically Smallest Array by Swapping Elements

Medium

You are given a **0-indexed** array of **positive** integers `nums` and a **positive** integer `limit`.

In one operation, you can choose any two indices `i` and `j` and swap `nums[i]` and `nums[j]` **if** `|nums[i] - nums[j]| <= limit`.

Return _the **lexicographically smallest array** that can be obtained by performing the operation any number of times_.

An array `a` is lexicographically smaller than an array `b` if in the first position where `a` and `b` differ, array `a` has an element that is less than the corresponding element in `b`. For example, the array `[2,10,3]` is lexicographically smaller than the array `[10,2,3]` because they differ at index `0` and `2 < 10`.

**Example 1:**

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

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

**Explanation:** Apply the operation 2 times:

- Swap nums[1] with nums[2]. The array becomes [1,3,5,9,8]

- Swap nums[3] with nums[4]. The array becomes [1,3,5,8,9]

We cannot obtain a lexicographically smaller array by applying any more operations.

Note that it may be possible to get the same result by doing different operations.

**Example 2:**

**Input:** nums = [1,7,6,18,2,1], limit = 3

**Output:** [1,6,7,18,1,2]

**Explanation:** Apply the operation 3 times:

- Swap nums[1] with nums[2]. The array becomes [1,6,7,18,2,1]

- Swap nums[0] with nums[4]. The array becomes [2,6,7,18,1,1]

- Swap nums[0] with nums[5]. The array becomes [1,6,7,18,1,2]

We cannot obtain a lexicographically smaller array by applying any more operations.

**Example 3:**

**Input:** nums = [1,7,28,19,10], limit = 3

**Output:** [1,7,28,19,10]

**Explanation:** [1,7,28,19,10] is the lexicographically smallest array we can obtain because we cannot apply the operation on any two indices.

**Constraints:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
* <code>1 <= limit <= 10<sup>9</sup></code>
Loading

0 comments on commit 8a1030e

Please sign in to comment.