Skip to content

Added tasks 3002-3007 #1692

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 9 commits into from
Feb 26, 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,36 @@
package g3001_3100.s3002_maximum_size_of_a_set_after_removals;

// #Medium #Array #Hash_Table #Greedy #2024_02_26_Time_26_ms_(94.01%)_Space_53.3_MB_(80.90%)

import java.util.HashSet;

public class Solution {
public int maximumSetSize(int[] nums1, int[] nums2) {
HashSet<Integer> uniq1 = new HashSet<>();
HashSet<Integer> uniq2 = new HashSet<>();
for (int i = 0; i < nums1.length; i++) {
uniq1.add(nums1[i]);
uniq2.add(nums2[i]);
}
int common = 0;
if (uniq1.size() <= uniq2.size()) {
for (int u : uniq1) {
if (uniq2.contains(u)) {
common++;
}
}
} else {
for (int u : uniq2) {
if (uniq1.contains(u)) {
common++;
}
}
}
int half = nums1.length / 2;
int from1 = Math.min(uniq1.size() - common, half);
int from2 = Math.min(uniq2.size() - common, half);
int takeFromCommon1 = half - from1;
int takeFromCommon2 = half - from2;
return from1 + from2 + Math.min(takeFromCommon1 + takeFromCommon2, common);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
3002\. Maximum Size of a Set After Removals

Medium

You are given two **0-indexed** integer arrays `nums1` and `nums2` of even length `n`.

You must remove `n / 2` elements from `nums1` and `n / 2` elements from `nums2`. After the removals, you insert the remaining elements of `nums1` and `nums2` into a set `s`.

Return _the **maximum** possible size of the set_ `s`.

**Example 1:**

**Input:** nums1 = [1,2,1,2], nums2 = [1,1,1,1]

**Output:** 2

**Explanation:** We remove two occurences of 1 from nums1 and nums2. After the removals, the arrays become equal to nums1 = [2,2] and nums2 = [1,1]. Therefore, s = {1,2}.

It can be shown that 2 is the maximum possible size of the set s after the removals.

**Example 2:**

**Input:** nums1 = [1,2,3,4,5,6], nums2 = [2,3,2,3,2,3]

**Output:** 5

**Explanation:** We remove 2, 3, and 6 from nums1, as well as 2 and two occurrences of 3 from nums2. After the removals, the arrays become equal to nums1 = [1,4,5] and nums2 = [2,3,2]. Therefore, s = {1,2,3,4,5}.

It can be shown that 5 is the maximum possible size of the set s after the removals.

**Example 3:**

**Input:** nums1 = [1,1,2,2,3,3], nums2 = [4,4,5,5,6,6]

**Output:** 6

**Explanation:** We remove 1, 2, and 3 from nums1, as well as 4, 5, and 6 from nums2. After the removals, the arrays become equal to nums1 = [1,2,3] and nums2 = [4,5,6]. Therefore, s = {1,2,3,4,5,6}.

It can be shown that 6 is the maximum possible size of the set s after the removals.

**Constraints:**

* `n == nums1.length == nums2.length`
* <code>1 <= n <= 2 * 10<sup>4</sup></code>
* `n` is even.
* <code>1 <= nums1[i], nums2[i] <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package g3001_3100.s3003_maximize_the_number_of_partitions_after_operations;

// #Hard #String #Dynamic_Programming #Bit_Manipulation #Bitmask
// #2024_02_26_Time_1_ms_(100.00%)_Space_42.1_MB_(99.44%)

@SuppressWarnings("java:S6541")
public class Solution {
private static final int ALPHABET_SIZE = 'z' - 'a' + 1;

public int maxPartitionsAfterOperations(String s, int k) {
if (k == ALPHABET_SIZE) {
return 1;
}
int n = s.length();
int[] ansr = new int[n];
int[] usedr = new int[n];
int used = 0;
int cntUsed = 0;
int ans = 1;
for (int i = n - 1; i >= 0; --i) {
int ch = s.charAt(i) - 'a';
if ((used & (1 << ch)) == 0) {
if (cntUsed == k) {
cntUsed = 0;
used = 0;
ans++;
}
used |= (1 << ch);
cntUsed++;
}
ansr[i] = ans;
usedr[i] = used;
}
int ansl = 0;
ans = ansr[0];
int l = 0;
while (l < n) {
used = 0;
cntUsed = 0;
int usedBeforeLast = 0;
int usedTwiceBeforeLast = 0;
int last = -1;
int r = l;
while (r < n) {
int ch = s.charAt(r) - 'a';
if ((used & (1 << ch)) == 0) {
if (cntUsed == k) {
break;
}
usedBeforeLast = used;
last = r;
used |= (1 << ch);
cntUsed++;
} else if (cntUsed < k) {
usedTwiceBeforeLast |= (1 << ch);
}
r++;
}
if (cntUsed == k) {
if (last - l > Integer.bitCount(usedBeforeLast)) {
ans = Math.max(ans, ansl + 1 + ansr[last]);
}
if (last + 1 < r) {
if (last + 2 >= n) {
ans = Math.max(ans, ansl + 1 + 1);
} else {
if (Integer.bitCount(usedr[last + 2]) == k) {
int canUse = ((1 << ALPHABET_SIZE) - 1) & ~used & ~usedr[last + 2];
if (canUse > 0) {
ans = Math.max(ans, ansl + 1 + 1 + ansr[last + 2]);
} else {
ans = Math.max(ans, ansl + 1 + ansr[last + 2]);
}
int l1 = s.charAt(last + 1) - 'a';
if ((usedTwiceBeforeLast & (1 << l1)) == 0) {
ans = Math.max(ans, ansl + 1 + ansr[last + 1]);
}
} else {
ans = Math.max(ans, ansl + 1 + ansr[last + 2]);
}
}
}
}
l = r;
ansl++;
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
3003\. Maximize the Number of Partitions After Operations

Hard

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

You are to perform the following partitioning operations until `s` is **empty**:

* Choose the **longest** **prefix** of `s` containing at most `k` **distinct** characters.
* **Delete** the prefix from `s` and increase the number of partitions by one. The remaining characters (if any) in `s` maintain their initial order.

**Before** the operations, you are allowed to change **at most** **one** index in `s` to another lowercase English letter.

Return _an integer denoting the **maximum** number of resulting partitions after the operations by optimally choosing at most one index to change._

**Example 1:**

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

**Output:** 3

**Explanation:** In this example, to maximize the number of resulting partitions, s[2] can be changed to 'b'. s becomes "acbca". The operations can now be performed as follows until s becomes empty:
- Choose the longest prefix containing at most 2 distinct characters, "<ins>ac</ins>bca".
- Delete the prefix, and s becomes "bca". The number of partitions is now 1.
- Choose the longest prefix containing at most 2 distinct characters, "<ins>bc</ins>a".
- Delete the prefix, and s becomes "a". The number of partitions is now 2.
- Choose the longest prefix containing at most 2 distinct characters, "<ins>a</ins>".
- Delete the prefix, and s becomes empty. The number of partitions is now 3.

Hence, the answer is 3. It can be shown that it is not possible to obtain more than 3 partitions.

**Example 2:**

**Input:** s = "aabaab", k = 3

**Output:** 1

**Explanation:** In this example, to maximize the number of resulting partitions we can leave s as it is. The operations can now be performed as follows until s becomes empty:
- Choose the longest prefix containing at most 3 distinct characters, "<ins>aabaab</ins>".
- Delete the prefix, and s becomes empty. The number of partitions becomes 1.

Hence, the answer is 1. It can be shown that it is not possible to obtain more than 1 partition.

**Example 3:**

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

**Output:** 4

**Explanation:** In this example, to maximize the number of resulting partitions, s[1] can be changed to 'a'. s becomes "xayz". The operations can now be performed as follows until s becomes empty:
- Choose the longest prefix containing at most 1 distinct character, "<ins>x</ins>ayz".
- Delete the prefix, and s becomes "ayz". The number of partitions is now 1.
- Choose the longest prefix containing at most 1 distinct character, "<ins>a</ins>yz".
- Delete the prefix, and s becomes "yz". The number of partitions is now 2.
- Choose the longest prefix containing at most 1 distinct character, "<ins>y</ins>z".
- Delete the prefix, and s becomes "z". The number of partitions is now 3.
- Choose the longest prefix containing at most 1 distinct character, "<ins>z</ins>".
- Delete the prefix, and s becomes empty. The number of partitions is now 4.

Hence, the answer is 4. It can be shown that it is not possible to obtain more than 4 partitions.

**Constraints:**

* <code>1 <= s.length <= 10<sup>4</sup></code>
* `s` consists only of lowercase English letters.
* `1 <= k <= 26`
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package g3001_3100.s3005_count_elements_with_maximum_frequency;

// #Easy #Array #Hash_Table #Counting #2024_02_26_Time_1_ms_(99.76%)_Space_41.6_MB_(98.97%)

import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("java:S135")
public class Solution {
public int maxFrequencyElements(int[] nums) {
if (nums.length == 1) {
return 1;
}
List<Integer> list = new ArrayList<>();
int co = 0;
int prev = 0;
for (int num : nums) {
if (list.contains(num)) {
continue;
}
list.add(num);
if (list.size() == nums.length) {
break;
}
int c = 0;
for (int i : nums) {
if (num == i) {
c++;
}
}
if (c > 1) {
if (c > prev) {
co = c;
prev = c;
} else if (c == prev) {
co = c + co;
}
}
}
if (co == 0) {
return nums.length;
}
return co;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
3005\. Count Elements With Maximum Frequency

Easy

You are given an array `nums` consisting of **positive** integers.

Return _the **total frequencies** of elements in_ `nums` _such that those elements all have the **maximum** frequency_.

The **frequency** of an element is the number of occurrences of that element in the array.

**Example 1:**

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

**Output:** 4

**Explanation:** The elements 1 and 2 have a frequency of 2 which is the maximum frequency in the array. So the number of elements in the array with maximum frequency is 4.

**Example 2:**

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

**Output:** 5

**Explanation:** All elements of the array have a frequency of 1 which is the maximum. So the number of elements in the array with maximum frequency is 5.

**Constraints:**

* `1 <= nums.length <= 100`
* `1 <= nums[i] <= 100`
Loading