Skip to content

Commit bce104a

Browse files
authored
Improved tasks 194, 1022, 2366, 3473
1 parent 9f6bb48 commit bce104a

File tree

5 files changed

+52
-69
lines changed
  • src
    • main/kotlin
      • g0101_0200/s0194_transpose_file
      • g1001_1100/s1022_sum_of_root_to_leaf_binary_numbers
      • g2301_2400/s2366_minimum_replacements_to_sort_the_array
      • g3401_3500/s3473_sum_of_k_subarrays_with_length_at_least_m
    • test/kotlin/g1001_1100/s1022_sum_of_root_to_leaf_binary_numbers

5 files changed

+52
-69
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
# Read from the file file.txt and print its transposed content to stdout.
2-
# #Medium #Shell #2022_11_25_Time_461_ms_(33.47%)_Space_3.9_MB_(34.89%)
3-
wordcount=$(head -1 file.txt | wc -w)
4-
col_n=1
5-
while [[ $col_n -le $wordcount ]]; do
6-
awk "{ print \$$col_n }" file.txt | paste -sd " "
7-
col_n=$((col_n + 1))
8-
done
2+
# #Medium #Shell #2025_05_03_Time_61_ms_(88.19%)_Space_4.14_MB_(38.67%)
3+
awk '
4+
{
5+
for (i = 1; i <= NF; i++) {
6+
if (NR == 1) {
7+
a[i] = $i
8+
} else {
9+
a[i] = a[i] " " $i
10+
}
11+
}
12+
}
13+
END {
14+
for (i = 1; i <= NF; i++) {
15+
print a[i]
16+
}
17+
}' file.txt
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package g1001_1100.s1022_sum_of_root_to_leaf_binary_numbers
22

33
// #Easy #Depth_First_Search #Tree #Binary_Tree
4-
// #2023_05_22_Time_158_ms_(88.89%)_Space_36.3_MB_(11.11%)
4+
// #2025_05_03_Time_0_ms_(100.00%)_Space_41.68_MB_(93.33%)
55

66
import com_github_leetcode.TreeNode
77

@@ -17,31 +17,18 @@ import com_github_leetcode.TreeNode
1717
*/
1818
class Solution {
1919
fun sumRootToLeaf(root: TreeNode?): Int {
20-
val paths: MutableList<List<Int>> = ArrayList()
21-
dfs(root, paths, ArrayList())
22-
var sum = 0
23-
for (list in paths) {
24-
var num = 0
25-
for (i in list) {
26-
num = (num shl 1) + i
27-
}
28-
sum += num
29-
}
30-
return sum
20+
return sumRootToLeaf(root, 0)
3121
}
3222

33-
private fun dfs(root: TreeNode?, paths: MutableList<List<Int>>, path: MutableList<Int>) {
34-
path.add(root!!.`val`)
35-
if (root.left != null) {
36-
dfs(root.left!!, paths, path)
37-
path.removeAt(path.size - 1)
38-
}
39-
if (root.right != null) {
40-
dfs(root.right!!, paths, path)
41-
path.removeAt(path.size - 1)
23+
private fun sumRootToLeaf(root: TreeNode?, sum: Int): Int {
24+
var sum = sum
25+
if (root == null) {
26+
return 0
4227
}
28+
sum = 2 * sum + root.`val`
4329
if (root.left == null && root.right == null) {
44-
paths.add(ArrayList(path))
30+
return sum
4531
}
32+
return sumRootToLeaf(root.left, sum) + sumRootToLeaf(root.right, sum)
4633
}
4734
}

src/main/kotlin/g2301_2400/s2366_minimum_replacements_to_sort_the_array/Solution.kt

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
package g2301_2400.s2366_minimum_replacements_to_sort_the_array
22

3-
// #Hard #Array #Math #Greedy #2023_07_02_Time_433_ms_(100.00%)_Space_57.9_MB_(100.00%)
3+
// #Hard #Array #Math #Greedy #2025_05_03_Time_4_ms_(100.00%)_Space_60.26_MB_(66.67%)
44

55
class Solution {
66
fun minimumReplacement(nums: IntArray): Long {
7-
var limit = nums[nums.size - 1]
7+
val n = nums.size
8+
var prev = nums[n - 1]
89
var ans: Long = 0
9-
for (i in nums.size - 2 downTo 0) {
10-
var replacements = nums[i] / limit - 1
11-
if (nums[i] % limit != 0) {
12-
replacements++
10+
for (i in n - 2 downTo 0) {
11+
var noOfTime = nums[i] / prev
12+
if (nums[i] % prev != 0) {
13+
noOfTime++
14+
prev = nums[i] / noOfTime
1315
}
14-
ans += replacements.toLong()
15-
limit = nums[i] / (replacements + 1)
16+
ans += (noOfTime - 1).toLong()
1617
}
1718
return ans
1819
}
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,26 @@
11
package g3401_3500.s3473_sum_of_k_subarrays_with_length_at_least_m
22

33
// #Medium #Array #Dynamic_Programming #Prefix_Sum
4-
// #2025_03_06_Time_227_ms_(24.47%)_Space_99.61_MB_(48.94%)
5-
6-
import kotlin.math.max
4+
// #2025_05_03_Time_33_ms_(98.18%)_Space_81.75_MB_(87.27%)
75

86
class Solution {
97
fun maxSum(nums: IntArray, k: Int, m: Int): Int {
108
val n = nums.size
11-
// Calculate prefix sums
12-
val prefixSum = IntArray(n + 1)
13-
for (i in 0..<n) {
14-
prefixSum[i + 1] = prefixSum[i] + nums[i]
9+
val dp = Array(k + 1) { IntArray(n + 1) { Int.MIN_VALUE } }
10+
val ps = IntArray(n + 1)
11+
for (i in nums.indices) {
12+
ps[i + 1] = ps[i] + nums[i]
1513
}
16-
// using elements from nums[0...i-1]
17-
val dp = Array<IntArray>(n + 1) { IntArray(k + 1) }
18-
// Initialize dp array
19-
for (j in 1..k) {
20-
for (i in 0..n) {
21-
dp[i][j] = Int.Companion.MIN_VALUE / 2
22-
}
14+
for (j in 0..n) {
15+
dp[0][j] = 0
2316
}
24-
// Fill dp array
25-
for (j in 1..k) {
26-
val maxPrev = IntArray(n + 1)
27-
for (i in 0..<n + 1) {
28-
maxPrev[i] =
29-
if (i == 0) {
30-
dp[0][j - 1] - prefixSum[0]
31-
} else {
32-
max(maxPrev[i - 1], dp[i][j - 1] - prefixSum[i])
33-
}
34-
}
35-
for (i in m..n) {
36-
// Option 1: Don't include the current element in any new subarray
37-
dp[i][j] = dp[i - 1][j]
38-
// Option 2: Form a new subarray ending at position i
39-
// Find the best starting position for the subarray
40-
dp[i][j] = max(dp[i][j], prefixSum[i] + maxPrev[i - m])
17+
for (i in 1..k) {
18+
var best = Int.MIN_VALUE
19+
for (j in (i * m)..n) {
20+
best = maxOf(best, dp[i - 1][j - m] - ps[j - m])
21+
dp[i][j] = maxOf(dp[i][j - 1], ps[j] + best)
4122
}
4223
}
43-
return dp[n][k]
24+
return dp[k][n]
4425
}
4526
}

src/test/kotlin/g1001_1100/s1022_sum_of_root_to_leaf_binary_numbers/SolutionTest.kt

+5
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,9 @@ internal class SolutionTest {
1717
val root: TreeNode? = TreeNode.create(listOf(0))
1818
assertThat(Solution().sumRootToLeaf(root), equalTo(0))
1919
}
20+
21+
@Test
22+
fun sumRootToLeaf3() {
23+
assertThat(Solution().sumRootToLeaf(null), equalTo(0))
24+
}
2025
}

0 commit comments

Comments
 (0)