Skip to content

Commit df459b9

Browse files
committed
Added tasks 3002-3007
1 parent c3d337d commit df459b9

File tree

15 files changed

+656
-0
lines changed

15 files changed

+656
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g3001_3100.s3002_maximum_size_of_a_set_after_removals;
2+
3+
// #Medium #Array #Hash_Table #Greedy #2024_02_26_Time_26_ms_(94.01%)_Space_53.3_MB_(80.90%)
4+
5+
import java.util.HashSet;
6+
7+
public class Solution {
8+
public int maximumSetSize(int[] nums1, int[] nums2) {
9+
HashSet<Integer> uniq1 = new HashSet<>();
10+
HashSet<Integer> uniq2 = new HashSet<>();
11+
for (int i = 0; i < nums1.length; i++) {
12+
uniq1.add(nums1[i]);
13+
uniq2.add(nums2[i]);
14+
}
15+
int common = 0;
16+
if (uniq1.size() <= uniq2.size()) {
17+
for (int u : uniq1) {
18+
if (uniq2.contains(u)) {
19+
common++;
20+
}
21+
}
22+
} else {
23+
for (int u : uniq2) {
24+
if (uniq1.contains(u)) {
25+
common++;
26+
}
27+
}
28+
}
29+
int half = nums1.length / 2;
30+
int from1 = Math.min(uniq1.size() - common, half);
31+
int from2 = Math.min(uniq2.size() - common, half);
32+
int takeFromCommon1 = half - from1;
33+
int takeFromCommon2 = half - from2;
34+
return from1 + from2 + Math.min(takeFromCommon1 + takeFromCommon2, common);
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
3002\. Maximum Size of a Set After Removals
2+
3+
Medium
4+
5+
You are given two **0-indexed** integer arrays `nums1` and `nums2` of even length `n`.
6+
7+
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`.
8+
9+
Return _the **maximum** possible size of the set_ `s`.
10+
11+
**Example 1:**
12+
13+
**Input:** nums1 = [1,2,1,2], nums2 = [1,1,1,1]
14+
15+
**Output:** 2
16+
17+
**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.
18+
19+
**Example 2:**
20+
21+
**Input:** nums1 = [1,2,3,4,5,6], nums2 = [2,3,2,3,2,3]
22+
23+
**Output:** 5
24+
25+
**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.
26+
27+
**Example 3:**
28+
29+
**Input:** nums1 = [1,1,2,2,3,3], nums2 = [4,4,5,5,6,6]
30+
31+
**Output:** 6
32+
33+
**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.
34+
35+
**Constraints:**
36+
37+
* `n == nums1.length == nums2.length`
38+
* <code>1 <= n <= 2 * 10<sup>4</sup></code>
39+
* `n` is even.
40+
* <code>1 <= nums1[i], nums2[i] <= 10<sup>9</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package g3001_3100.s3003_maximize_the_number_of_partitions_after_operations;
2+
3+
// #Hard #String #Dynamic_Programming #Bit_Manipulation #Bitmask
4+
// #2024_02_26_Time_1_ms_(100.00%)_Space_42.1_MB_(99.44%)
5+
6+
public class Solution {
7+
private static final int ALPHABET_SIZE = 'z' - 'a' + 1;
8+
9+
public int maxPartitionsAfterOperations(String s, int k) {
10+
if (k == ALPHABET_SIZE) {
11+
return 1;
12+
}
13+
int n = s.length();
14+
int[] ansr = new int[n];
15+
int[] usedr = new int[n];
16+
int used = 0;
17+
int cntUsed = 0;
18+
int ans = 1;
19+
for (int i = n - 1; i >= 0; --i) {
20+
int ch = s.charAt(i) - 'a';
21+
if ((used & (1 << ch)) == 0) {
22+
if (cntUsed == k) {
23+
cntUsed = 0;
24+
used = 0;
25+
ans++;
26+
}
27+
used |= (1 << ch);
28+
cntUsed++;
29+
}
30+
ansr[i] = ans;
31+
usedr[i] = used;
32+
}
33+
34+
int ansl = 0;
35+
ans = ansr[0];
36+
for (int l = 0; l < n; ) {
37+
used = 0;
38+
cntUsed = 0;
39+
int usedBeforeLast = 0;
40+
int usedTwiceBeforeLast = 0;
41+
int last = -1;
42+
int r = l;
43+
while (r < n) {
44+
int ch = s.charAt(r) - 'a';
45+
if ((used & (1 << ch)) == 0) {
46+
if (cntUsed == k) {
47+
break;
48+
}
49+
usedBeforeLast = used;
50+
last = r;
51+
used |= (1 << ch);
52+
cntUsed++;
53+
} else if (cntUsed < k) {
54+
usedTwiceBeforeLast |= (1 << ch);
55+
}
56+
r++;
57+
}
58+
59+
if (cntUsed == k) {
60+
if (last - l > Integer.bitCount(usedBeforeLast)) {
61+
ans = Math.max(ans, ansl + 1 + ansr[last]);
62+
}
63+
if (last + 1 < r) {
64+
if (last + 2 >= n) {
65+
ans = Math.max(ans, ansl + 1 + 1);
66+
} else {
67+
if (Integer.bitCount(usedr[last + 2]) == k) {
68+
int canUse = ((1 << ALPHABET_SIZE) - 1) & ~used & ~usedr[last + 2];
69+
if (canUse > 0) {
70+
ans = Math.max(ans, ansl + 1 + 1 + ansr[last + 2]);
71+
} else {
72+
ans = Math.max(ans, ansl + 1 + ansr[last + 2]);
73+
}
74+
int l1 = s.charAt(last + 1) - 'a';
75+
if ((usedTwiceBeforeLast & (1 << l1)) == 0) {
76+
ans = Math.max(ans, ansl + 1 + ansr[last + 1]);
77+
}
78+
} else {
79+
ans = Math.max(ans, ansl + 1 + ansr[last + 2]);
80+
}
81+
}
82+
}
83+
}
84+
l = r;
85+
ansl++;
86+
}
87+
88+
return ans;
89+
}
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
3003\. Maximize the Number of Partitions After Operations
2+
3+
Hard
4+
5+
You are given a **0-indexed** string `s` and an integer `k`.
6+
7+
You are to perform the following partitioning operations until `s` is **empty**:
8+
9+
* Choose the **longest** **prefix** of `s` containing at most `k` **distinct** characters.
10+
* **Delete** the prefix from `s` and increase the number of partitions by one. The remaining characters (if any) in `s` maintain their initial order.
11+
12+
**Before** the operations, you are allowed to change **at most** **one** index in `s` to another lowercase English letter.
13+
14+
Return _an integer denoting the **maximum** number of resulting partitions after the operations by optimally choosing at most one index to change._
15+
16+
**Example 1:**
17+
18+
**Input:** s = "accca", k = 2
19+
20+
**Output:** 3
21+
22+
**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:
23+
- Choose the longest prefix containing at most 2 distinct characters, "<ins>ac</ins>bca".
24+
- Delete the prefix, and s becomes "bca". The number of partitions is now 1.
25+
- Choose the longest prefix containing at most 2 distinct characters, "<ins>bc</ins>a".
26+
- Delete the prefix, and s becomes "a". The number of partitions is now 2.
27+
- Choose the longest prefix containing at most 2 distinct characters, "<ins>a</ins>".
28+
- Delete the prefix, and s becomes empty. The number of partitions is now 3.
29+
30+
Hence, the answer is 3. It can be shown that it is not possible to obtain more than 3 partitions.
31+
32+
**Example 2:**
33+
34+
**Input:** s = "aabaab", k = 3
35+
36+
**Output:** 1
37+
38+
**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:
39+
- Choose the longest prefix containing at most 3 distinct characters, "<ins>aabaab</ins>".
40+
- Delete the prefix, and s becomes empty. The number of partitions becomes 1.
41+
42+
Hence, the answer is 1. It can be shown that it is not possible to obtain more than 1 partition.
43+
44+
**Example 3:**
45+
46+
**Input:** s = "xxyz", k = 1
47+
48+
**Output:** 4
49+
50+
**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:
51+
- Choose the longest prefix containing at most 1 distinct character, "<ins>x</ins>ayz".
52+
- Delete the prefix, and s becomes "ayz". The number of partitions is now 1.
53+
- Choose the longest prefix containing at most 1 distinct character, "<ins>a</ins>yz".
54+
- Delete the prefix, and s becomes "yz". The number of partitions is now 2.
55+
- Choose the longest prefix containing at most 1 distinct character, "<ins>y</ins>z".
56+
- Delete the prefix, and s becomes "z". The number of partitions is now 3.
57+
- Choose the longest prefix containing at most 1 distinct character, "<ins>z</ins>".
58+
- Delete the prefix, and s becomes empty. The number of partitions is now 4.
59+
60+
Hence, the answer is 4. It can be shown that it is not possible to obtain more than 4 partitions.
61+
62+
**Constraints:**
63+
64+
* <code>1 <= s.length <= 10<sup>4</sup></code>
65+
* `s` consists only of lowercase English letters.
66+
* `1 <= k <= 26`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package g3001_3100.s3005_count_elements_with_maximum_frequency;
2+
3+
// #Easy #Array #Hash_Table #Counting #2024_02_26_Time_1_ms_(99.76%)_Space_41.6_MB_(98.97%)
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class Solution {
9+
public int maxFrequencyElements(int[] nums) {
10+
if (nums.length == 1) {
11+
return 1;
12+
}
13+
List<Integer> list = new ArrayList<>();
14+
int co = 0;
15+
int prev = 0;
16+
for (int num : nums) {
17+
if (list.contains(num)) {
18+
continue;
19+
}
20+
list.add(num);
21+
if (list.size() == nums.length) {
22+
break;
23+
}
24+
int c = 0;
25+
for (int i : nums) {
26+
if (num == i) {
27+
c++;
28+
}
29+
}
30+
if (c > 1) {
31+
if (c > prev) {
32+
co = c;
33+
prev = c;
34+
} else if (c == prev) {
35+
co = c + co;
36+
}
37+
}
38+
}
39+
if (co == 0) {
40+
return nums.length;
41+
}
42+
return co;
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
3005\. Count Elements With Maximum Frequency
2+
3+
Easy
4+
5+
You are given an array `nums` consisting of **positive** integers.
6+
7+
Return _the **total frequencies** of elements in_ `nums` _such that those elements all have the **maximum** frequency_.
8+
9+
The **frequency** of an element is the number of occurrences of that element in the array.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [1,2,2,3,1,4]
14+
15+
**Output:** 4
16+
17+
**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.
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [1,2,3,4,5]
22+
23+
**Output:** 5
24+
25+
**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.
26+
27+
**Constraints:**
28+
29+
* `1 <= nums.length <= 100`
30+
* `1 <= nums[i] <= 100`

0 commit comments

Comments
 (0)