From 37bc21b164ec82082238e64e1ae9cd69b19bce35 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sat, 3 May 2025 08:43:46 +0300 Subject: [PATCH 1/3] Improved tasks 194, 1022, 2366 --- .../g0101_0200/s0194_transpose_file/script.sh | 23 +++++++++----- .../Solution.kt | 31 ++++++------------- .../Solution.kt | 17 +++++----- 3 files changed, 34 insertions(+), 37 deletions(-) diff --git a/src/main/kotlin/g0101_0200/s0194_transpose_file/script.sh b/src/main/kotlin/g0101_0200/s0194_transpose_file/script.sh index 65f94e5a0..2b5f71645 100644 --- a/src/main/kotlin/g0101_0200/s0194_transpose_file/script.sh +++ b/src/main/kotlin/g0101_0200/s0194_transpose_file/script.sh @@ -1,8 +1,17 @@ # Read from the file file.txt and print its transposed content to stdout. -# #Medium #Shell #2022_11_25_Time_461_ms_(33.47%)_Space_3.9_MB_(34.89%) -wordcount=$(head -1 file.txt | wc -w) -col_n=1 -while [[ $col_n -le $wordcount ]]; do - awk "{ print \$$col_n }" file.txt | paste -sd " " - col_n=$((col_n + 1)) -done +# #Medium #Shell #2025_05_03_Time_61_ms_(88.19%)_Space_4.14_MB_(38.67%) +awk ' +{ + for (i = 1; i <= NF; i++) { + if (NR == 1) { + a[i] = $i + } else { + a[i] = a[i] " " $i + } + } +} +END { + for (i = 1; i <= NF; i++) { + print a[i] + } +}' file.txt diff --git a/src/main/kotlin/g1001_1100/s1022_sum_of_root_to_leaf_binary_numbers/Solution.kt b/src/main/kotlin/g1001_1100/s1022_sum_of_root_to_leaf_binary_numbers/Solution.kt index 91a1ff048..dc64b59ea 100644 --- a/src/main/kotlin/g1001_1100/s1022_sum_of_root_to_leaf_binary_numbers/Solution.kt +++ b/src/main/kotlin/g1001_1100/s1022_sum_of_root_to_leaf_binary_numbers/Solution.kt @@ -1,7 +1,7 @@ package g1001_1100.s1022_sum_of_root_to_leaf_binary_numbers // #Easy #Depth_First_Search #Tree #Binary_Tree -// #2023_05_22_Time_158_ms_(88.89%)_Space_36.3_MB_(11.11%) +// #2025_05_03_Time_0_ms_(100.00%)_Space_41.68_MB_(93.33%) import com_github_leetcode.TreeNode @@ -17,31 +17,18 @@ import com_github_leetcode.TreeNode */ class Solution { fun sumRootToLeaf(root: TreeNode?): Int { - val paths: MutableList> = ArrayList() - dfs(root, paths, ArrayList()) - var sum = 0 - for (list in paths) { - var num = 0 - for (i in list) { - num = (num shl 1) + i - } - sum += num - } - return sum + return sumRootToLeaf(root, 0) } - private fun dfs(root: TreeNode?, paths: MutableList>, path: MutableList) { - path.add(root!!.`val`) - if (root.left != null) { - dfs(root.left!!, paths, path) - path.removeAt(path.size - 1) - } - if (root.right != null) { - dfs(root.right!!, paths, path) - path.removeAt(path.size - 1) + private fun sumRootToLeaf(root: TreeNode?, sum: Int): Int { + var sum = sum + if (root == null) { + return 0 } + sum = 2 * sum + root.`val` if (root.left == null && root.right == null) { - paths.add(ArrayList(path)) + return sum } + return sumRootToLeaf(root.left, sum) + sumRootToLeaf(root.right, sum) } } diff --git a/src/main/kotlin/g2301_2400/s2366_minimum_replacements_to_sort_the_array/Solution.kt b/src/main/kotlin/g2301_2400/s2366_minimum_replacements_to_sort_the_array/Solution.kt index 8fdcc329b..bbec45fb7 100644 --- a/src/main/kotlin/g2301_2400/s2366_minimum_replacements_to_sort_the_array/Solution.kt +++ b/src/main/kotlin/g2301_2400/s2366_minimum_replacements_to_sort_the_array/Solution.kt @@ -1,18 +1,19 @@ package g2301_2400.s2366_minimum_replacements_to_sort_the_array -// #Hard #Array #Math #Greedy #2023_07_02_Time_433_ms_(100.00%)_Space_57.9_MB_(100.00%) +// #Hard #Array #Math #Greedy #2025_05_03_Time_4_ms_(100.00%)_Space_60.26_MB_(66.67%) class Solution { fun minimumReplacement(nums: IntArray): Long { - var limit = nums[nums.size - 1] + val n = nums.size + var prev = nums[n - 1] var ans: Long = 0 - for (i in nums.size - 2 downTo 0) { - var replacements = nums[i] / limit - 1 - if (nums[i] % limit != 0) { - replacements++ + for (i in n - 2 downTo 0) { + var noOfTime = nums[i] / prev + if (nums[i] % prev != 0) { + noOfTime++ + prev = nums[i] / noOfTime } - ans += replacements.toLong() - limit = nums[i] / (replacements + 1) + ans += (noOfTime - 1).toLong() } return ans } From 50e69beef4f49de99b10787771c35b788b348f7a Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sat, 3 May 2025 08:43:46 +0300 Subject: [PATCH 2/3] Improved tasks 194, 1022, 2366, 3473 --- .../Solution.kt | 45 ++++++------------- 1 file changed, 13 insertions(+), 32 deletions(-) diff --git a/src/main/kotlin/g3401_3500/s3473_sum_of_k_subarrays_with_length_at_least_m/Solution.kt b/src/main/kotlin/g3401_3500/s3473_sum_of_k_subarrays_with_length_at_least_m/Solution.kt index ddb5fb49d..f832cfd57 100644 --- a/src/main/kotlin/g3401_3500/s3473_sum_of_k_subarrays_with_length_at_least_m/Solution.kt +++ b/src/main/kotlin/g3401_3500/s3473_sum_of_k_subarrays_with_length_at_least_m/Solution.kt @@ -1,45 +1,26 @@ package g3401_3500.s3473_sum_of_k_subarrays_with_length_at_least_m // #Medium #Array #Dynamic_Programming #Prefix_Sum -// #2025_03_06_Time_227_ms_(24.47%)_Space_99.61_MB_(48.94%) - -import kotlin.math.max +// #2025_05_03_Time_33_ms_(98.18%)_Space_81.75_MB_(87.27%) class Solution { fun maxSum(nums: IntArray, k: Int, m: Int): Int { val n = nums.size - // Calculate prefix sums - val prefixSum = IntArray(n + 1) - for (i in 0..(n + 1) { IntArray(k + 1) } - // Initialize dp array - for (j in 1..k) { - for (i in 0..n) { - dp[i][j] = Int.Companion.MIN_VALUE / 2 - } + for (j in 0..n) { + dp[0][j] = 0 } - // Fill dp array - for (j in 1..k) { - val maxPrev = IntArray(n + 1) - for (i in 0.. Date: Sat, 3 May 2025 08:53:09 +0300 Subject: [PATCH 3/3] Added test --- .../s1022_sum_of_root_to_leaf_binary_numbers/SolutionTest.kt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/test/kotlin/g1001_1100/s1022_sum_of_root_to_leaf_binary_numbers/SolutionTest.kt b/src/test/kotlin/g1001_1100/s1022_sum_of_root_to_leaf_binary_numbers/SolutionTest.kt index a69cc3032..2a4d6af83 100644 --- a/src/test/kotlin/g1001_1100/s1022_sum_of_root_to_leaf_binary_numbers/SolutionTest.kt +++ b/src/test/kotlin/g1001_1100/s1022_sum_of_root_to_leaf_binary_numbers/SolutionTest.kt @@ -17,4 +17,9 @@ internal class SolutionTest { val root: TreeNode? = TreeNode.create(listOf(0)) assertThat(Solution().sumRootToLeaf(root), equalTo(0)) } + + @Test + fun sumRootToLeaf3() { + assertThat(Solution().sumRootToLeaf(null), equalTo(0)) + } }