Skip to content

Commit d9d20e0

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

File tree

9 files changed

+509
-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
    • g3501_3600
      • s3536_maximum_product_of_two_digits
      • s3537_fill_a_special_grid
      • s3538_merge_operations_for_minimum_travel_time
      • s3539_find_sum_of_array_product_of_magical_sequences

9 files changed

+509
-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
```
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3536\. Maximum Product of Two Digits
5+
6+
Easy
7+
8+
You are given a positive integer `n`.
9+
10+
Return the **maximum** product of any two digits in `n`.
11+
12+
**Note:** You may use the **same** digit twice if it appears more than once in `n`.
13+
14+
**Example 1:**
15+
16+
**Input:** n = 31
17+
18+
**Output:** 3
19+
20+
**Explanation:**
21+
22+
* The digits of `n` are `[3, 1]`.
23+
* The possible products of any two digits are: `3 * 1 = 3`.
24+
* The maximum product is 3.
25+
26+
**Example 2:**
27+
28+
**Input:** n = 22
29+
30+
**Output:** 4
31+
32+
**Explanation:**
33+
34+
* The digits of `n` are `[2, 2]`.
35+
* The possible products of any two digits are: `2 * 2 = 4`.
36+
* The maximum product is 4.
37+
38+
**Example 3:**
39+
40+
**Input:** n = 124
41+
42+
**Output:** 8
43+
44+
**Explanation:**
45+
46+
* The digits of `n` are `[1, 2, 4]`.
47+
* The possible products of any two digits are: `1 * 2 = 2`, `1 * 4 = 4`, `2 * 4 = 8`.
48+
* The maximum product is 8.
49+
50+
**Constraints:**
51+
52+
* <code>10 <= n <= 10<sup>9</sup></code>
53+
54+
## Solution
55+
56+
```kotlin
57+
class Solution {
58+
fun maxProduct(n: Int): Int {
59+
var n = n
60+
var m1 = n % 10
61+
n /= 10
62+
var m2 = n % 10
63+
n /= 10
64+
while (n > 0) {
65+
val a = n % 10
66+
if (a > m1) {
67+
if (m1 > m2) {
68+
m2 = m1
69+
}
70+
m1 = a
71+
} else {
72+
if (a > m2) {
73+
m2 = a
74+
}
75+
}
76+
n /= 10
77+
}
78+
return m1 * m2
79+
}
80+
}
81+
```
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3537\. Fill a Special Grid
5+
6+
Medium
7+
8+
You are given a non-negative integer `n` representing a <code>2<sup>n</sup> x 2<sup>n</sup></code> grid. You must fill the grid with integers from 0 to <code>2<sup>2n</sup> - 1</code> to make it **special**. A grid is **special** if it satisfies **all** the following conditions:
9+
10+
* All numbers in the top-right quadrant are smaller than those in the bottom-right quadrant.
11+
* All numbers in the bottom-right quadrant are smaller than those in the bottom-left quadrant.
12+
* All numbers in the bottom-left quadrant are smaller than those in the top-left quadrant.
13+
* Each of its quadrants is also a special grid.
14+
15+
Return the **special** <code>2<sup>n</sup> x 2<sup>n</sup></code> grid.
16+
17+
**Note**: Any 1x1 grid is special.
18+
19+
**Example 1:**
20+
21+
**Input:** n = 0
22+
23+
**Output:** [[0]]
24+
25+
**Explanation:**
26+
27+
The only number that can be placed is 0, and there is only one possible position in the grid.
28+
29+
**Example 2:**
30+
31+
**Input:** n = 1
32+
33+
**Output:** [[3,0],[2,1]]
34+
35+
**Explanation:**
36+
37+
The numbers in each quadrant are:
38+
39+
* Top-right: 0
40+
* Bottom-right: 1
41+
* Bottom-left: 2
42+
* Top-left: 3
43+
44+
Since `0 < 1 < 2 < 3`, this satisfies the given constraints.
45+
46+
**Example 3:**
47+
48+
**Input:** n = 2
49+
50+
**Output:** [[15,12,3,0],[14,13,2,1],[11,8,7,4],[10,9,6,5]]
51+
52+
**Explanation:**
53+
54+
![](https://assets.leetcode.com/uploads/2025/03/05/4123example3p1drawio.png)
55+
56+
The numbers in each quadrant are:
57+
58+
* Top-right: 3, 0, 2, 1
59+
* Bottom-right: 7, 4, 6, 5
60+
* Bottom-left: 11, 8, 10, 9
61+
* Top-left: 15, 12, 14, 13
62+
* `max(3, 0, 2, 1) < min(7, 4, 6, 5)`
63+
* `max(7, 4, 6, 5) < min(11, 8, 10, 9)`
64+
* `max(11, 8, 10, 9) < min(15, 12, 14, 13)`
65+
66+
This satisfies the first three requirements. Additionally, each quadrant is also a special grid. Thus, this is a special grid.
67+
68+
**Constraints:**
69+
70+
* `0 <= n <= 10`
71+
72+
## Solution
73+
74+
```kotlin
75+
import kotlin.math.pow
76+
77+
class Solution {
78+
fun specialGrid(n: Int): Array<IntArray> {
79+
if (n == 0) {
80+
return arrayOf<IntArray>(intArrayOf(0))
81+
}
82+
val len = 2.0.pow(n.toDouble()).toInt()
83+
val ans = Array<IntArray>(len) { IntArray(len) }
84+
val num = intArrayOf(2.0.pow(2.0 * n).toInt() - 1)
85+
backtrack(ans, len, len, 0, 0, num)
86+
return ans
87+
}
88+
89+
private fun backtrack(ans: Array<IntArray>, m: Int, n: Int, x: Int, y: Int, num: IntArray) {
90+
if (m == 2 && n == 2) {
91+
ans[x][y] = num[0]
92+
ans[x + 1][y] = num[0] - 1
93+
ans[x + 1][y + 1] = num[0] - 2
94+
ans[x][y + 1] = num[0] - 3
95+
num[0] -= 4
96+
return
97+
}
98+
backtrack(ans, m / 2, n / 2, x, y, num)
99+
backtrack(ans, m / 2, n / 2, x + m / 2, y, num)
100+
backtrack(ans, m / 2, n / 2, x + m / 2, y + n / 2, num)
101+
backtrack(ans, m / 2, n / 2, x, y + n / 2, num)
102+
}
103+
}
104+
```

0 commit comments

Comments
 (0)