Skip to content

Commit 37bc21b

Browse files
committed
Improved tasks 194, 1022, 2366
1 parent 9f6bb48 commit 37bc21b

File tree

3 files changed

+34
-37
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

3 files changed

+34
-37
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
}

0 commit comments

Comments
 (0)