Skip to content

Commit 601436a

Browse files
authored
Improved tasks 2471-3478
1 parent 1417b8e commit 601436a

File tree

24 files changed

+168
-158
lines changed
  • src
    • main/kotlin
      • g2401_2500/s2471_minimum_number_of_operations_to_sort_a_binary_tree_by_level
      • g2601_2700/s2608_shortest_cycle_in_a_graph
      • g3101_3200/s3165_maximum_sum_of_subsequence_with_non_adjacent_elements
      • g3201_3300
      • g3301_3400
        • s3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii
        • s3311_construct_2d_grid_matching_graph_layout
        • s3318_find_x_sum_of_all_k_long_subarrays_i
        • s3319_k_th_largest_perfect_subtree_size_in_binary_tree
        • s3320_count_the_number_of_winning_sequences
        • s3321_find_x_sum_of_all_k_long_subarrays_ii
        • s3327_check_if_dfs_strings_are_palindromes
        • s3333_find_the_original_typed_string_ii
        • s3334_find_the_maximum_factor_score_of_array
        • s3335_total_characters_in_string_after_transformations_i
        • s3342_find_minimum_time_to_reach_last_room_ii
        • s3343_count_number_of_balanced_permutations
        • s3361_shift_distance_between_two_strings
        • s3362_zero_array_transformation_iii
        • s3365_rearrange_k_substrings_to_form_target_string
        • s3387_maximize_amount_after_two_days_of_conversions
      • g3401_3500/s3478_choose_k_elements_with_maximum_sum
    • test/kotlin/g3301_3400/s3320_count_the_number_of_winning_sequences

24 files changed

+168
-158
lines changed

Diff for: src/main/kotlin/g2401_2500/s2471_minimum_number_of_operations_to_sort_a_binary_tree_by_level/Solution.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import java.util.ArrayDeque
1818
*/
1919
class Solution {
2020
fun minimumOperations(root: TreeNode): Int {
21-
val q = ArrayDeque<TreeNode?>()
21+
val q = ArrayDeque<TreeNode>()
2222
var count = 0
2323
if (root.left != null && root.right != null && root.left!!.`val` > root.right!!.`val`) {
2424
count++
@@ -55,14 +55,14 @@ class Solution {
5555
sorted[i] = list[i]
5656
}
5757
sorted.sort()
58-
val ind: MutableMap<Int, Int?> = HashMap()
58+
val ind: MutableMap<Int, Int> = HashMap()
5959
for (i in list.indices) {
6060
ind[list[i]] = i
6161
}
6262
for (i in list.indices) {
6363
if (list[i] != sorted[i]) {
6464
swaps++
65-
ind[list[i]] = ind[sorted[i]]
65+
ind[list[i]] = ind[sorted[i]]!!
6666
list[ind[sorted[i]]!!] = list[i]
6767
}
6868
}

Diff for: src/main/kotlin/g2601_2700/s2608_shortest_cycle_in_a_graph/Solution.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ class Solution {
1515
adj[edge[1]].add(edge[0])
1616
}
1717
for (i in 0 until n) {
18-
dfs(adj, HashSet<Int?>(), i)
18+
dfs(adj, HashSet<Int>(), i)
1919
}
2020
return if (min == Int.MAX_VALUE) -1 else min
2121
}
2222

23-
private fun dfs(adj: List<MutableList<Int>>, set: HashSet<Int?>, node: Int) {
23+
private fun dfs(adj: List<MutableList<Int>>, set: HashSet<Int>, node: Int) {
2424
val queue: Queue<IntArray> = LinkedList()
2525
set.add(node)
2626
queue.add(intArrayOf(node, node))

Diff for: src/main/kotlin/g3101_3200/s3165_maximum_sum_of_subsequence_with_non_adjacent_elements/Solution.kt

+48-48
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import kotlin.math.max
77

88
class Solution {
99
fun maximumSumSubsequence(nums: IntArray, queries: Array<IntArray>): Int {
10-
val tree: Array<LongArray?> = build(nums)
10+
val tree: Array<LongArray> = build(nums)
1111
var result: Long = 0
1212
for (i in queries.indices) {
1313
result += set(tree, queries[i][0], queries[i][1])
@@ -23,103 +23,103 @@ class Solution {
2323
private const val NN = 3
2424
private const val MOD = 1000000007
2525

26-
private fun build(nums: IntArray): Array<LongArray?> {
26+
private fun build(nums: IntArray): Array<LongArray> {
2727
val len = nums.size
2828
var size = 1
2929
while (size < len) {
3030
size = size shl 1
3131
}
32-
val tree = Array<LongArray?>(size * 2) { LongArray(4) }
32+
val tree = Array<LongArray>(size * 2) { LongArray(4) }
3333
for (i in 0 until len) {
34-
tree[size + i]!![YY] = nums[i].toLong()
34+
tree[size + i][YY] = nums[i].toLong()
3535
}
3636
for (i in size - 1 downTo 1) {
37-
tree[i]!![YY] = max(
38-
(tree[2 * i]!![YY] + tree[2 * i + 1]!![NY]),
37+
tree[i][YY] = max(
38+
(tree[2 * i][YY] + tree[2 * i + 1][NY]),
3939
(
40-
tree[2 * i]!![YN] + max(
41-
tree[2 * i + 1]!![YY],
42-
tree[2 * i + 1]!![NY],
40+
tree[2 * i][YN] + max(
41+
tree[2 * i + 1][YY],
42+
tree[2 * i + 1][NY],
4343
)
4444
),
4545
)
46-
tree[i]!![YN] = max(
47-
(tree[2 * i]!![YY] + tree[2 * i + 1]!![NN]),
46+
tree[i][YN] = max(
47+
(tree[2 * i][YY] + tree[2 * i + 1][NN]),
4848
(
49-
tree[2 * i]!![YN] + max(
50-
tree[2 * i + 1]!![YN],
51-
tree[2 * i + 1]!![NN],
49+
tree[2 * i][YN] + max(
50+
tree[2 * i + 1][YN],
51+
tree[2 * i + 1][NN],
5252
)
5353
),
5454
)
55-
tree[i]!![NY] = max(
56-
(tree[2 * i]!![NY] + tree[2 * i + 1]!![NY]),
55+
tree[i][NY] = max(
56+
(tree[2 * i][NY] + tree[2 * i + 1][NY]),
5757
(
58-
tree[2 * i]!![NN] + max(
59-
tree[2 * i + 1]!![YY],
60-
tree[2 * i + 1]!![NY],
58+
tree[2 * i][NN] + max(
59+
tree[2 * i + 1][YY],
60+
tree[2 * i + 1][NY],
6161
)
6262
),
6363
)
64-
tree[i]!![NN] = max(
65-
(tree[2 * i]!![NY] + tree[2 * i + 1]!![NN]),
64+
tree[i][NN] = max(
65+
(tree[2 * i][NY] + tree[2 * i + 1][NN]),
6666
(
67-
tree[2 * i]!![NN] + max(
68-
tree[2 * i + 1]!![YN],
69-
tree[2 * i + 1]!![NN],
67+
tree[2 * i][NN] + max(
68+
tree[2 * i + 1][YN],
69+
tree[2 * i + 1][NN],
7070
)
7171
),
7272
)
7373
}
7474
return tree
7575
}
7676

77-
private fun set(tree: Array<LongArray?>, idx: Int, `val`: Int): Long {
77+
private fun set(tree: Array<LongArray>, idx: Int, `val`: Int): Long {
7878
val size = tree.size / 2
79-
tree[size + idx]!![YY] = `val`.toLong()
79+
tree[size + idx][YY] = `val`.toLong()
8080
var i = (size + idx) / 2
8181
while (i > 0) {
82-
tree[i]!![YY] = max(
83-
(tree[2 * i]!![YY] + tree[2 * i + 1]!![NY]),
82+
tree[i][YY] = max(
83+
(tree[2 * i][YY] + tree[2 * i + 1][NY]),
8484
(
85-
tree[2 * i]!![YN] + max(
86-
tree[2 * i + 1]!![YY],
87-
tree[2 * i + 1]!![NY],
85+
tree[2 * i][YN] + max(
86+
tree[2 * i + 1][YY],
87+
tree[2 * i + 1][NY],
8888
)
8989
),
9090
)
91-
tree[i]!![YN] = max(
92-
(tree[2 * i]!![YY] + tree[2 * i + 1]!![NN]),
91+
tree[i][YN] = max(
92+
(tree[2 * i][YY] + tree[2 * i + 1][NN]),
9393
(
94-
tree[2 * i]!![YN] + max(
95-
tree[2 * i + 1]!![YN],
96-
tree[2 * i + 1]!![NN],
94+
tree[2 * i][YN] + max(
95+
tree[2 * i + 1][YN],
96+
tree[2 * i + 1][NN],
9797
)
9898
),
9999
)
100-
tree[i]!![NY] = max(
101-
(tree[2 * i]!![NY] + tree[2 * i + 1]!![NY]),
100+
tree[i][NY] = max(
101+
(tree[2 * i][NY] + tree[2 * i + 1][NY]),
102102
(
103-
tree[2 * i]!![NN] + max(
104-
tree[2 * i + 1]!![YY],
105-
tree[2 * i + 1]!![NY],
103+
tree[2 * i][NN] + max(
104+
tree[2 * i + 1][YY],
105+
tree[2 * i + 1][NY],
106106
)
107107
),
108108
)
109-
tree[i]!![NN] = max(
110-
(tree[2 * i]!![NY] + tree[2 * i + 1]!![NN]),
109+
tree[i][NN] = max(
110+
(tree[2 * i][NY] + tree[2 * i + 1][NN]),
111111
(
112-
tree[2 * i]!![NN] + max(
113-
tree[2 * i + 1]!![YN],
114-
tree[2 * i + 1]!![NN],
112+
tree[2 * i][NN] + max(
113+
tree[2 * i + 1][YN],
114+
tree[2 * i + 1][NN],
115115
)
116116
),
117117
)
118118
i /= 2
119119
}
120120
return max(
121-
tree[1]!![YY],
122-
max(tree[1]!![YN], max(tree[1]!![NY], tree[1]!![NN])),
121+
tree[1][YY],
122+
max(tree[1][YN], max(tree[1][NY], tree[1][NN])),
123123
)
124124
}
125125
}

Diff for: src/main/kotlin/g3201_3300/s3245_alternating_groups_iii/Solution.kt

+6-6
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class Solution {
7474
}
7575

7676
// Initializes the alternating groups using the colors array.
77-
private fun initializeGroups(colors: IntArray, groups: TreeMap<Int?, Int?>) {
77+
private fun initializeGroups(colors: IntArray, groups: TreeMap<Int, Int>) {
7878
val n = colors.size
7979
var i = 0
8080
while (i < n) {
@@ -95,7 +95,7 @@ class Solution {
9595

9696
// Processes a type 1 query: returns the number of alternating groups
9797
// of at least the given size.
98-
private fun processQueryType1(colors: IntArray, groups: TreeMap<Int?, Int?>, groupSize: Int): Int {
98+
private fun processQueryType1(colors: IntArray, groups: TreeMap<Int, Int>, groupSize: Int): Int {
9999
var ans = qry(groupSize)
100100
val firstGroup = groups.firstEntry()
101101
val lastGroup = groups.lastEntry()
@@ -114,7 +114,7 @@ class Solution {
114114
// Processes a type 2 query: updates the color at index x and adjusts groups.
115115
private fun processQueryType2(
116116
colors: IntArray,
117-
groups: TreeMap<Int?, Int?>,
117+
groups: TreeMap<Int, Int>,
118118
x: Int,
119119
newColor: Int,
120120
) {
@@ -175,10 +175,10 @@ class Solution {
175175
}
176176

177177
// Main function to handle queries on alternating groups.
178-
fun numberOfAlternatingGroups(colors: IntArray, queries: Array<IntArray>): MutableList<Int?> {
179-
val groups = TreeMap<Int?, Int?>()
178+
fun numberOfAlternatingGroups(colors: IntArray, queries: Array<IntArray>): MutableList<Int> {
179+
val groups = TreeMap<Int, Int>()
180180
val n = colors.size
181-
val results: MutableList<Int?> = ArrayList<Int?>()
181+
val results: MutableList<Int> = ArrayList<Int>()
182182
// Initialize alternating groups.
183183
initializeGroups(colors, groups)
184184
// Process each query.

Diff for: src/main/kotlin/g3201_3300/s3286_find_a_safe_walk_through_a_grid/Solution.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ class Solution {
1313
val m = grid[0].size
1414
val dr = intArrayOf(0, 0, 1, -1)
1515
val dc = intArrayOf(1, -1, 0, 0)
16-
val visited = Array<Array<BooleanArray>?>(n) { Array<BooleanArray>(m) { BooleanArray(health + 1) } }
16+
val visited = Array<Array<BooleanArray>>(n) { Array<BooleanArray>(m) { BooleanArray(health + 1) } }
1717
val bfs: Queue<IntArray?> = LinkedList<IntArray>()
1818
bfs.add(intArrayOf(0, 0, health - grid[0][0]))
19-
visited[0]!![0][health - grid[0][0]] = true
19+
visited[0][0][health - grid[0][0]] = true
2020
while (bfs.isNotEmpty()) {
2121
var size = bfs.size
2222
while (size-- > 0) {
@@ -32,8 +32,8 @@ class Solution {
3232
val nc = c + dc[k]
3333
if (isValidMove(nr, nc, n, m)) {
3434
val nh: Int = h - grid[nr][nc]
35-
if (nh >= 0 && !visited[nr]!![nc][nh]) {
36-
visited[nr]!![nc][nh] = true
35+
if (nh >= 0 && !visited[nr][nc][nh]) {
36+
visited[nr][nc][nh] = true
3737
bfs.add(intArrayOf(nr, nc, nh))
3838
}
3939
}

Diff for: src/main/kotlin/g3201_3300/s3288_length_of_the_longest_increasing_path/Solution.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ class Solution {
3939
}
4040

4141
private fun longestIncreasingLength(array: List<IntArray>): Int {
42-
val list: MutableList<Int?> = ArrayList<Int?>()
42+
val list: MutableList<Int> = ArrayList<Int>()
4343
for (pair in array) {
4444
val m = list.size
45-
if (m == 0 || list[m - 1]!! < pair[1]) {
45+
if (m == 0 || list[m - 1] < pair[1]) {
4646
list.add(pair[1])
4747
} else {
4848
val idx = binarySearch(list, pair[1])
@@ -52,15 +52,15 @@ class Solution {
5252
return list.size
5353
}
5454

55-
private fun binarySearch(list: List<Int?>, target: Int): Int {
55+
private fun binarySearch(list: List<Int>, target: Int): Int {
5656
val n = list.size
5757
var left = 0
5858
var right = n - 1
5959
while (left < right) {
6060
val mid = (left + right) / 2
6161
if (list[mid] == target) {
6262
return mid
63-
} else if (list[mid]!! > target) {
63+
} else if (list[mid] > target) {
6464
right = mid
6565
} else {
6666
left = mid + 1

Diff for: src/main/kotlin/g3301_3400/s3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii/Solution.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ class Solution {
1717
private fun countOfSubstringHavingAtleastXConsonants(word: String, k: Int): Long {
1818
var start = 0
1919
var end = 0
20-
val vowels: MutableSet<Char?> = HashSet<Char?>()
20+
val vowels: MutableSet<Char> = HashSet<Char>()
2121
vowels.add('a')
2222
vowels.add('e')
2323
vowels.add('i')
2424
vowels.add('o')
2525
vowels.add('u')
2626
var consonants = 0
27-
val map: MutableMap<Char?, Int?> = HashMap<Char?, Int?>()
27+
val map: MutableMap<Char, Int> = HashMap<Char, Int>()
2828
var res: Long = 0
2929
while (end < word.length) {
3030
val ch = word[end]

Diff for: src/main/kotlin/g3301_3400/s3311_construct_2d_grid_matching_graph_layout/Solution.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class Solution {
6666
): Array<IntArray> {
6767
var st = st
6868
var res: Array<IntArray>
69-
val al = ArrayList<Int?>()
69+
val al = ArrayList<Int>()
7070
var f = true
7171
seen[st] = true
7272
al.add(st)
@@ -86,7 +86,7 @@ class Solution {
8686
}
8787
res = Array<IntArray>(n / al.size) { IntArray(al.size) }
8888
for (i in res[0].indices) {
89-
res[0][i] = al[i]!!
89+
res[0][i] = al[i]
9090
}
9191
for (i in 1 until res.size) {
9292
for (j in res[0].indices) {

Diff for: src/main/kotlin/g3301_3400/s3318_find_x_sum_of_all_k_long_subarrays_i/Solution.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Solution {
2222
val n = nums.size
2323
val ans = IntArray(n - k + 1)
2424
for (i in 0 until n - k + 1) {
25-
val map = HashMap<Int?, Int?>()
25+
val map = HashMap<Int, Int>()
2626
val pq =
2727
PriorityQueue<Pair>(
2828
Comparator { a: Pair, b: Pair ->
@@ -33,10 +33,10 @@ class Solution {
3333
},
3434
)
3535
for (j in i until i + k) {
36-
map.put(nums[j], map.getOrDefault(nums[j], 0)!! + 1)
36+
map.put(nums[j], map.getOrDefault(nums[j], 0) + 1)
3737
}
3838
for (entry in map.entries) {
39-
pq.add(Pair(entry.key!!, entry.value!!))
39+
pq.add(Pair(entry.key, entry.value))
4040
}
4141
var count = x
4242
var sum = 0

Diff for: src/main/kotlin/g3301_3400/s3319_k_th_largest_perfect_subtree_size_in_binary_tree/Solution.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import java.util.Queue
1818
* }
1919
*/
2020
class Solution {
21-
private val pq: Queue<Int?> = PriorityQueue<Int?>()
21+
private val pq: Queue<Int> = PriorityQueue<Int>()
2222

2323
fun kthLargestPerfectSubtree(root: TreeNode?, k: Int): Int {
2424
dfs(root, k)

0 commit comments

Comments
 (0)