Skip to content

Commit

Permalink
Added tasks 3008-3013
Browse files Browse the repository at this point in the history
  • Loading branch information
ThanhNIT authored Feb 27, 2024
1 parent 57dec68 commit 8810188
Show file tree
Hide file tree
Showing 15 changed files with 591 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package g3001_3100.s3008_find_beautiful_indices_in_the_given_array_ii;

// #Hard #String #Binary_Search #Two_Pointers #Hash_Function #String_Matching #Rolling_Hash
// #2024_02_27_Time_36_ms_(99.66%)_Space_67.9_MB_(99.32%)

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

public class Solution {
public List<Integer> beautifulIndices(String s, String a, String b, int k) {
int[] lpsA = getLps(a);
int[] lpsB = getLps(b);
List<Integer> ans = new ArrayList<>();
Deque<Integer> matchesA = new ArrayDeque<>();
int n = s.length();
int aLen = a.length();
int bLen = b.length();
int i = 0;
int j = 0;
while (i < n) {
if (s.charAt(i) == a.charAt(j)) {
i++;
j++;
} else {
if (j == 0) {
i++;
} else {
j = lpsA[j - 1];
}
}
if (j == aLen) {
int aStart = i - aLen;
matchesA.offer(aStart);
j = lpsA[aLen - 1];
}
}
i = j = 0;
while (i < n && !matchesA.isEmpty()) {
if (s.charAt(i) == b.charAt(j)) {
i++;
j++;
} else {
if (j == 0) {
i++;
} else {
j = lpsB[j - 1];
}
}
if (j == bLen) {
int bStart = i - bLen;
j = lpsB[bLen - 1];

while (!matchesA.isEmpty() && bStart - matchesA.peek() > k) {
matchesA.poll();
}
while (!matchesA.isEmpty() && Math.abs(matchesA.peek() - bStart) <= k) {
ans.add(matchesA.poll());
}
}
}
return ans;
}

private int[] getLps(String s) {
int n = s.length();
int[] lps = new int[n];
int i = 1;
int prevLps = 0;
while (i < n) {
if (s.charAt(i) == s.charAt(prevLps)) {
prevLps++;
lps[i++] = prevLps;
} else {
if (prevLps == 0) {
lps[i++] = 0;
} else {
prevLps = lps[prevLps - 1];
}
}
}
return lps;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
3008\. Find Beautiful Indices in the Given Array II

Hard

You are given a **0-indexed** string `s`, a string `a`, a string `b`, and an integer `k`.

An index `i` is **beautiful** if:

* `0 <= i <= s.length - a.length`
* `s[i..(i + a.length - 1)] == a`
* There exists an index `j` such that:
* `0 <= j <= s.length - b.length`
* `s[j..(j + b.length - 1)] == b`
* `|j - i| <= k`

Return _the array that contains beautiful indices in **sorted order from smallest to largest**_.

**Example 1:**

**Input:** s = "isawsquirrelnearmysquirrelhouseohmy", a = "my", b = "squirrel", k = 15

**Output:** [16,33]

**Explanation:** There are 2 beautiful indices: [16,33].
- The index 16 is beautiful as s[16..17] == "my" and there exists an index 4 with s[4..11] == "squirrel" and |16 - 4| <= 15.
- The index 33 is beautiful as s[33..34] == "my" and there exists an index 18 with s[18..25] == "squirrel" and |33 - 18| <= 15. Thus we return [16,33] as the result.

**Example 2:**

**Input:** s = "abcd", a = "a", b = "a", k = 4

**Output:** [0]

**Explanation:** There is 1 beautiful index: [0].
- The index 0 is beautiful as s[0..0] == "a" and there exists an index 0 with s[0..0] == "a" and |0 - 0| <= 4. Thus we return [0] as the result.

**Constraints:**

* <code>1 <= k <= s.length <= 5 * 10<sup>5</sup></code>
* <code>1 <= a.length, b.length <= 5 * 10<sup>5</sup></code>
* `s`, `a`, and `b` contain only lowercase English letters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package g3001_3100.s3010_divide_an_array_into_subarrays_with_minimum_cost_i;

// #Easy #Array #Sorting #Enumeration #2024_02_27_Time_1_ms_(99.09%)_Space_43.6_MB_(96.36%)

public class Solution {
public int minimumCost(int[] nums) {
int first = nums[0];
int min = 51;
int secMin = 52;
for (int i = 1; i < nums.length; i++) {
if (nums[i] < min) {
secMin = min;
min = nums[i];
} else if (nums[i] < secMin) {
secMin = nums[i];
}
}
return first + min + secMin;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
3010\. Divide an Array Into Subarrays With Minimum Cost I

Easy

You are given an array of integers `nums` of length `n`.

The **cost** of an array is the value of its **first** element. For example, the cost of `[1,2,3]` is `1` while the cost of `[3,4,1]` is `3`.

You need to divide `nums` into `3` **disjoint contiguous** subarrays.

Return _the **minimum** possible **sum** of the cost of these subarrays_.

**Example 1:**

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

**Output:** 6

**Explanation:** The best possible way to form 3 subarrays is: [1], [2], and [3,12] at a total cost of 1 + 2 + 3 = 6. The other possible ways to form 3 subarrays are:
- [1], [2,3], and [12] at a total cost of 1 + 2 + 12 = 15.
- [1,2], [3], and [12] at a total cost of 1 + 3 + 12 = 16.

**Example 2:**

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

**Output:** 12

**Explanation:** The best possible way to form 3 subarrays is: [5], [4], and [3] at a total cost of 5 + 4 + 3 = 12.

It can be shown that 12 is the minimum cost achievable.

**Example 3:**

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

**Output:** 12

**Explanation:** The best possible way to form 3 subarrays is: [10,3], [1], and [1] at a total cost of 10 + 1 + 1 = 12.

It can be shown that 12 is the minimum cost achievable.

**Constraints:**

* `3 <= n <= 50`
* `1 <= nums[i] <= 50`
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package g3001_3100.s3011_find_if_array_can_be_sorted;

// #Medium #Array #Sorting #Bit_Manipulation #2024_02_27_Time_1_ms_(100.00%)_Space_44.4_MB_(59.99%)

public class Solution {
public boolean canSortArray(int[] nums) {
int lastGroupMax = Integer.MIN_VALUE;
int max = nums[0];
int lastBit = Integer.bitCount(nums[0]);
for (int i = 1; i < nums.length; i++) {
int bit = Integer.bitCount(nums[i]);
if (bit == lastBit) {
max = Math.max(max, nums[i]);
} else {
lastGroupMax = max;
max = nums[i];
lastBit = bit;
}
if (nums[i] < lastGroupMax) {
return false;
}
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
3011\. Find if Array Can Be Sorted

Medium

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

In one **operation**, you can swap any two **adjacent** elements if they have the **same** number of set bits. You are allowed to do this operation **any** number of times (**including zero**).

Return `true` _if you can sort the array, else return_ `false`.

**Example 1:**

**Input:** nums = [8,4,2,30,15]

**Output:** true

**Explanation:** Let's look at the binary representation of every element. The numbers 2, 4, and 8 have one set bit each with binary representation "10", "100", and "1000" respectively. The numbers 15 and 30 have four set bits each with binary representation "1111" and "11110". We can sort the array using 4 operations:
- Swap nums[0] with nums[1]. This operation is valid because 8 and 4 have one set bit each. The array becomes [4,8,2,30,15].
- Swap nums[1] with nums[2]. This operation is valid because 8 and 2 have one set bit each. The array becomes [4,2,8,30,15].
- Swap nums[0] with nums[1]. This operation is valid because 4 and 2 have one set bit each. The array becomes [2,4,8,30,15].
- Swap nums[3] with nums[4]. This operation is valid because 30 and 15 have four set bits each. The array becomes [2,4,8,15,30].

The array has become sorted, hence we return true. Note that there may be other sequences of operations which also sort the array.

**Example 2:**

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

**Output:** true

**Explanation:** The array is already sorted, hence we return true.

**Example 3:**

**Input:** nums = [3,16,8,4,2]

**Output:** false

**Explanation:** It can be shown that it is not possible to sort the input array using any number of operations.

**Constraints:**

* `1 <= nums.length <= 100`
* <code>1 <= nums[i] <= 2<sup>8</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package g3001_3100.s3012_minimize_length_of_array_using_operations;

// #Medium #Array #Math #Greedy #Number_Theory #2024_02_27_Time_2_ms_(96.82%)_Space_58.5_MB_(74.97%)

public class Solution {
public int minimumArrayLength(int[] nums) {
int min = nums[0];
int max = nums[0];
for (int i : nums) {
if (i < min) {
min = i;
}
if (i > max) {
max = i;
}
}
int n = nums.length;
if (n == 1) {
return 1;
}
if (max % min != 0) {
return 1;
}
int count = 0;
for (int i : nums) {
if (i % min != 0 && i % min < min) {
return 1;
}
if (i == min) {
count++;
}
}
return (count + 1) / 2;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
3012\. Minimize Length of Array Using Operations

Medium

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

Your task is to **minimize** the length of `nums` by performing the following operations **any** number of times (including zero):

* Select **two** **distinct** indices `i` and `j` from `nums`, such that `nums[i] > 0` and `nums[j] > 0`.
* Insert the result of `nums[i] % nums[j]` at the end of `nums`.
* Delete the elements at indices `i` and `j` from `nums`.

Return _an integer denoting the **minimum** **length** of_ `nums` _after performing the operation any number of times._

**Example 1:**

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

**Output:** 1

**Explanation:** One way to minimize the length of the array is as follows:

Operation 1: Select indices 2 and 1, insert nums[2] % nums[1] at the end and it becomes [1,4,3,1,3], then delete elements at indices 2 and 1. nums becomes [1,1,3].

Operation 2: Select indices 1 and 2, insert nums[1] % nums[2] at the end and it becomes [1,1,3,1], then delete elements at indices 1 and 2. nums becomes [1,1].

Operation 3: Select indices 1 and 0, insert nums[1] % nums[0] at the end and it becomes [1,1,0], then delete elements at indices 1 and 0. nums becomes [0].

The length of nums cannot be reduced further. Hence, the answer is 1. It can be shown that 1 is the minimum achievable length.

**Example 2:**

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

**Output:** 2

**Explanation:** One way to minimize the length of the array is as follows:

Operation 1: Select indices 0 and 3, insert nums[0] % nums[3] at the end and it becomes [5,5,5,10,5,5], then delete elements at indices 0 and 3. nums becomes [5,5,5,5].

Operation 2: Select indices 2 and 3, insert nums[2] % nums[3] at the end and it becomes [5,5,5,5,0], then delete elements at indices 2 and 3. nums becomes [5,5,0].

Operation 3: Select indices 0 and 1, insert nums[0] % nums[1] at the end and it becomes [5,5,0,0], then delete elements at indices 0 and 1. nums becomes [0,0].

The length of nums cannot be reduced further. Hence, the answer is 2. It can be shown that 2 is the minimum achievable length.

**Example 3:**

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

**Output:** 1

**Explanation:** One way to minimize the length of the array is as follows:

Operation 1: Select indices 1 and 2, insert nums[1] % nums[2] at the end and it becomes [2,3,4,3], then delete elements at indices 1 and 2. nums becomes [2,3].

Operation 2: Select indices 1 and 0, insert nums[1] % nums[0] at the end and it becomes [2,3,1], then delete elements at indices 1 and 0. nums becomes [1]. The length of nums cannot be reduced further. Hence, the answer is 1. It can be shown that 1 is the minimum achievable length.

**Constraints:**

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

0 comments on commit 8810188

Please sign in to comment.