Skip to content

Commit f0e170a

Browse files
authored
Added tasks 3002-3019
1 parent f3c963f commit f0e170a

File tree

45 files changed

+1878
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1878
-0
lines changed
Lines changed: 36 additions & 0 deletions
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_28_Time_467_ms_(100.00%)_Space_51.9_MB_(100.00%)
4+
5+
import kotlin.math.min
6+
7+
class Solution {
8+
fun maximumSetSize(nums1: IntArray, nums2: IntArray): Int {
9+
val uniq1 = HashSet<Int>()
10+
val uniq2 = HashSet<Int>()
11+
for (i in nums1.indices) {
12+
uniq1.add(nums1[i])
13+
uniq2.add(nums2[i])
14+
}
15+
var common = 0
16+
if (uniq1.size <= uniq2.size) {
17+
for (u in uniq1) {
18+
if (uniq2.contains(u)) {
19+
common++
20+
}
21+
}
22+
} else {
23+
for (u in uniq2) {
24+
if (uniq1.contains(u)) {
25+
common++
26+
}
27+
}
28+
}
29+
val half = nums1.size / 2
30+
val from1 = min(uniq1.size - common, half)
31+
val from2 = min(uniq2.size - common, half)
32+
val takeFromCommon1 = half - from1
33+
val takeFromCommon2 = half - from2
34+
return from1 + from2 + min(takeFromCommon1 + takeFromCommon2, common)
35+
}
36+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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}.
18+
19+
It can be shown that 2 is the maximum possible size of the set s after the removals.
20+
21+
**Example 2:**
22+
23+
**Input:** nums1 = [1,2,3,4,5,6], nums2 = [2,3,2,3,2,3]
24+
25+
**Output:** 5
26+
27+
**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}.
28+
29+
It can be shown that 5 is the maximum possible size of the set s after the removals.
30+
31+
**Example 3:**
32+
33+
**Input:** nums1 = [1,1,2,2,3,3], nums2 = [4,4,5,5,6,6]
34+
35+
**Output:** 6
36+
37+
**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}.
38+
39+
It can be shown that 6 is the maximum possible size of the set s after the removals.
40+
41+
**Constraints:**
42+
43+
* `n == nums1.length == nums2.length`
44+
* <code>1 <= n <= 2 * 10<sup>4</sup></code>
45+
* `n` is even.
46+
* <code>1 <= nums1[i], nums2[i] <= 10<sup>9</sup></code>
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package g3001_3100.s3003_maximize_the_number_of_partitions_after_operations
2+
3+
// #Hard #String #Dynamic_Programming #Bit_Manipulation #Bitmask
4+
// #2024_02_28_Time_147_ms_(100.00%)_Space_35.7_MB_(100.00%)
5+
6+
import kotlin.math.max
7+
8+
class Solution {
9+
fun maxPartitionsAfterOperations(s: String, k: Int): Int {
10+
if (k == ALPHABET_SIZE) {
11+
return 1
12+
}
13+
val n = s.length
14+
val ansr = IntArray(n)
15+
val usedr = IntArray(n)
16+
var used = 0
17+
var cntUsed = 0
18+
var ans = 1
19+
for (i in n - 1 downTo 0) {
20+
val ch = s[i].code - 'a'.code
21+
if ((used and (1 shl ch)) == 0) {
22+
if (cntUsed == k) {
23+
cntUsed = 0
24+
used = 0
25+
ans++
26+
}
27+
used = used or (1 shl ch)
28+
cntUsed++
29+
}
30+
ansr[i] = ans
31+
usedr[i] = used
32+
}
33+
var ansl = 0
34+
ans = ansr[0]
35+
var l = 0
36+
while (l < n) {
37+
used = 0
38+
cntUsed = 0
39+
var usedBeforeLast = 0
40+
var usedTwiceBeforeLast = 0
41+
var last = -1
42+
var r = l
43+
while (r < n) {
44+
val ch = s[r].code - 'a'.code
45+
if ((used and (1 shl ch)) == 0) {
46+
if (cntUsed == k) {
47+
break
48+
}
49+
usedBeforeLast = used
50+
last = r
51+
used = used or (1 shl ch)
52+
cntUsed++
53+
} else if (cntUsed < k) {
54+
usedTwiceBeforeLast = usedTwiceBeforeLast or (1 shl ch)
55+
}
56+
r++
57+
}
58+
if (cntUsed == k) {
59+
if (last - l > Integer.bitCount(usedBeforeLast)) {
60+
ans = max(ans, (ansl + 1 + ansr[last]))
61+
}
62+
if (last + 1 < r) {
63+
if (last + 2 >= n) {
64+
ans = max(ans, (ansl + 1 + 1))
65+
} else {
66+
if (Integer.bitCount(usedr[last + 2]) == k) {
67+
val canUse = ((1 shl ALPHABET_SIZE) - 1) and used.inv() and usedr[last + 2].inv()
68+
ans = if (canUse > 0) {
69+
max(ans, (ansl + 1 + 1 + ansr[last + 2]))
70+
} else {
71+
max(ans, (ansl + 1 + ansr[last + 2]))
72+
}
73+
val l1 = s[last + 1].code - 'a'.code
74+
if ((usedTwiceBeforeLast and (1 shl l1)) == 0) {
75+
ans = max(ans, (ansl + 1 + ansr[last + 1]))
76+
}
77+
} else {
78+
ans = max(ans, (ansl + 1 + ansr[last + 2]))
79+
}
80+
}
81+
}
82+
}
83+
l = r
84+
ansl++
85+
}
86+
return ans
87+
}
88+
89+
companion object {
90+
private const val ALPHABET_SIZE = 'z'.code - 'a'.code + 1
91+
}
92+
}
Lines changed: 66 additions & 0 deletions
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`
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package g3001_3100.s3005_count_elements_with_maximum_frequency
2+
3+
// #Easy #Array #Hash_Table #Counting #2024_02_28_Time_168_ms_(80.00%)_Space_34.8_MB_(99.09%)
4+
5+
class Solution {
6+
fun maxFrequencyElements(nums: IntArray): Int {
7+
if (nums.size == 1) {
8+
return 1
9+
}
10+
val list: MutableList<Int> = ArrayList()
11+
var co = 0
12+
var prev = 0
13+
for (num in nums) {
14+
if (list.contains(num)) {
15+
continue
16+
}
17+
list.add(num)
18+
if (list.size == nums.size) {
19+
break
20+
}
21+
var c = 0
22+
for (i in nums) {
23+
if (num == i) {
24+
c++
25+
}
26+
}
27+
if (c > 1) {
28+
if (c > prev) {
29+
co = c
30+
prev = c
31+
} else if (c == prev) {
32+
co += c
33+
}
34+
}
35+
}
36+
if (co == 0) {
37+
return nums.size
38+
}
39+
return co
40+
}
41+
}
Lines changed: 30 additions & 0 deletions
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)