Skip to content

Commit 2d4a146

Browse files
committed
Added tasks 3536-3539
1 parent bbf0249 commit 2d4a146

File tree

5 files changed

+51
-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

5 files changed

+51
-69
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,6 +2088,10 @@
20882088

20892089
| # | Title | Difficulty | Tag | Time, ms | Time, %
20902090
|------|----------------|-------------|-------------|----------|--------
2091+
| 3539 |[Find Sum of Array Product of Magical Sequences](src/main/kotlin/g3501_3600/s3539_find_sum_of_array_product_of_magical_sequences)| Hard | Array, Dynamic_Programming, Math, Bit_Manipulation, Bitmask, Combinatorics | 60 | 100.00
2092+
| 3538 |[Merge Operations for Minimum Travel Time](src/main/kotlin/g3501_3600/s3538_merge_operations_for_minimum_travel_time)| Hard | Array, Dynamic_Programming, Prefix_Sum | 10 | 100.00
2093+
| 3537 |[Fill a Special Grid](src/main/kotlin/g3501_3600/s3537_fill_a_special_grid)| Medium | Array, Matrix, Divide_and_Conquer | 2 | 100.00
2094+
| 3536 |[Maximum Product of Two Digits](src/main/kotlin/g3501_3600/s3536_maximum_product_of_two_digits)| Easy | Math, Sorting | 1 | 100.00
20912095
| 3534 |[Path Existence Queries in a Graph II](src/main/kotlin/g3501_3600/s3534_path_existence_queries_in_a_graph_ii)| Hard | Array, Sorting, Greedy, Binary_Search, Graph | 152 | 100.00
20922096
| 3533 |[Concatenated Divisibility](src/main/kotlin/g3501_3600/s3533_concatenated_divisibility)| Hard | Array, Dynamic_Programming, Bit_Manipulation, Bitmask | 20 | 100.00
20932097
| 3532 |[Path Existence Queries in a Graph I](src/main/kotlin/g3501_3600/s3532_path_existence_queries_in_a_graph_i)| Medium | Array, Binary_Search, Graph, Union_Find | 5 | 90.91
@@ -2141,7 +2145,7 @@
21412145
| 3477 |[Fruits Into Baskets II](src/main/kotlin/g3401_3500/s3477_fruits_into_baskets_ii)| Easy | Array, Binary_Search, Simulation, Segment_Tree | 3 | 100.00
21422146
| 3475 |[DNA Pattern Recognition](src/main/kotlin/g3401_3500/s3475_dna_pattern_recognition)| Medium | Database | 362 | 83.49
21432147
| 3474 |[Lexicographically Smallest Generated String](src/main/kotlin/g3401_3500/s3474_lexicographically_smallest_generated_string)| Hard | String, Greedy, String_Matching | 30 | 100.00
2144-
| 3473 |[Sum of K Subarrays With Length at Least M](src/main/kotlin/g3401_3500/s3473_sum_of_k_subarrays_with_length_at_least_m)| Medium | Array, Dynamic_Programming, Prefix_Sum | 227 | 24.47
2148+
| 3473 |[Sum of K Subarrays With Length at Least M](src/main/kotlin/g3401_3500/s3473_sum_of_k_subarrays_with_length_at_least_m)| Medium | Array, Dynamic_Programming, Prefix_Sum | 33 | 98.18
21452149
| 3472 |[Longest Palindromic Subsequence After at Most K Operations](src/main/kotlin/g3401_3500/s3472_longest_palindromic_subsequence_after_at_most_k_operations)| Medium | String, Dynamic_Programming | 142 | 55.00
21462150
| 3471 |[Find the Largest Almost Missing Integer](src/main/kotlin/g3401_3500/s3471_find_the_largest_almost_missing_integer)| Easy | Array, Hash_Table | 6 | 86.49
21472151
| 3470 |[Permutations IV](src/main/kotlin/g3401_3500/s3470_permutations_iv)| Hard | Array, Math, Enumeration, Combinatorics | 4 | 96.77
@@ -3006,7 +3010,7 @@
30063010
| 2369 |[Check if There is a Valid Partition For The Array](src/main/kotlin/g2301_2400/s2369_check_if_there_is_a_valid_partition_for_the_array)| Medium | Array, Dynamic_Programming | 533 | 100.00
30073011
| 2368 |[Reachable Nodes With Restrictions](src/main/kotlin/g2301_2400/s2368_reachable_nodes_with_restrictions)| Medium | Array, Hash_Table, Depth_First_Search, Breadth_First_Search, Tree, Graph | 1199 | 76.92
30083012
| 2367 |[Number of Arithmetic Triplets](src/main/kotlin/g2301_2400/s2367_number_of_arithmetic_triplets)| Easy | Array, Hash_Table, Two_Pointers, Enumeration | 146 | 91.18
3009-
| 2366 |[Minimum Replacements to Sort the Array](src/main/kotlin/g2301_2400/s2366_minimum_replacements_to_sort_the_array)| Hard | Array, Math, Greedy | 433 | 100.00
3013+
| 2366 |[Minimum Replacements to Sort the Array](src/main/kotlin/g2301_2400/s2366_minimum_replacements_to_sort_the_array)| Hard | Array, Math, Greedy | 4 | 100.00
30103014
| 2365 |[Task Scheduler II](src/main/kotlin/g2301_2400/s2365_task_scheduler_ii)| Medium | Array, Hash_Table, Simulation | 595 | 100.00
30113015
| 2364 |[Count Number of Bad Pairs](src/main/kotlin/g2301_2400/s2364_count_number_of_bad_pairs)| Medium | Array, Hash_Table | 590 | 100.00
30123016
| 2363 |[Merge Similar Items](src/main/kotlin/g2301_2400/s2363_merge_similar_items)| Easy | Array, Hash_Table, Sorting, Ordered_Set | 320 | 100.00
@@ -4041,7 +4045,7 @@
40414045
| 1025 |[Divisor Game](src/main/kotlin/g1001_1100/s1025_divisor_game)| Easy | Dynamic_Programming, Math, Game_Theory, Brainteaser | 114 | 93.33
40424046
| 1024 |[Video Stitching](src/main/kotlin/g1001_1100/s1024_video_stitching)| Medium | Array, Dynamic_Programming, Greedy | 141 | 100.00
40434047
| 1023 |[Camelcase Matching](src/main/kotlin/g1001_1100/s1023_camelcase_matching)| Medium | String, Two_Pointers, Trie, String_Matching | 149 | 60.00
4044-
| 1022 |[Sum of Root To Leaf Binary Numbers](src/main/kotlin/g1001_1100/s1022_sum_of_root_to_leaf_binary_numbers)| Easy | Depth_First_Search, Tree, Binary_Tree | 158 | 88.89
4048+
| 1022 |[Sum of Root To Leaf Binary Numbers](src/main/kotlin/g1001_1100/s1022_sum_of_root_to_leaf_binary_numbers)| Easy | Depth_First_Search, Tree, Binary_Tree | 0 | 100.00
40454049
| 1021 |[Remove Outermost Parentheses](src/main/kotlin/g1001_1100/s1021_remove_outermost_parentheses)| Easy | String, Stack | 156 | 60.00
40464050
| 1020 |[Number of Enclaves](src/main/kotlin/g1001_1100/s1020_number_of_enclaves)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Graph_Theory_I_Day_3_Matrix_Related_Problems | 283 | 90.70
40474051
| 1019 |[Next Greater Node In Linked List](src/main/kotlin/g1001_1100/s1019_next_greater_node_in_linked_list)| Medium | Array, Stack, Linked_List, Monotonic_Stack | 472 | 75.00
@@ -4722,7 +4726,7 @@
47224726
| 0197 |[Rising Temperature](src/main/kotlin/g0101_0200/s0197_rising_temperature)| Easy | Database, SQL_I_Day_6_Union | 394 | 94.15
47234727
| 0196 |[Delete Duplicate Emails](src/main/kotlin/g0101_0200/s0196_delete_duplicate_emails)| Easy | Database, SQL_I_Day_2_Select_and_Order | 593 | 94.17
47244728
| 0195 |[Tenth Line](src/main/kotlin/g0101_0200/s0195_tenth_line)| Easy | Shell | 36 | 87.50
4725-
| 0194 |[Transpose File](src/main/kotlin/g0101_0200/s0194_transpose_file)| Medium | Shell | 461 | 33.47
4729+
| 0194 |[Transpose File](src/main/kotlin/g0101_0200/s0194_transpose_file)| Medium | Shell | 61 | 88.19
47264730
| 0193 |[Valid Phone Numbers](src/main/kotlin/g0101_0200/s0193_valid_phone_numbers)| Easy | Shell | 98 | 88.64
47274731
| 0192 |[Word Frequency](src/main/kotlin/g0101_0200/s0192_word_frequency)| Medium | Shell | 96 | 80.40
47284732
| 0191 |[Number of 1 Bits](src/main/kotlin/g0101_0200/s0191_number_of_1_bits)| Easy | Top_Interview_Questions, Bit_Manipulation, Algorithm_I_Day_13_Bit_Manipulation, Programming_Skills_I_Day_2_Operator, Udemy_Bit_Manipulation, Top_Interview_150_Bit_Manipulation | 237 | 68.44

src/main/kotlin/g0101_0200/s0194_transpose_file/readme.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,19 @@ Output the following:
2626

2727
```bash
2828
# Read from the file file.txt and print its transposed content to stdout.
29-
wordcount=$(head -1 file.txt | wc -w)
30-
col_n=1
31-
while [[ $col_n -le $wordcount ]]; do
32-
awk "{ print \$$col_n }" file.txt | paste -sd " "
33-
col_n=$((col_n + 1))
34-
done
29+
awk '
30+
{
31+
for (i = 1; i <= NF; i++) {
32+
if (NR == 1) {
33+
a[i] = $i
34+
} else {
35+
a[i] = a[i] " " $i
36+
}
37+
}
38+
}
39+
END {
40+
for (i = 1; i <= NF; i++) {
41+
print a[i]
42+
}
43+
}' file.txt
3544
```

src/main/kotlin/g1001_1100/s1022_sum_of_root_to_leaf_binary_numbers/readme.md

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,32 +51,19 @@ import com_github_leetcode.TreeNode
5151
*/
5252
class Solution {
5353
fun sumRootToLeaf(root: TreeNode?): Int {
54-
val paths: MutableList<List<Int>> = ArrayList()
55-
dfs(root, paths, ArrayList())
56-
var sum = 0
57-
for (list in paths) {
58-
var num = 0
59-
for (i in list) {
60-
num = (num shl 1) + i
61-
}
62-
sum += num
63-
}
64-
return sum
54+
return sumRootToLeaf(root, 0)
6555
}
6656

67-
private fun dfs(root: TreeNode?, paths: MutableList<List<Int>>, path: MutableList<Int>) {
68-
path.add(root!!.`val`)
69-
if (root.left != null) {
70-
dfs(root.left!!, paths, path)
71-
path.removeAt(path.size - 1)
72-
}
73-
if (root.right != null) {
74-
dfs(root.right!!, paths, path)
75-
path.removeAt(path.size - 1)
57+
private fun sumRootToLeaf(root: TreeNode?, sum: Int): Int {
58+
var sum = sum
59+
if (root == null) {
60+
return 0
7661
}
62+
sum = 2 * sum + root.`val`
7763
if (root.left == null && root.right == null) {
78-
paths.add(ArrayList(path))
64+
return sum
7965
}
66+
return sumRootToLeaf(root.left, sum) + sumRootToLeaf(root.right, sum)
8067
}
8168
}
8269
```

src/main/kotlin/g2301_2400/s2366_minimum_replacements_to_sort_the_array/readme.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,16 @@ There are 2 steps to sort the array in non-decreasing order. Therefore, we retur
4343
```kotlin
4444
class Solution {
4545
fun minimumReplacement(nums: IntArray): Long {
46-
var limit = nums[nums.size - 1]
46+
val n = nums.size
47+
var prev = nums[n - 1]
4748
var ans: Long = 0
48-
for (i in nums.size - 2 downTo 0) {
49-
var replacements = nums[i] / limit - 1
50-
if (nums[i] % limit != 0) {
51-
replacements++
49+
for (i in n - 2 downTo 0) {
50+
var noOfTime = nums[i] / prev
51+
if (nums[i] % prev != 0) {
52+
noOfTime++
53+
prev = nums[i] / noOfTime
5254
}
53-
ans += replacements.toLong()
54-
limit = nums[i] / (replacements + 1)
55+
ans += (noOfTime - 1).toLong()
5556
}
5657
return ans
5758
}

src/main/kotlin/g3401_3500/s3473_sum_of_k_subarrays_with_length_at_least_m/readme.md

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -44,44 +44,25 @@ The optimal choice is choosing each element as a subarray. The output is `(-10)
4444
## Solution
4545

4646
```kotlin
47-
import kotlin.math.max
48-
4947
class Solution {
5048
fun maxSum(nums: IntArray, k: Int, m: Int): Int {
5149
val n = nums.size
52-
// Calculate prefix sums
53-
val prefixSum = IntArray(n + 1)
54-
for (i in 0..<n) {
55-
prefixSum[i + 1] = prefixSum[i] + nums[i]
50+
val dp = Array(k + 1) { IntArray(n + 1) { Int.MIN_VALUE } }
51+
val ps = IntArray(n + 1)
52+
for (i in nums.indices) {
53+
ps[i + 1] = ps[i] + nums[i]
5654
}
57-
// using elements from nums[0...i-1]
58-
val dp = Array<IntArray>(n + 1) { IntArray(k + 1) }
59-
// Initialize dp array
60-
for (j in 1..k) {
61-
for (i in 0..n) {
62-
dp[i][j] = Int.Companion.MIN_VALUE / 2
63-
}
55+
for (j in 0..n) {
56+
dp[0][j] = 0
6457
}
65-
// Fill dp array
66-
for (j in 1..k) {
67-
val maxPrev = IntArray(n + 1)
68-
for (i in 0..<n + 1) {
69-
maxPrev[i] =
70-
if (i == 0) {
71-
dp[0][j - 1] - prefixSum[0]
72-
} else {
73-
max(maxPrev[i - 1], dp[i][j - 1] - prefixSum[i])
74-
}
75-
}
76-
for (i in m..n) {
77-
// Option 1: Don't include the current element in any new subarray
78-
dp[i][j] = dp[i - 1][j]
79-
// Option 2: Form a new subarray ending at position i
80-
// Find the best starting position for the subarray
81-
dp[i][j] = max(dp[i][j], prefixSum[i] + maxPrev[i - m])
58+
for (i in 1..k) {
59+
var best = Int.MIN_VALUE
60+
for (j in (i * m)..n) {
61+
best = maxOf(best, dp[i - 1][j - m] - ps[j - m])
62+
dp[i][j] = maxOf(dp[i][j - 1], ps[j] + best)
8263
}
8364
}
84-
return dp[n][k]
65+
return dp[k][n]
8566
}
8667
}
8768
```

0 commit comments

Comments
 (0)