Skip to content

Commit 674770b

Browse files
committed
Added tasks 3467-3475
1 parent 3e957bc commit 674770b

File tree

11 files changed

+1026
-139
lines changed
  • src/main/kotlin
    • g0001_0100/s0030_substring_with_concatenation_of_all_words
    • g3401_3500
      • s3467_transform_array_by_parity
      • s3468_find_the_number_of_copy_arrays
      • s3469_find_minimum_cost_to_remove_array_elements
      • s3470_permutations_iv
      • s3471_find_the_largest_almost_missing_integer
      • s3472_longest_palindromic_subsequence_after_at_most_k_operations
      • s3473_sum_of_k_subarrays_with_length_at_least_m
      • s3474_lexicographically_smallest_generated_string
      • s3475_dna_pattern_recognition

11 files changed

+1026
-139
lines changed

README.md

+116-107
Large diffs are not rendered by default.

src/main/kotlin/g0001_0100/s0030_substring_with_concatenation_of_all_words/readme.md

+29-32
Original file line numberDiff line numberDiff line change
@@ -71,44 +71,41 @@ The substring starting at 12 is "thefoobar". It is the concatenation of ["the","
7171
```kotlin
7272
class Solution {
7373
fun findSubstring(s: String, words: Array<String>): List<Int> {
74-
val indices: MutableList<Int> = ArrayList()
75-
if (words.size == 0) {
76-
return indices
74+
val ans: MutableList<Int> = ArrayList<Int>()
75+
val n1 = words[0].length
76+
val n2 = s.length
77+
val map1: MutableMap<String, Int> = HashMap<String, Int>()
78+
for (ch in words) {
79+
map1.put(ch, map1.getOrDefault(ch, 0) + 1)
7780
}
78-
// Put each word into a HashMap and calculate word frequency
79-
val wordMap: MutableMap<String, Int> = HashMap()
80-
for (word in words) {
81-
wordMap[word] = wordMap.getOrDefault(word, 0) + 1
82-
}
83-
val wordLength = words[0].length
84-
val window = words.size * wordLength
85-
for (i in 0 until wordLength) {
86-
// move a word's length each time
81+
for (i in 0..<n1) {
82+
var left = i
8783
var j = i
88-
while (j + window <= s.length) {
89-
// get the subStr
90-
val subStr = s.substring(j, j + window)
91-
val map: MutableMap<String, Int> = HashMap()
92-
// start from the last word
93-
for (k in words.indices.reversed()) {
94-
// get the word from subStr
95-
val word = subStr.substring(k * wordLength, (k + 1) * wordLength)
96-
val count = map.getOrDefault(word, 0) + 1
97-
// if the num of the word is greater than wordMap's, move (k * wordLength) and
98-
// break
99-
if (count > wordMap.getOrDefault(word, 0)) {
100-
j = j + k * wordLength
101-
break
102-
} else if (k == 0) {
103-
indices.add(j)
104-
} else {
105-
map[word] = count
84+
var c = 0
85+
val map2: MutableMap<String, Int> = HashMap<String, Int>()
86+
while (j + n1 <= n2) {
87+
val word1 = s.substring(j, j + n1)
88+
j += n1
89+
if (map1.containsKey(word1)) {
90+
map2.put(word1, map2.getOrDefault(word1, 0) + 1)
91+
c++
92+
while (map2[word1]!! > map1[word1]!!) {
93+
val word2 = s.substring(left, left + n1)
94+
map2.put(word2, map2[word2]!! - 1)
95+
left += n1
96+
c--
97+
}
98+
if (c == words.size) {
99+
ans.add(left)
106100
}
101+
} else {
102+
map2.clear()
103+
c = 0
104+
left = j
107105
}
108-
j = j + wordLength
109106
}
110107
}
111-
return indices
108+
return ans
112109
}
113110
}
114111
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
## 3467\. Transform Array by Parity
5+
6+
Easy
7+
8+
You are given an integer array `nums`. Transform `nums` by performing the following operations in the **exact** order specified:
9+
10+
1. Replace each even number with 0.
11+
2. Replace each odd numbers with 1.
12+
3. Sort the modified array in **non-decreasing** order.
13+
14+
Return the resulting array after performing these operations.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [4,3,2,1]
19+
20+
**Output:** [0,0,1,1]
21+
22+
**Explanation:**
23+
24+
* Replace the even numbers (4 and 2) with 0 and the odd numbers (3 and 1) with 1. Now, `nums = [0, 1, 0, 1]`.
25+
* After sorting `nums` in non-descending order, `nums = [0, 0, 1, 1]`.
26+
27+
**Example 2:**
28+
29+
**Input:** nums = [1,5,1,4,2]
30+
31+
**Output:** [0,0,1,1,1]
32+
33+
**Explanation:**
34+
35+
* Replace the even numbers (4 and 2) with 0 and the odd numbers (1, 5 and 1) with 1. Now, `nums = [1, 1, 1, 0, 0]`.
36+
* After sorting `nums` in non-descending order, `nums = [0, 0, 1, 1, 1]`.
37+
38+
**Constraints:**
39+
40+
* `1 <= nums.length <= 100`
41+
* `1 <= nums[i] <= 1000`
42+
43+
## Solution
44+
45+
```kotlin
46+
class Solution {
47+
fun transformArray(nums: IntArray): IntArray {
48+
val size = nums.size
49+
val ans = IntArray(size)
50+
var countEven = 0
51+
for (i in nums.indices) {
52+
if (nums[i] and 1 == 0) {
53+
countEven++
54+
}
55+
}
56+
for (i in countEven until size) {
57+
ans[i] = 1
58+
}
59+
return ans
60+
}
61+
}
62+
```
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+
## 3468\. Find the Number of Copy Arrays
5+
6+
Medium
7+
8+
You are given an array `original` of length `n` and a 2D array `bounds` of length `n x 2`, where <code>bounds[i] = [u<sub>i</sub>, v<sub>i</sub>]</code>.
9+
10+
You need to find the number of **possible** arrays `copy` of length `n` such that:
11+
12+
1. `(copy[i] - copy[i - 1]) == (original[i] - original[i - 1])` for `1 <= i <= n - 1`.
13+
2. <code>u<sub>i</sub> <= copy[i] <= v<sub>i</sub></code> for `0 <= i <= n - 1`.
14+
15+
Return the number of such arrays.
16+
17+
**Example 1:**
18+
19+
**Input:** original = [1,2,3,4], bounds = \[\[1,2],[2,3],[3,4],[4,5]]
20+
21+
**Output:** 2
22+
23+
**Explanation:**
24+
25+
The possible arrays are:
26+
27+
* `[1, 2, 3, 4]`
28+
* `[2, 3, 4, 5]`
29+
30+
**Example 2:**
31+
32+
**Input:** original = [1,2,3,4], bounds = \[\[1,10],[2,9],[3,8],[4,7]]
33+
34+
**Output:** 4
35+
36+
**Explanation:**
37+
38+
The possible arrays are:
39+
40+
* `[1, 2, 3, 4]`
41+
* `[2, 3, 4, 5]`
42+
* `[3, 4, 5, 6]`
43+
* `[4, 5, 6, 7]`
44+
45+
**Example 3:**
46+
47+
**Input:** original = [1,2,1,2], bounds = \[\[1,1],[2,3],[3,3],[2,3]]
48+
49+
**Output:** 0
50+
51+
**Explanation:**
52+
53+
No array is possible.
54+
55+
**Constraints:**
56+
57+
* <code>2 <= n == original.length <= 10<sup>5</sup></code>
58+
* <code>1 <= original[i] <= 10<sup>9</sup></code>
59+
* `bounds.length == n`
60+
* `bounds[i].length == 2`
61+
* <code>1 <= bounds[i][0] <= bounds[i][1] <= 10<sup>9</sup></code>
62+
63+
## Solution
64+
65+
```kotlin
66+
import kotlin.math.max
67+
import kotlin.math.min
68+
69+
class Solution {
70+
fun countArrays(original: IntArray, bounds: Array<IntArray>): Int {
71+
var low = bounds[0][0]
72+
var high = bounds[0][1]
73+
var ans = high - low + 1
74+
for (i in 1..<original.size) {
75+
val diff = original[i] - original[i - 1]
76+
low = max((low + diff), bounds[i][0])
77+
high = min((high + diff), bounds[i][1])
78+
ans = min(ans, (high - low + 1)).toInt()
79+
}
80+
return max(ans, 0)
81+
}
82+
}
83+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
## 3469\. Find Minimum Cost to Remove Array Elements
5+
6+
Medium
7+
8+
You are given an integer array `nums`. Your task is to remove **all elements** from the array by performing one of the following operations at each step until `nums` is empty:
9+
10+
* Choose any two elements from the first three elements of `nums` and remove them. The cost of this operation is the **maximum** of the two elements removed.
11+
* If fewer than three elements remain in `nums`, remove all the remaining elements in a single operation. The cost of this operation is the **maximum** of the remaining elements.
12+
13+
Return the **minimum** cost required to remove all the elements.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [6,2,8,4]
18+
19+
**Output:** 12
20+
21+
**Explanation:**
22+
23+
Initially, `nums = [6, 2, 8, 4]`.
24+
25+
* In the first operation, remove `nums[0] = 6` and `nums[2] = 8` with a cost of `max(6, 8) = 8`. Now, `nums = [2, 4]`.
26+
* In the second operation, remove the remaining elements with a cost of `max(2, 4) = 4`.
27+
28+
The cost to remove all elements is `8 + 4 = 12`. This is the minimum cost to remove all elements in `nums`. Hence, the output is 12.
29+
30+
**Example 2:**
31+
32+
**Input:** nums = [2,1,3,3]
33+
34+
**Output:** 5
35+
36+
**Explanation:**
37+
38+
Initially, `nums = [2, 1, 3, 3]`.
39+
40+
* In the first operation, remove `nums[0] = 2` and `nums[1] = 1` with a cost of `max(2, 1) = 2`. Now, `nums = [3, 3]`.
41+
* In the second operation remove the remaining elements with a cost of `max(3, 3) = 3`.
42+
43+
The cost to remove all elements is `2 + 3 = 5`. This is the minimum cost to remove all elements in `nums`. Hence, the output is 5.
44+
45+
**Constraints:**
46+
47+
* `1 <= nums.length <= 1000`
48+
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
49+
50+
## Solution
51+
52+
```kotlin
53+
import kotlin.math.max
54+
import kotlin.math.min
55+
56+
class Solution {
57+
fun minCost(nums: IntArray): Int {
58+
var nums = nums
59+
var n = nums.size
60+
if (n % 2 == 0) {
61+
nums = nums.copyOf(++n)
62+
}
63+
val dp = IntArray(n)
64+
var j = 1
65+
while (j < n - 1) {
66+
var cost1: Int = INF
67+
var cost2: Int = INF
68+
val max = max(nums[j], nums[j + 1])
69+
for (i in 0..<j) {
70+
cost1 =
71+
min(cost1, dp[i] + max(nums[i], nums[j + 1]))
72+
cost2 = min(cost2, dp[i] + max(nums[i], nums[j]))
73+
dp[i] += max
74+
}
75+
dp[j] = cost1
76+
dp[j + 1] = cost2
77+
j += 2
78+
}
79+
var result: Int = INF
80+
for (i in 0..<n) {
81+
result = min(result, dp[i] + nums[i])
82+
}
83+
return result
84+
}
85+
86+
companion object {
87+
private const val INF = 1e9.toInt()
88+
}
89+
}
90+
```

0 commit comments

Comments
 (0)