diff --git a/src/main/kotlin/g2401_2500/s2471_minimum_number_of_operations_to_sort_a_binary_tree_by_level/Solution.kt b/src/main/kotlin/g2401_2500/s2471_minimum_number_of_operations_to_sort_a_binary_tree_by_level/Solution.kt index 2b87fd26b..29e8ea783 100644 --- a/src/main/kotlin/g2401_2500/s2471_minimum_number_of_operations_to_sort_a_binary_tree_by_level/Solution.kt +++ b/src/main/kotlin/g2401_2500/s2471_minimum_number_of_operations_to_sort_a_binary_tree_by_level/Solution.kt @@ -18,7 +18,7 @@ import java.util.ArrayDeque */ class Solution { fun minimumOperations(root: TreeNode): Int { - val q = ArrayDeque() + val q = ArrayDeque() var count = 0 if (root.left != null && root.right != null && root.left!!.`val` > root.right!!.`val`) { count++ @@ -55,14 +55,14 @@ class Solution { sorted[i] = list[i] } sorted.sort() - val ind: MutableMap = HashMap() + val ind: MutableMap = HashMap() for (i in list.indices) { ind[list[i]] = i } for (i in list.indices) { if (list[i] != sorted[i]) { swaps++ - ind[list[i]] = ind[sorted[i]] + ind[list[i]] = ind[sorted[i]]!! list[ind[sorted[i]]!!] = list[i] } } diff --git a/src/main/kotlin/g2601_2700/s2608_shortest_cycle_in_a_graph/Solution.kt b/src/main/kotlin/g2601_2700/s2608_shortest_cycle_in_a_graph/Solution.kt index 9869a9d0b..efec2da35 100644 --- a/src/main/kotlin/g2601_2700/s2608_shortest_cycle_in_a_graph/Solution.kt +++ b/src/main/kotlin/g2601_2700/s2608_shortest_cycle_in_a_graph/Solution.kt @@ -15,12 +15,12 @@ class Solution { adj[edge[1]].add(edge[0]) } for (i in 0 until n) { - dfs(adj, HashSet(), i) + dfs(adj, HashSet(), i) } return if (min == Int.MAX_VALUE) -1 else min } - private fun dfs(adj: List>, set: HashSet, node: Int) { + private fun dfs(adj: List>, set: HashSet, node: Int) { val queue: Queue = LinkedList() set.add(node) queue.add(intArrayOf(node, node)) diff --git a/src/main/kotlin/g3101_3200/s3165_maximum_sum_of_subsequence_with_non_adjacent_elements/Solution.kt b/src/main/kotlin/g3101_3200/s3165_maximum_sum_of_subsequence_with_non_adjacent_elements/Solution.kt index 0a91b87e4..10b2cb188 100644 --- a/src/main/kotlin/g3101_3200/s3165_maximum_sum_of_subsequence_with_non_adjacent_elements/Solution.kt +++ b/src/main/kotlin/g3101_3200/s3165_maximum_sum_of_subsequence_with_non_adjacent_elements/Solution.kt @@ -7,7 +7,7 @@ import kotlin.math.max class Solution { fun maximumSumSubsequence(nums: IntArray, queries: Array): Int { - val tree: Array = build(nums) + val tree: Array = build(nums) var result: Long = 0 for (i in queries.indices) { result += set(tree, queries[i][0], queries[i][1]) @@ -23,50 +23,50 @@ class Solution { private const val NN = 3 private const val MOD = 1000000007 - private fun build(nums: IntArray): Array { + private fun build(nums: IntArray): Array { val len = nums.size var size = 1 while (size < len) { size = size shl 1 } - val tree = Array(size * 2) { LongArray(4) } + val tree = Array(size * 2) { LongArray(4) } for (i in 0 until len) { - tree[size + i]!![YY] = nums[i].toLong() + tree[size + i][YY] = nums[i].toLong() } for (i in size - 1 downTo 1) { - tree[i]!![YY] = max( - (tree[2 * i]!![YY] + tree[2 * i + 1]!![NY]), + tree[i][YY] = max( + (tree[2 * i][YY] + tree[2 * i + 1][NY]), ( - tree[2 * i]!![YN] + max( - tree[2 * i + 1]!![YY], - tree[2 * i + 1]!![NY], + tree[2 * i][YN] + max( + tree[2 * i + 1][YY], + tree[2 * i + 1][NY], ) ), ) - tree[i]!![YN] = max( - (tree[2 * i]!![YY] + tree[2 * i + 1]!![NN]), + tree[i][YN] = max( + (tree[2 * i][YY] + tree[2 * i + 1][NN]), ( - tree[2 * i]!![YN] + max( - tree[2 * i + 1]!![YN], - tree[2 * i + 1]!![NN], + tree[2 * i][YN] + max( + tree[2 * i + 1][YN], + tree[2 * i + 1][NN], ) ), ) - tree[i]!![NY] = max( - (tree[2 * i]!![NY] + tree[2 * i + 1]!![NY]), + tree[i][NY] = max( + (tree[2 * i][NY] + tree[2 * i + 1][NY]), ( - tree[2 * i]!![NN] + max( - tree[2 * i + 1]!![YY], - tree[2 * i + 1]!![NY], + tree[2 * i][NN] + max( + tree[2 * i + 1][YY], + tree[2 * i + 1][NY], ) ), ) - tree[i]!![NN] = max( - (tree[2 * i]!![NY] + tree[2 * i + 1]!![NN]), + tree[i][NN] = max( + (tree[2 * i][NY] + tree[2 * i + 1][NN]), ( - tree[2 * i]!![NN] + max( - tree[2 * i + 1]!![YN], - tree[2 * i + 1]!![NN], + tree[2 * i][NN] + max( + tree[2 * i + 1][YN], + tree[2 * i + 1][NN], ) ), ) @@ -74,52 +74,52 @@ class Solution { return tree } - private fun set(tree: Array, idx: Int, `val`: Int): Long { + private fun set(tree: Array, idx: Int, `val`: Int): Long { val size = tree.size / 2 - tree[size + idx]!![YY] = `val`.toLong() + tree[size + idx][YY] = `val`.toLong() var i = (size + idx) / 2 while (i > 0) { - tree[i]!![YY] = max( - (tree[2 * i]!![YY] + tree[2 * i + 1]!![NY]), + tree[i][YY] = max( + (tree[2 * i][YY] + tree[2 * i + 1][NY]), ( - tree[2 * i]!![YN] + max( - tree[2 * i + 1]!![YY], - tree[2 * i + 1]!![NY], + tree[2 * i][YN] + max( + tree[2 * i + 1][YY], + tree[2 * i + 1][NY], ) ), ) - tree[i]!![YN] = max( - (tree[2 * i]!![YY] + tree[2 * i + 1]!![NN]), + tree[i][YN] = max( + (tree[2 * i][YY] + tree[2 * i + 1][NN]), ( - tree[2 * i]!![YN] + max( - tree[2 * i + 1]!![YN], - tree[2 * i + 1]!![NN], + tree[2 * i][YN] + max( + tree[2 * i + 1][YN], + tree[2 * i + 1][NN], ) ), ) - tree[i]!![NY] = max( - (tree[2 * i]!![NY] + tree[2 * i + 1]!![NY]), + tree[i][NY] = max( + (tree[2 * i][NY] + tree[2 * i + 1][NY]), ( - tree[2 * i]!![NN] + max( - tree[2 * i + 1]!![YY], - tree[2 * i + 1]!![NY], + tree[2 * i][NN] + max( + tree[2 * i + 1][YY], + tree[2 * i + 1][NY], ) ), ) - tree[i]!![NN] = max( - (tree[2 * i]!![NY] + tree[2 * i + 1]!![NN]), + tree[i][NN] = max( + (tree[2 * i][NY] + tree[2 * i + 1][NN]), ( - tree[2 * i]!![NN] + max( - tree[2 * i + 1]!![YN], - tree[2 * i + 1]!![NN], + tree[2 * i][NN] + max( + tree[2 * i + 1][YN], + tree[2 * i + 1][NN], ) ), ) i /= 2 } return max( - tree[1]!![YY], - max(tree[1]!![YN], max(tree[1]!![NY], tree[1]!![NN])), + tree[1][YY], + max(tree[1][YN], max(tree[1][NY], tree[1][NN])), ) } } diff --git a/src/main/kotlin/g3201_3300/s3245_alternating_groups_iii/Solution.kt b/src/main/kotlin/g3201_3300/s3245_alternating_groups_iii/Solution.kt index cf9ea0c04..7546988e5 100644 --- a/src/main/kotlin/g3201_3300/s3245_alternating_groups_iii/Solution.kt +++ b/src/main/kotlin/g3201_3300/s3245_alternating_groups_iii/Solution.kt @@ -74,7 +74,7 @@ class Solution { } // Initializes the alternating groups using the colors array. - private fun initializeGroups(colors: IntArray, groups: TreeMap) { + private fun initializeGroups(colors: IntArray, groups: TreeMap) { val n = colors.size var i = 0 while (i < n) { @@ -95,7 +95,7 @@ class Solution { // Processes a type 1 query: returns the number of alternating groups // of at least the given size. - private fun processQueryType1(colors: IntArray, groups: TreeMap, groupSize: Int): Int { + private fun processQueryType1(colors: IntArray, groups: TreeMap, groupSize: Int): Int { var ans = qry(groupSize) val firstGroup = groups.firstEntry() val lastGroup = groups.lastEntry() @@ -114,7 +114,7 @@ class Solution { // Processes a type 2 query: updates the color at index x and adjusts groups. private fun processQueryType2( colors: IntArray, - groups: TreeMap, + groups: TreeMap, x: Int, newColor: Int, ) { @@ -175,10 +175,10 @@ class Solution { } // Main function to handle queries on alternating groups. - fun numberOfAlternatingGroups(colors: IntArray, queries: Array): MutableList { - val groups = TreeMap() + fun numberOfAlternatingGroups(colors: IntArray, queries: Array): MutableList { + val groups = TreeMap() val n = colors.size - val results: MutableList = ArrayList() + val results: MutableList = ArrayList() // Initialize alternating groups. initializeGroups(colors, groups) // Process each query. diff --git a/src/main/kotlin/g3201_3300/s3286_find_a_safe_walk_through_a_grid/Solution.kt b/src/main/kotlin/g3201_3300/s3286_find_a_safe_walk_through_a_grid/Solution.kt index 56f109835..27ed45cdf 100644 --- a/src/main/kotlin/g3201_3300/s3286_find_a_safe_walk_through_a_grid/Solution.kt +++ b/src/main/kotlin/g3201_3300/s3286_find_a_safe_walk_through_a_grid/Solution.kt @@ -13,10 +13,10 @@ class Solution { val m = grid[0].size val dr = intArrayOf(0, 0, 1, -1) val dc = intArrayOf(1, -1, 0, 0) - val visited = Array?>(n) { Array(m) { BooleanArray(health + 1) } } + val visited = Array>(n) { Array(m) { BooleanArray(health + 1) } } val bfs: Queue = LinkedList() bfs.add(intArrayOf(0, 0, health - grid[0][0])) - visited[0]!![0][health - grid[0][0]] = true + visited[0][0][health - grid[0][0]] = true while (bfs.isNotEmpty()) { var size = bfs.size while (size-- > 0) { @@ -32,8 +32,8 @@ class Solution { val nc = c + dc[k] if (isValidMove(nr, nc, n, m)) { val nh: Int = h - grid[nr][nc] - if (nh >= 0 && !visited[nr]!![nc][nh]) { - visited[nr]!![nc][nh] = true + if (nh >= 0 && !visited[nr][nc][nh]) { + visited[nr][nc][nh] = true bfs.add(intArrayOf(nr, nc, nh)) } } diff --git a/src/main/kotlin/g3201_3300/s3288_length_of_the_longest_increasing_path/Solution.kt b/src/main/kotlin/g3201_3300/s3288_length_of_the_longest_increasing_path/Solution.kt index 7107b5e7e..bf3104cc2 100644 --- a/src/main/kotlin/g3201_3300/s3288_length_of_the_longest_increasing_path/Solution.kt +++ b/src/main/kotlin/g3201_3300/s3288_length_of_the_longest_increasing_path/Solution.kt @@ -39,10 +39,10 @@ class Solution { } private fun longestIncreasingLength(array: List): Int { - val list: MutableList = ArrayList() + val list: MutableList = ArrayList() for (pair in array) { val m = list.size - if (m == 0 || list[m - 1]!! < pair[1]) { + if (m == 0 || list[m - 1] < pair[1]) { list.add(pair[1]) } else { val idx = binarySearch(list, pair[1]) @@ -52,7 +52,7 @@ class Solution { return list.size } - private fun binarySearch(list: List, target: Int): Int { + private fun binarySearch(list: List, target: Int): Int { val n = list.size var left = 0 var right = n - 1 @@ -60,7 +60,7 @@ class Solution { val mid = (left + right) / 2 if (list[mid] == target) { return mid - } else if (list[mid]!! > target) { + } else if (list[mid] > target) { right = mid } else { left = mid + 1 diff --git a/src/main/kotlin/g3301_3400/s3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii/Solution.kt b/src/main/kotlin/g3301_3400/s3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii/Solution.kt index 03403cebb..46d8e44af 100644 --- a/src/main/kotlin/g3301_3400/s3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii/Solution.kt @@ -17,14 +17,14 @@ class Solution { private fun countOfSubstringHavingAtleastXConsonants(word: String, k: Int): Long { var start = 0 var end = 0 - val vowels: MutableSet = HashSet() + val vowels: MutableSet = HashSet() vowels.add('a') vowels.add('e') vowels.add('i') vowels.add('o') vowels.add('u') var consonants = 0 - val map: MutableMap = HashMap() + val map: MutableMap = HashMap() var res: Long = 0 while (end < word.length) { val ch = word[end] diff --git a/src/main/kotlin/g3301_3400/s3311_construct_2d_grid_matching_graph_layout/Solution.kt b/src/main/kotlin/g3301_3400/s3311_construct_2d_grid_matching_graph_layout/Solution.kt index 4d5637c01..dc1100722 100644 --- a/src/main/kotlin/g3301_3400/s3311_construct_2d_grid_matching_graph_layout/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3311_construct_2d_grid_matching_graph_layout/Solution.kt @@ -66,7 +66,7 @@ class Solution { ): Array { var st = st var res: Array - val al = ArrayList() + val al = ArrayList() var f = true seen[st] = true al.add(st) @@ -86,7 +86,7 @@ class Solution { } res = Array(n / al.size) { IntArray(al.size) } for (i in res[0].indices) { - res[0][i] = al[i]!! + res[0][i] = al[i] } for (i in 1 until res.size) { for (j in res[0].indices) { diff --git a/src/main/kotlin/g3301_3400/s3318_find_x_sum_of_all_k_long_subarrays_i/Solution.kt b/src/main/kotlin/g3301_3400/s3318_find_x_sum_of_all_k_long_subarrays_i/Solution.kt index 886a832c3..6de78e62a 100644 --- a/src/main/kotlin/g3301_3400/s3318_find_x_sum_of_all_k_long_subarrays_i/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3318_find_x_sum_of_all_k_long_subarrays_i/Solution.kt @@ -22,7 +22,7 @@ class Solution { val n = nums.size val ans = IntArray(n - k + 1) for (i in 0 until n - k + 1) { - val map = HashMap() + val map = HashMap() val pq = PriorityQueue( Comparator { a: Pair, b: Pair -> @@ -33,10 +33,10 @@ class Solution { }, ) for (j in i until i + k) { - map.put(nums[j], map.getOrDefault(nums[j], 0)!! + 1) + map.put(nums[j], map.getOrDefault(nums[j], 0) + 1) } for (entry in map.entries) { - pq.add(Pair(entry.key!!, entry.value!!)) + pq.add(Pair(entry.key, entry.value)) } var count = x var sum = 0 diff --git a/src/main/kotlin/g3301_3400/s3319_k_th_largest_perfect_subtree_size_in_binary_tree/Solution.kt b/src/main/kotlin/g3301_3400/s3319_k_th_largest_perfect_subtree_size_in_binary_tree/Solution.kt index 8e327a68b..cf1da72b6 100644 --- a/src/main/kotlin/g3301_3400/s3319_k_th_largest_perfect_subtree_size_in_binary_tree/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3319_k_th_largest_perfect_subtree_size_in_binary_tree/Solution.kt @@ -18,7 +18,7 @@ import java.util.Queue * } */ class Solution { - private val pq: Queue = PriorityQueue() + private val pq: Queue = PriorityQueue() fun kthLargestPerfectSubtree(root: TreeNode?, k: Int): Int { dfs(root, k) diff --git a/src/main/kotlin/g3301_3400/s3320_count_the_number_of_winning_sequences/Solution.kt b/src/main/kotlin/g3301_3400/s3320_count_the_number_of_winning_sequences/Solution.kt index 771a418ad..67315fcf1 100644 --- a/src/main/kotlin/g3301_3400/s3320_count_the_number_of_winning_sequences/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3320_count_the_number_of_winning_sequences/Solution.kt @@ -1,62 +1,62 @@ package g3301_3400.s3320_count_the_number_of_winning_sequences -// #Hard #String #Dynamic_Programming #2024_10_15_Time_335_ms_(100.00%)_Space_78.8_MB_(66.67%) +// #Hard #String #Dynamic_Programming #2025_03_14_Time_56_ms_(100.00%)_Space_79.51_MB_(100.00%) class Solution { fun countWinningSequences(s: String): Int { val n = s.length - val dp = Array?>(n) { Array(3) { IntArray(2 * n + 1) } } + val dp = Array>(n) { Array(3) { IntArray(2 * n + 1) } } if (s[0] == 'F') { - dp[0]!![0]!![n] = 1 - dp[0]!![1]!![1 + n] = 1 - dp[0]!![2]!![-1 + n] = 1 + dp[0][0][n] = 1 + dp[0][1][1 + n] = 1 + dp[0][2][-1 + n] = 1 } else if (s[0] == 'W') { - dp[0]!![0]!![-1 + n] = 1 - dp[0]!![1]!![n] = 1 - dp[0]!![2]!![1 + n] = 1 + dp[0][0][-1 + n] = 1 + dp[0][1][n] = 1 + dp[0][2][1 + n] = 1 } else if (s[0] == 'E') { - dp[0]!![0]!![1 + n] = 1 - dp[0]!![1]!![-1 + n] = 1 - dp[0]!![2]!![n] = 1 + dp[0][0][1 + n] = 1 + dp[0][1][-1 + n] = 1 + dp[0][2][n] = 1 } for (i in 1 until n) { if (s[i] == 'F') { for (j in 0 until 2 * n + 1) { - dp[i]!![0]!![j] = (dp[i - 1]!![1]!![j] + dp[i - 1]!![2]!![j]) % MOD + dp[i][0][j] = (dp[i - 1][1][j] + dp[i - 1][2][j]) % MOD } for (j in 1 until 2 * n + 1) { - dp[i]!![1]!![j] = (dp[i - 1]!![0]!![j - 1] + dp[i - 1]!![2]!![j - 1]) % MOD + dp[i][1][j] = (dp[i - 1][0][j - 1] + dp[i - 1][2][j - 1]) % MOD } for (j in 0 until 2 * n) { - dp[i]!![2]!![j] = (dp[i - 1]!![0]!![j + 1] + dp[i - 1]!![1]!![j + 1]) % MOD + dp[i][2][j] = (dp[i - 1][0][j + 1] + dp[i - 1][1][j + 1]) % MOD } } else if (s[i] == 'W') { for (j in 0 until 2 * n + 1) { - dp[i]!![1]!![j] = (dp[i - 1]!![0]!![j] + dp[i - 1]!![2]!![j]) % MOD + dp[i][1][j] = (dp[i - 1][0][j] + dp[i - 1][2][j]) % MOD } for (j in 1 until 2 * n + 1) { - dp[i]!![2]!![j] = (dp[i - 1]!![0]!![j - 1] + dp[i - 1]!![1]!![j - 1]) % MOD + dp[i][2][j] = (dp[i - 1][0][j - 1] + dp[i - 1][1][j - 1]) % MOD } for (j in 0 until 2 * n) { - dp[i]!![0]!![j] = (dp[i - 1]!![1]!![j + 1] + dp[i - 1]!![2]!![j + 1]) % MOD + dp[i][0][j] = (dp[i - 1][1][j + 1] + dp[i - 1][2][j + 1]) % MOD } } else if (s[i] == 'E') { for (j in 0 until 2 * n) { - dp[i]!![2]!![j] = (dp[i - 1]!![0]!![j] + dp[i - 1]!![1]!![j]) % MOD + dp[i][2][j] = (dp[i - 1][0][j] + dp[i - 1][1][j]) % MOD } for (j in 1 until 2 * n + 1) { - dp[i]!![0]!![j] = (dp[i - 1]!![1]!![j - 1] + dp[i - 1]!![2]!![j - 1]) % MOD + dp[i][0][j] = (dp[i - 1][1][j - 1] + dp[i - 1][2][j - 1]) % MOD } for (j in 0 until 2 * n) { - dp[i]!![1]!![j] = (dp[i - 1]!![0]!![j + 1] + dp[i - 1]!![2]!![j + 1]) % MOD + dp[i][1][j] = (dp[i - 1][0][j + 1] + dp[i - 1][2][j + 1]) % MOD } } } var count = 0 for (j in n + 1 until 2 * n + 1) { - count = (count + dp[n - 1]!![0]!![j]) % MOD - count = (count + dp[n - 1]!![1]!![j]) % MOD - count = (count + dp[n - 1]!![2]!![j]) % MOD + count = (count + dp[n - 1][0][j]) % MOD + count = (count + dp[n - 1][1][j]) % MOD + count = (count + dp[n - 1][2][j]) % MOD } return count % MOD } diff --git a/src/main/kotlin/g3301_3400/s3321_find_x_sum_of_all_k_long_subarrays_ii/Solution.kt b/src/main/kotlin/g3301_3400/s3321_find_x_sum_of_all_k_long_subarrays_ii/Solution.kt index f3ed9a24e..3e7687ec0 100644 --- a/src/main/kotlin/g3301_3400/s3321_find_x_sum_of_all_k_long_subarrays_ii/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3321_find_x_sum_of_all_k_long_subarrays_ii/Solution.kt @@ -27,14 +27,14 @@ class Solution { fun findXSum(nums: IntArray, k: Int, x: Int): LongArray { val n = nums.size val ans = LongArray(n - k + 1) - val cnt: MutableMap = HashMap() + val cnt: MutableMap = HashMap() val s1 = TreeSet() val s2 = TreeSet() var sum: Long = 0 var xSum: Long = 0 for (i in 0 until n) { sum += nums[i].toLong() - var curCnt: Int = cnt.getOrDefault(nums[i], 0)!! + var curCnt: Int = cnt.getOrDefault(nums[i], 0) cnt.put(nums[i], curCnt + 1) var tmp = RC(nums[i], curCnt) if (s1.contains(tmp)) { diff --git a/src/main/kotlin/g3301_3400/s3327_check_if_dfs_strings_are_palindromes/Solution.kt b/src/main/kotlin/g3301_3400/s3327_check_if_dfs_strings_are_palindromes/Solution.kt index 6ba7afdb4..ed5c630ae 100644 --- a/src/main/kotlin/g3301_3400/s3327_check_if_dfs_strings_are_palindromes/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3327_check_if_dfs_strings_are_palindromes/Solution.kt @@ -6,7 +6,7 @@ package g3301_3400.s3327_check_if_dfs_strings_are_palindromes import kotlin.math.min class Solution { - private val e: MutableList?> = ArrayList?>() + private val e: MutableList> = ArrayList>() private val stringBuilder = StringBuilder() private var s: String? = null private var now = 0 @@ -18,8 +18,8 @@ class Solution { private fun dfs(x: Int) { l[x] = now + 1 - for (v in e[x]!!) { - dfs(v!!) + for (v in e[x]) { + dfs(v) } stringBuilder.append(s!![x]) r[x] = ++now @@ -54,10 +54,10 @@ class Solution { n = parent.size this.s = s for (i in 0 until n) { - e.add(ArrayList()) + e.add(ArrayList()) } for (i in 1 until n) { - e[parent[i]]!!.add(i) + e[parent[i]].add(i) } l = IntArray(n) r = IntArray(n) diff --git a/src/main/kotlin/g3301_3400/s3333_find_the_original_typed_string_ii/Solution.kt b/src/main/kotlin/g3301_3400/s3333_find_the_original_typed_string_ii/Solution.kt index 3244971f5..1d0c01fa6 100644 --- a/src/main/kotlin/g3301_3400/s3333_find_the_original_typed_string_ii/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3333_find_the_original_typed_string_ii/Solution.kt @@ -27,29 +27,29 @@ class Solution { if (m >= k) { return power[0].toInt() } - val dp = Array(m) { LongArray(k - m + 1) } + val dp = Array(m) { LongArray(k - m + 1) } i = 0 while (i < k - m + 1) { if (list[m - 1] + i + m > k) { - dp[m - 1]!![i] = list[m - 1] - (k - m - i).toLong() + dp[m - 1][i] = list[m - 1] - (k - m - i).toLong() } i++ } i = m - 2 while (i >= 0) { - var sum: Long = dp[i + 1]!![k - m] * list[i] % MOD + var sum: Long = dp[i + 1][k - m] * list[i] % MOD for (j in k - m downTo 0) { - sum += dp[i + 1]!![j] + sum += dp[i + 1][j] if (j + list[i] > k - m) { - sum = (sum - dp[i + 1]!![k - m] + MOD) % MOD + sum = (sum - dp[i + 1][k - m] + MOD) % MOD } else { - sum = (sum - dp[i + 1]!![j + list[i]] + MOD) % MOD + sum = (sum - dp[i + 1][j + list[i]] + MOD) % MOD } - dp[i]!![j] = sum + dp[i][j] = sum } i-- } - return dp[0]!![0].toInt() + return dp[0][0].toInt() } companion object { diff --git a/src/main/kotlin/g3301_3400/s3334_find_the_maximum_factor_score_of_array/Solution.kt b/src/main/kotlin/g3301_3400/s3334_find_the_maximum_factor_score_of_array/Solution.kt index de5c471dc..0628b1036 100644 --- a/src/main/kotlin/g3301_3400/s3334_find_the_maximum_factor_score_of_array/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3334_find_the_maximum_factor_score_of_array/Solution.kt @@ -10,36 +10,36 @@ class Solution { if (n == 1) { return nums[0].toLong() * nums[0] } - val lToR = Array(n) { LongArray(2) } - val rToL = Array(n) { LongArray(2) } + val lToR = Array(n) { LongArray(2) } + val rToL = Array(n) { LongArray(2) } for (i in 0 until n) { if (i == 0) { - lToR[i]!![1] = nums[i].toLong() - lToR[i]!![0] = lToR[i]!![1] - rToL[n - i - 1]!![1] = nums[n - i - 1].toLong() - rToL[n - i - 1]!![0] = rToL[n - i - 1]!![1] + lToR[i][1] = nums[i].toLong() + lToR[i][0] = lToR[i][1] + rToL[n - i - 1][1] = nums[n - i - 1].toLong() + rToL[n - i - 1][0] = rToL[n - i - 1][1] } else { - rToL[n - i - 1]!![0] = gcd(nums[n - i - 1].toLong(), rToL[n - i]!![0]) - lToR[i]!![0] = gcd(nums[i].toLong(), lToR[i - 1]!![0]) + rToL[n - i - 1][0] = gcd(nums[n - i - 1].toLong(), rToL[n - i][0]) + lToR[i][0] = gcd(nums[i].toLong(), lToR[i - 1][0]) - rToL[n - i - 1]!![1] = lcm(nums[n - i - 1].toLong(), rToL[n - i]!![1]) - lToR[i]!![1] = lcm(nums[i].toLong(), lToR[i - 1]!![1]) + rToL[n - i - 1][1] = lcm(nums[n - i - 1].toLong(), rToL[n - i][1]) + lToR[i][1] = lcm(nums[i].toLong(), lToR[i - 1][1]) } } var max: Long = 0 for (i in 0 until n) { - val gcd = if (i == 0) rToL[i + 1]!![0] else getLong(i, n, lToR, rToL) - max = max(max, (gcd * (if (i == 0) rToL[i + 1]!![1] else getaLong(i, n, lToR, rToL)))) + val gcd = if (i == 0) rToL[i + 1][0] else getLong(i, n, lToR, rToL) + max = max(max, (gcd * (if (i == 0) rToL[i + 1][1] else getaLong(i, n, lToR, rToL)))) } - return max(max, (rToL[0]!![0] * rToL[0]!![1])) + return max(max, (rToL[0][0] * rToL[0][1])) } - private fun getaLong(i: Int, n: Int, lToR: Array, rToL: Array): Long { - return if (i == n - 1) lToR[i - 1]!![1] else lcm(rToL[i + 1]!![1], lToR[i - 1]!![1]) + private fun getaLong(i: Int, n: Int, lToR: Array, rToL: Array): Long { + return if (i == n - 1) lToR[i - 1][1] else lcm(rToL[i + 1][1], lToR[i - 1][1]) } - private fun getLong(i: Int, n: Int, lToR: Array, rToL: Array): Long { - return if (i == n - 1) lToR[i - 1]!![0] else gcd(rToL[i + 1]!![0], lToR[i - 1]!![0]) + private fun getLong(i: Int, n: Int, lToR: Array, rToL: Array): Long { + return if (i == n - 1) lToR[i - 1][0] else gcd(rToL[i + 1][0], lToR[i - 1][0]) } private fun gcd(a: Long, b: Long): Long { diff --git a/src/main/kotlin/g3301_3400/s3335_total_characters_in_string_after_transformations_i/Solution.kt b/src/main/kotlin/g3301_3400/s3335_total_characters_in_string_after_transformations_i/Solution.kt index 61bfb7d86..ef8dc6ddd 100644 --- a/src/main/kotlin/g3301_3400/s3335_total_characters_in_string_after_transformations_i/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3335_total_characters_in_string_after_transformations_i/Solution.kt @@ -11,13 +11,13 @@ class Solution { for (c in s.toCharArray()) { count[c.code - 'a'.code]++ } - val list = LinkedList() + val list = LinkedList() for (c in count) { list.add(c) } var delta = s.length % 1000000007 for (i in 0 until t) { - val zCount = list.removeLast()!! % 1000000007 + val zCount = list.removeLast() % 1000000007 val aCount = list.pollFirst()!! % 1000000007 list.offerFirst((aCount + zCount) % 1000000007) list.offerFirst(zCount) diff --git a/src/main/kotlin/g3301_3400/s3342_find_minimum_time_to_reach_last_room_ii/Solution.kt b/src/main/kotlin/g3301_3400/s3342_find_minimum_time_to_reach_last_room_ii/Solution.kt index 6923936f1..ba0397e58 100644 --- a/src/main/kotlin/g3301_3400/s3342_find_minimum_time_to_reach_last_room_ii/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3342_find_minimum_time_to_reach_last_room_ii/Solution.kt @@ -15,7 +15,7 @@ class Solution { var turn: Int = 0 } - private val dir = arrayOf(intArrayOf(1, 0), intArrayOf(-1, 0), intArrayOf(0, 1), intArrayOf(0, -1)) + private val dir = arrayOf(intArrayOf(1, 0), intArrayOf(-1, 0), intArrayOf(0, 1), intArrayOf(0, -1)) fun minTimeToReach(moveTime: Array): Int { val pq = PriorityQueue(Comparator { a: Node, b: Node -> a.t - b.t }) @@ -32,8 +32,8 @@ class Solution { while (pq.isNotEmpty()) { val curr = pq.poll() for (i in 0..3) { - val x = curr.x + dir[i]!![0] - val y = curr.y + dir[i]!![1] + val x = curr.x + dir[i][0] + val y = curr.y + dir[i][1] if (x == m - 1 && y == n - 1) { t = max(curr.t, moveTime[x][y]) + 1 + curr.turn return t diff --git a/src/main/kotlin/g3301_3400/s3343_count_number_of_balanced_permutations/Solution.kt b/src/main/kotlin/g3301_3400/s3343_count_number_of_balanced_permutations/Solution.kt index 82bbb2131..7ca7dc214 100644 --- a/src/main/kotlin/g3301_3400/s3343_count_number_of_balanced_permutations/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3343_count_number_of_balanced_permutations/Solution.kt @@ -27,22 +27,22 @@ class Solution { for (i in l - 1 downTo 0) { invF[i] = invF[i + 1] * (i + 1) % M } - val dp = Array(m + 1) { LongArray(hs + 1) } - dp[0]!![0] = 1 + val dp = Array(m + 1) { LongArray(hs + 1) } + dp[0][0] = 1 for (d in 0..9) { if (c[d] == 0) { continue } for (k in m downTo 0) { for (s in hs downTo 0) { - if (dp[k]!![s] == 0L) { + if (dp[k][s] == 0L) { continue } var t = 1 while (t <= c[d] && k + t <= m && s + d * t <= hs) { - dp[k + t]!![s + d * t] = + dp[k + t][s + d * t] = ( - dp[k + t]!![s + d * t] + dp[k]!![s] * comb( + dp[k + t][s + d * t] + dp[k][s] * comb( c[d], t, f, @@ -55,7 +55,7 @@ class Solution { } } } - val w = dp[m]!![hs] + val w = dp[m][hs] var r: Long = f[m] * f[l - m] % M for (d in 0..9) { r = r * invF[c[d]] % M diff --git a/src/main/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/Solution.kt b/src/main/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/Solution.kt index 171fc37ae..df7b66444 100644 --- a/src/main/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/Solution.kt @@ -6,13 +6,13 @@ import kotlin.math.min class Solution { fun shiftDistance(s: String, t: String, nextCost: IntArray, previousCost: IntArray): Long { - val costs = Array(26) { LongArray(26) } + val costs = Array(26) { LongArray(26) } var cost: Long for (i in 0..25) { cost = nextCost[i].toLong() var j = if (i == 25) 0 else i + 1 while (j != i) { - costs[i]!![j] = cost + costs[i][j] = cost cost += nextCost[j].toLong() if (j == 25) { j = -1 @@ -24,7 +24,7 @@ class Solution { cost = previousCost[i].toLong() var j = if (i == 0) 25 else i - 1 while (j != i) { - costs[i]!![j] = min(costs[i]!![j].toDouble(), cost.toDouble()).toLong() + costs[i][j] = min(costs[i][j], cost) cost += previousCost[j].toLong() if (j == 0) { j = 26 @@ -35,7 +35,7 @@ class Solution { val n = s.length var ans: Long = 0 for (i in 0..): Int { queries.sortWith { a: IntArray, b: IntArray -> a[0] - b[0] } - val last = PriorityQueue(Comparator { a: Int?, b: Int? -> b!! - a!! }) + val last = PriorityQueue(Comparator { a: Int, b: Int -> b - a }) val diffs = IntArray(nums.size + 1) var idx = 0 var cur = 0 diff --git a/src/main/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/Solution.kt b/src/main/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/Solution.kt index 7d30add83..d499b588a 100644 --- a/src/main/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/Solution.kt @@ -1,24 +1,24 @@ package g3301_3400.s3365_rearrange_k_substrings_to_form_target_string -// #Medium #String #Hash_Table #Sorting #2024_12_03_Time_457_ms_(100.00%)_Space_51.5_MB_(81.40%) +// #Medium #String #Hash_Table #Sorting #2025_03_14_Time_464_ms_(100.00%)_Space_55.32_MB_(100.00%) class Solution { fun isPossibleToRearrange(s: String, t: String, k: Int): Boolean { val size = s.length val div = size / k - val map: MutableMap = HashMap() + val map: MutableMap = HashMap() run { var i = 0 while (i < size) { val sub = s.substring(i, i + div) - map.put(sub, map.getOrDefault(sub, 0)!! + 1) + map.put(sub, map.getOrDefault(sub, 0) + 1) i += div } } var i = 0 while (i < size) { val sub = t.substring(i, i + div) - if (map.getOrDefault(sub, 0)!! > 0) { + if (map.getOrDefault(sub, 0) > 0) { map.put(sub, map[sub]!! - 1) } else { return false diff --git a/src/main/kotlin/g3301_3400/s3387_maximize_amount_after_two_days_of_conversions/Solution.kt b/src/main/kotlin/g3301_3400/s3387_maximize_amount_after_two_days_of_conversions/Solution.kt index 69d601531..aa36ee44f 100644 --- a/src/main/kotlin/g3301_3400/s3387_maximize_amount_after_two_days_of_conversions/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3387_maximize_amount_after_two_days_of_conversions/Solution.kt @@ -1,7 +1,7 @@ package g3301_3400.s3387_maximize_amount_after_two_days_of_conversions // #Medium #Array #String #Depth_First_Search #Breadth_First_Search #Graph -// #2024_12_18_Time_13_ms_(88.46%)_Space_47.8_MB_(30.77%) +// #2025_03_14_Time_10_ms_(93.75%)_Space_51.20_MB_(12.50%) import kotlin.math.max @@ -15,9 +15,9 @@ class Solution { private fun solve( currCurrency: String, value: Double, - targetCurrency: String?, + targetCurrency: String, day: Int, - used: MutableSet, + used: MutableSet, ) { if (currCurrency == targetCurrency) { res = max(res, value) @@ -37,7 +37,7 @@ class Solution { } } if (day == 1) { - solve(currCurrency, value, targetCurrency, day + 1, HashSet()) + solve(currCurrency, value, targetCurrency, day + 1, HashSet()) } } @@ -79,7 +79,7 @@ class Solution { map2[c2]!!.add(Pair(c1, 1.0 / rates2[i])) } res = 1.0 - solve(initialCurrency, 1.0, initialCurrency, 1, HashSet()) + solve(initialCurrency, 1.0, initialCurrency, 1, HashSet()) return res } } diff --git a/src/main/kotlin/g3401_3500/s3478_choose_k_elements_with_maximum_sum/Solution.kt b/src/main/kotlin/g3401_3500/s3478_choose_k_elements_with_maximum_sum/Solution.kt index 7580e3dcf..fdcb6783a 100644 --- a/src/main/kotlin/g3401_3500/s3478_choose_k_elements_with_maximum_sum/Solution.kt +++ b/src/main/kotlin/g3401_3500/s3478_choose_k_elements_with_maximum_sum/Solution.kt @@ -1,7 +1,7 @@ package g3401_3500.s3478_choose_k_elements_with_maximum_sum // #Medium #Array #Sorting #Heap_Priority_Queue -// #2025_03_11_Time_151_ms_(100.00%)_Space_93.64_MB_(40.74%) +// #2025_03_14_Time_171_ms_(95.45%)_Space_93.28_MB_(36.36%) import java.util.PriorityQueue @@ -11,7 +11,7 @@ class Solution { val ans = LongArray(n) val ps = Array(n) { i -> Point(nums1[i], nums2[i], i) } ps.sortWith { p1: Point, p2: Point -> p1.x.compareTo(p2.x) } - val pq = PriorityQueue() + val pq = PriorityQueue() var s: Long = 0 var i = 0 while (i < n) { diff --git a/src/test/kotlin/g3301_3400/s3320_count_the_number_of_winning_sequences/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3320_count_the_number_of_winning_sequences/SolutionTest.kt index 302a6bcaa..36f03769d 100644 --- a/src/test/kotlin/g3301_3400/s3320_count_the_number_of_winning_sequences/SolutionTest.kt +++ b/src/test/kotlin/g3301_3400/s3320_count_the_number_of_winning_sequences/SolutionTest.kt @@ -14,4 +14,14 @@ internal class SolutionTest { fun countWinningSequences2() { assertThat(Solution().countWinningSequences("FWEFW"), equalTo(18)) } + + @Test + fun countWinningSequences3() { + assertThat(Solution().countWinningSequences("W"), equalTo(1)) + } + + @Test + fun countWinningSequences4() { + assertThat(Solution().countWinningSequences("E"), equalTo(1)) + } }