Skip to content

Improved tasks 194, 1022, 2366, 3473 #810

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/main/kotlin/g0101_0200/s0194_transpose_file/script.sh
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -17,31 +17,18 @@ import com_github_leetcode.TreeNode
*/
class Solution {
fun sumRootToLeaf(root: TreeNode?): Int {
val paths: MutableList<List<Int>> = 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<List<Int>>, path: MutableList<Int>) {
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)
}
}
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
prefixSum[i + 1] = prefixSum[i] + nums[i]
val dp = Array(k + 1) { IntArray(n + 1) { Int.MIN_VALUE } }
val ps = IntArray(n + 1)
for (i in nums.indices) {
ps[i + 1] = ps[i] + nums[i]
}
// using elements from nums[0...i-1]
val dp = Array<IntArray>(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..<n + 1) {
maxPrev[i] =
if (i == 0) {
dp[0][j - 1] - prefixSum[0]
} else {
max(maxPrev[i - 1], dp[i][j - 1] - prefixSum[i])
}
}
for (i in m..n) {
// Option 1: Don't include the current element in any new subarray
dp[i][j] = dp[i - 1][j]
// Option 2: Form a new subarray ending at position i
// Find the best starting position for the subarray
dp[i][j] = max(dp[i][j], prefixSum[i] + maxPrev[i - m])
for (i in 1..k) {
var best = Int.MIN_VALUE
for (j in (i * m)..n) {
best = maxOf(best, dp[i - 1][j - m] - ps[j - m])
dp[i][j] = maxOf(dp[i][j - 1], ps[j] + best)
}
}
return dp[n][k]
return dp[k][n]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}