Skip to content

Commit 1e3216e

Browse files
authored
Added tasks 3001-3049
1 parent 29a5ed7 commit 1e3216e

File tree

45 files changed

+4598
-399
lines changed
  • src/main/kotlin
    • g2801_2900/s2815_max_pair_sum_in_an_array
    • g3001_3100
      • s3001_minimum_moves_to_capture_the_queen
      • s3002_maximum_size_of_a_set_after_removals
      • s3003_maximize_the_number_of_partitions_after_operations
      • s3005_count_elements_with_maximum_frequency
      • s3006_find_beautiful_indices_in_the_given_array_i
      • s3007_maximum_number_that_sum_of_the_prices_is_less_than_or_equal_to_k
      • s3008_find_beautiful_indices_in_the_given_array_ii
      • s3010_divide_an_array_into_subarrays_with_minimum_cost_i
      • s3011_find_if_array_can_be_sorted
      • s3012_minimize_length_of_array_using_operations
      • s3013_divide_an_array_into_subarrays_with_minimum_cost_ii
      • s3014_minimum_number_of_pushes_to_type_word_i
      • s3015_count_the_number_of_houses_at_a_certain_distance_i
      • s3016_minimum_number_of_pushes_to_type_word_ii
      • s3017_count_the_number_of_houses_at_a_certain_distance_ii
      • s3019_number_of_changing_keys
      • s3020_find_the_maximum_number_of_elements_in_subset
      • s3021_alice_and_bob_playing_flower_game
      • s3022_minimize_or_of_remaining_elements_using_operations
      • s3024_type_of_triangle
      • s3025_find_the_number_of_ways_to_place_people_i
      • s3026_maximum_good_subarray_sum
      • s3027_find_the_number_of_ways_to_place_people_ii
      • s3028_ant_on_the_boundary
      • s3029_minimum_time_to_revert_word_to_initial_state_i
      • s3030_find_the_grid_of_region_average
      • s3031_minimum_time_to_revert_word_to_initial_state_ii
      • s3033_modify_the_matrix
      • s3034_number_of_subarrays_that_match_a_pattern_i
      • s3035_maximum_palindromes_after_operations
      • s3036_number_of_subarrays_that_match_a_pattern_ii
      • s3038_maximum_number_of_operations_with_the_same_score_i
      • s3039_apply_operations_to_make_string_empty
      • s3040_maximum_number_of_operations_with_the_same_score_ii
      • s3041_maximize_consecutive_elements_in_an_array_after_modification
      • s3042_count_prefix_and_suffix_pairs_i
      • s3043_find_the_length_of_the_longest_common_prefix
      • s3044_most_frequent_prime
      • s3045_count_prefix_and_suffix_pairs_ii
      • s3046_split_the_array
      • s3047_find_the_largest_area_of_square_inside_two_rectangles
      • s3048_earliest_second_to_mark_indices_i
      • s3049_earliest_second_to_mark_indices_ii

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

+4598
-399
lines changed

README.md

Lines changed: 441 additions & 398 deletions
Large diffs are not rendered by default.

src/main/kotlin/g2801_2900/s2815_max_pair_sum_in_an_array/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class Solution {
6060
continue
6161
}
6262
val sum = value.poll() + value.poll()
63-
maxSum = max(maxSum.toDouble(), sum.toDouble()).toInt()
63+
maxSum = max(maxSum, sum)
6464
}
6565
return maxSum
6666
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3001\. Minimum Moves to Capture The Queen
5+
6+
Medium
7+
8+
There is a **1-indexed** `8 x 8` chessboard containing `3` pieces.
9+
10+
You are given `6` integers `a`, `b`, `c`, `d`, `e`, and `f` where:
11+
12+
* `(a, b)` denotes the position of the white rook.
13+
* `(c, d)` denotes the position of the white bishop.
14+
* `(e, f)` denotes the position of the black queen.
15+
16+
Given that you can only move the white pieces, return _the **minimum** number of moves required to capture the black queen_.
17+
18+
**Note** that:
19+
20+
* Rooks can move any number of squares either vertically or horizontally, but cannot jump over other pieces.
21+
* Bishops can move any number of squares diagonally, but cannot jump over other pieces.
22+
* A rook or a bishop can capture the queen if it is located in a square that they can move to.
23+
* The queen does not move.
24+
25+
**Example 1:**
26+
27+
![](https://assets.leetcode.com/uploads/2023/12/21/ex1.png)
28+
29+
**Input:** a = 1, b = 1, c = 8, d = 8, e = 2, f = 3
30+
31+
**Output:** 2
32+
33+
**Explanation:** We can capture the black queen in two moves by moving the white rook to (1, 3) then to (2, 3).
34+
35+
It is impossible to capture the black queen in less than two moves since it is not being attacked by any of the pieces at the beginning.
36+
37+
**Example 2:**
38+
39+
![](https://assets.leetcode.com/uploads/2023/12/21/ex2.png)
40+
41+
**Input:** a = 5, b = 3, c = 3, d = 4, e = 5, f = 2
42+
43+
**Output:** 1
44+
45+
**Explanation:** We can capture the black queen in a single move by doing one of the following:
46+
47+
- Move the white rook to (5, 2).
48+
49+
- Move the white bishop to (5, 2).
50+
51+
**Constraints:**
52+
53+
* `1 <= a, b, c, d, e, f <= 8`
54+
* No two pieces are on the same square.
55+
56+
## Solution
57+
58+
```kotlin
59+
import kotlin.math.abs
60+
61+
class Solution {
62+
fun minMovesToCaptureTheQueen(a: Int, b: Int, c: Int, d: Int, e: Int, f: Int): Int {
63+
if (a == e || b == f) {
64+
if (a == c && (d > b && d < f || d > f && d < b)) {
65+
return 2
66+
}
67+
if (b == d && (c > a && c < e || c > e && c < a)) {
68+
return 2
69+
}
70+
return 1
71+
} else if (abs(c - e) == abs(d - f)) {
72+
if (abs(a - c) == abs(b - d) &&
73+
abs(e - a) == abs(f - b) &&
74+
(a > e && a < c || a > c && a < e)
75+
) {
76+
return 2
77+
}
78+
return 1
79+
}
80+
return 2
81+
}
82+
}
83+
```
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3002\. Maximum Size of a Set After Removals
5+
6+
Medium
7+
8+
You are given two **0-indexed** integer arrays `nums1` and `nums2` of even length `n`.
9+
10+
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`.
11+
12+
Return _the **maximum** possible size of the set_ `s`.
13+
14+
**Example 1:**
15+
16+
**Input:** nums1 = [1,2,1,2], nums2 = [1,1,1,1]
17+
18+
**Output:** 2
19+
20+
**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}.
21+
22+
It can be shown that 2 is the maximum possible size of the set s after the removals.
23+
24+
**Example 2:**
25+
26+
**Input:** nums1 = [1,2,3,4,5,6], nums2 = [2,3,2,3,2,3]
27+
28+
**Output:** 5
29+
30+
**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}.
31+
32+
It can be shown that 5 is the maximum possible size of the set s after the removals.
33+
34+
**Example 3:**
35+
36+
**Input:** nums1 = [1,1,2,2,3,3], nums2 = [4,4,5,5,6,6]
37+
38+
**Output:** 6
39+
40+
**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}.
41+
42+
It can be shown that 6 is the maximum possible size of the set s after the removals.
43+
44+
**Constraints:**
45+
46+
* `n == nums1.length == nums2.length`
47+
* <code>1 <= n <= 2 * 10<sup>4</sup></code>
48+
* `n` is even.
49+
* <code>1 <= nums1[i], nums2[i] <= 10<sup>9</sup></code>
50+
51+
## Solution
52+
53+
```kotlin
54+
import kotlin.math.min
55+
56+
class Solution {
57+
fun maximumSetSize(nums1: IntArray, nums2: IntArray): Int {
58+
val uniq1 = HashSet<Int>()
59+
val uniq2 = HashSet<Int>()
60+
for (i in nums1.indices) {
61+
uniq1.add(nums1[i])
62+
uniq2.add(nums2[i])
63+
}
64+
var common = 0
65+
if (uniq1.size <= uniq2.size) {
66+
for (u in uniq1) {
67+
if (uniq2.contains(u)) {
68+
common++
69+
}
70+
}
71+
} else {
72+
for (u in uniq2) {
73+
if (uniq1.contains(u)) {
74+
common++
75+
}
76+
}
77+
}
78+
val half = nums1.size / 2
79+
val from1 = min(uniq1.size - common, half)
80+
val from2 = min(uniq2.size - common, half)
81+
val takeFromCommon1 = half - from1
82+
val takeFromCommon2 = half - from2
83+
return from1 + from2 + min(takeFromCommon1 + takeFromCommon2, common)
84+
}
85+
}
86+
```
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3003\. Maximize the Number of Partitions After Operations
5+
6+
Hard
7+
8+
You are given a **0-indexed** string `s` and an integer `k`.
9+
10+
You are to perform the following partitioning operations until `s` is **empty**:
11+
12+
* Choose the **longest** **prefix** of `s` containing at most `k` **distinct** characters.
13+
* **Delete** the prefix from `s` and increase the number of partitions by one. The remaining characters (if any) in `s` maintain their initial order.
14+
15+
**Before** the operations, you are allowed to change **at most** **one** index in `s` to another lowercase English letter.
16+
17+
Return _an integer denoting the **maximum** number of resulting partitions after the operations by optimally choosing at most one index to change._
18+
19+
**Example 1:**
20+
21+
**Input:** s = "accca", k = 2
22+
23+
**Output:** 3
24+
25+
**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:
26+
- Choose the longest prefix containing at most 2 distinct characters, "<ins>ac</ins>bca".
27+
- Delete the prefix, and s becomes "bca". The number of partitions is now 1.
28+
- Choose the longest prefix containing at most 2 distinct characters, "<ins>bc</ins>a".
29+
- Delete the prefix, and s becomes "a". The number of partitions is now 2.
30+
- Choose the longest prefix containing at most 2 distinct characters, "<ins>a</ins>".
31+
- Delete the prefix, and s becomes empty. The number of partitions is now 3.
32+
33+
Hence, the answer is 3. It can be shown that it is not possible to obtain more than 3 partitions.
34+
35+
**Example 2:**
36+
37+
**Input:** s = "aabaab", k = 3
38+
39+
**Output:** 1
40+
41+
**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:
42+
- Choose the longest prefix containing at most 3 distinct characters, "<ins>aabaab</ins>".
43+
- Delete the prefix, and s becomes empty. The number of partitions becomes 1.
44+
45+
Hence, the answer is 1. It can be shown that it is not possible to obtain more than 1 partition.
46+
47+
**Example 3:**
48+
49+
**Input:** s = "xxyz", k = 1
50+
51+
**Output:** 4
52+
53+
**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:
54+
- Choose the longest prefix containing at most 1 distinct character, "<ins>x</ins>ayz".
55+
- Delete the prefix, and s becomes "ayz". The number of partitions is now 1.
56+
- Choose the longest prefix containing at most 1 distinct character, "<ins>a</ins>yz".
57+
- Delete the prefix, and s becomes "yz". The number of partitions is now 2.
58+
- Choose the longest prefix containing at most 1 distinct character, "<ins>y</ins>z".
59+
- Delete the prefix, and s becomes "z". The number of partitions is now 3.
60+
- Choose the longest prefix containing at most 1 distinct character, "<ins>z</ins>".
61+
- Delete the prefix, and s becomes empty. The number of partitions is now 4.
62+
63+
Hence, the answer is 4. It can be shown that it is not possible to obtain more than 4 partitions.
64+
65+
**Constraints:**
66+
67+
* <code>1 <= s.length <= 10<sup>4</sup></code>
68+
* `s` consists only of lowercase English letters.
69+
* `1 <= k <= 26`
70+
71+
## Solution
72+
73+
```kotlin
74+
import kotlin.math.max
75+
76+
class Solution {
77+
fun maxPartitionsAfterOperations(s: String, k: Int): Int {
78+
if (k == ALPHABET_SIZE) {
79+
return 1
80+
}
81+
val n = s.length
82+
val ansr = IntArray(n)
83+
val usedr = IntArray(n)
84+
var used = 0
85+
var cntUsed = 0
86+
var ans = 1
87+
for (i in n - 1 downTo 0) {
88+
val ch = s[i].code - 'a'.code
89+
if ((used and (1 shl ch)) == 0) {
90+
if (cntUsed == k) {
91+
cntUsed = 0
92+
used = 0
93+
ans++
94+
}
95+
used = used or (1 shl ch)
96+
cntUsed++
97+
}
98+
ansr[i] = ans
99+
usedr[i] = used
100+
}
101+
var ansl = 0
102+
ans = ansr[0]
103+
var l = 0
104+
while (l < n) {
105+
used = 0
106+
cntUsed = 0
107+
var usedBeforeLast = 0
108+
var usedTwiceBeforeLast = 0
109+
var last = -1
110+
var r = l
111+
while (r < n) {
112+
val ch = s[r].code - 'a'.code
113+
if ((used and (1 shl ch)) == 0) {
114+
if (cntUsed == k) {
115+
break
116+
}
117+
usedBeforeLast = used
118+
last = r
119+
used = used or (1 shl ch)
120+
cntUsed++
121+
} else if (cntUsed < k) {
122+
usedTwiceBeforeLast = usedTwiceBeforeLast or (1 shl ch)
123+
}
124+
r++
125+
}
126+
if (cntUsed == k) {
127+
if (last - l > Integer.bitCount(usedBeforeLast)) {
128+
ans = max(ans, (ansl + 1 + ansr[last]))
129+
}
130+
if (last + 1 < r) {
131+
if (last + 2 >= n) {
132+
ans = max(ans, (ansl + 1 + 1))
133+
} else {
134+
if (Integer.bitCount(usedr[last + 2]) == k) {
135+
val canUse = ((1 shl ALPHABET_SIZE) - 1) and used.inv() and usedr[last + 2].inv()
136+
ans = if (canUse > 0) {
137+
max(ans, (ansl + 1 + 1 + ansr[last + 2]))
138+
} else {
139+
max(ans, (ansl + 1 + ansr[last + 2]))
140+
}
141+
val l1 = s[last + 1].code - 'a'.code
142+
if ((usedTwiceBeforeLast and (1 shl l1)) == 0) {
143+
ans = max(ans, (ansl + 1 + ansr[last + 1]))
144+
}
145+
} else {
146+
ans = max(ans, (ansl + 1 + ansr[last + 2]))
147+
}
148+
}
149+
}
150+
}
151+
l = r
152+
ansl++
153+
}
154+
return ans
155+
}
156+
157+
companion object {
158+
private const val ALPHABET_SIZE = 'z'.code - 'a'.code + 1
159+
}
160+
}
161+
```

0 commit comments

Comments
 (0)