Skip to content

Commit f599ec4

Browse files
committed
Added tasks 3536-3539
1 parent f6d0e06 commit f599ec4

File tree

8 files changed

+484
-42
lines changed
  • src/main/java
    • g0101_0200/s0194_transpose_file
    • g1001_1100/s1022_sum_of_root_to_leaf_binary_numbers
    • g2301_2400/s2366_minimum_replacements_to_sort_the_array
    • 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

8 files changed

+484
-42
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,7 @@
11601160
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11611161
|-|-|-|-|-|-
11621162
| 0200 |[Number of Islands](src/main/java/g0101_0200/s0200_number_of_islands)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Big_O_Time_O(M\*N)_Space_O(M\*N) | 3 | 87.24
1163-
| 0133 |[Clone Graph](src/main/java/g0101_0200/s0133_clone_graph)| Medium | Hash_Table, Depth_First_Search, Breadth_First_Search, Graph | 45 | 29.80
1163+
| 0133 |[Clone Graph](src/main/java/g0101_0200/s0133_clone_graph)| Medium | Hash_Table, Depth_First_Search, Breadth_First_Search, Graph | 25 | 68.87
11641164
| 0417 |[Pacific Atlantic Water Flow](src/main/java/g0401_0500/s0417_pacific_atlantic_water_flow)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix | 5 | 92.62
11651165

11661166
#### Udemy Dynamic Programming
@@ -1364,7 +1364,7 @@
13641364
|-|-|-|-|-|-
13651365
| 0200 |[Number of Islands](src/main/java/g0101_0200/s0200_number_of_islands)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Big_O_Time_O(M\*N)_Space_O(M\*N) | 3 | 87.24
13661366
| 0130 |[Surrounded Regions](src/main/java/g0101_0200/s0130_surrounded_regions)| Medium | Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 2 | 84.66
1367-
| 0133 |[Clone Graph](src/main/java/g0101_0200/s0133_clone_graph)| Medium | Hash_Table, Depth_First_Search, Breadth_First_Search, Graph | 45 | 29.80
1367+
| 0133 |[Clone Graph](src/main/java/g0101_0200/s0133_clone_graph)| Medium | Hash_Table, Depth_First_Search, Breadth_First_Search, Graph | 25 | 68.87
13681368
| 0399 |[Evaluate Division](src/main/java/g0301_0400/s0399_evaluate_division)| Medium | Array, Depth_First_Search, Breadth_First_Search, Graph, Union_Find, Shortest_Path, LeetCode_75_Graphs/DFS | 1 | 99.52
13691369
| 0207 |[Course Schedule](src/main/java/g0201_0300/s0207_course_schedule)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort, Big_O_Time_O(N)_Space_O(N) | 3 | 99.99
13701370
| 0210 |[Course Schedule II](src/main/java/g0201_0300/s0210_course_schedule_ii)| Medium | Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort | 4 | 91.07
@@ -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/java/g3501_3600/s3539_find_sum_of_array_product_of_magical_sequences)| Hard | Array, Dynamic_Programming, Math, Bit_Manipulation, Bitmask, Combinatorics | 39 | 95.71
2092+
| 3538 |[Merge Operations for Minimum Travel Time](src/main/java/g3501_3600/s3538_merge_operations_for_minimum_travel_time)| Hard | Array, Dynamic_Programming, Prefix_Sum | 7 | 99.32
2093+
| 3537 |[Fill a Special Grid](src/main/java/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/java/g3501_3600/s3536_maximum_product_of_two_digits)| Easy | Math, Sorting | 1 | 95.82
20912095
| 3534 |[Path Existence Queries in a Graph II](src/main/java/g3501_3600/s3534_path_existence_queries_in_a_graph_ii)| Hard | Array, Sorting, Greedy, Binary_Search, Graph | 84 | 100.00
20922096
| 3533 |[Concatenated Divisibility](src/main/java/g3501_3600/s3533_concatenated_divisibility)| Hard | Array, Dynamic_Programming, Bit_Manipulation, Bitmask | 14 | 100.00
20932097
| 3532 |[Path Existence Queries in a Graph I](src/main/java/g3501_3600/s3532_path_existence_queries_in_a_graph_i)| Medium | Array, Binary_Search, Graph, Union_Find | 3 | 100.00
@@ -3006,7 +3010,7 @@
30063010
| 2369 |[Check if There is a Valid Partition For The Array](src/main/java/g2301_2400/s2369_check_if_there_is_a_valid_partition_for_the_array)| Medium | Array, Dynamic_Programming | 7 | 81.82
30073011
| 2368 |[Reachable Nodes With Restrictions](src/main/java/g2301_2400/s2368_reachable_nodes_with_restrictions)| Medium | Array, Hash_Table, Depth_First_Search, Breadth_First_Search, Tree, Graph | 59 | 85.71
30083012
| 2367 |[Number of Arithmetic Triplets](src/main/java/g2301_2400/s2367_number_of_arithmetic_triplets)| Easy | Array, Hash_Table, Two_Pointers, Enumeration | 3 | 66.67
3009-
| 2366 |[Minimum Replacements to Sort the Array](src/main/java/g2301_2400/s2366_minimum_replacements_to_sort_the_array)| Hard | Array, Math, Greedy | 10 | 28.57
3013+
| 2366 |[Minimum Replacements to Sort the Array](src/main/java/g2301_2400/s2366_minimum_replacements_to_sort_the_array)| Hard | Array, Math, Greedy | 3 | 98.58
30103014
| 2365 |[Task Scheduler II](src/main/java/g2301_2400/s2365_task_scheduler_ii)| Medium | Array, Hash_Table, Simulation | 70 | 55.56
30113015
| 2364 |[Count Number of Bad Pairs](src/main/java/g2301_2400/s2364_count_number_of_bad_pairs)| Medium | Array, Hash_Table | 44 | 80.00
30123016
| 2363 |[Merge Similar Items](src/main/java/g2301_2400/s2363_merge_similar_items)| Easy | Array, Hash_Table, Sorting, Ordered_Set | 3 | 100.00
@@ -4041,7 +4045,7 @@
40414045
| 1025 |[Divisor Game](src/main/java/g1001_1100/s1025_divisor_game)| Easy | Dynamic_Programming, Math, Game_Theory, Brainteaser | 0 | 100.00
40424046
| 1024 |[Video Stitching](src/main/java/g1001_1100/s1024_video_stitching)| Medium | Array, Dynamic_Programming, Greedy | 1 | 88.78
40434047
| 1023 |[Camelcase Matching](src/main/java/g1001_1100/s1023_camelcase_matching)| Medium | String, Two_Pointers, Trie, String_Matching | 1 | 73.86
4044-
| 1022 |[Sum of Root To Leaf Binary Numbers](src/main/java/g1001_1100/s1022_sum_of_root_to_leaf_binary_numbers)| Easy | Depth_First_Search, Tree, Binary_Tree | 3 | 28.58
4048+
| 1022 |[Sum of Root To Leaf Binary Numbers](src/main/java/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/java/g1001_1100/s1021_remove_outermost_parentheses)| Easy | String, Stack | 4 | 75.39
40464050
| 1020 |[Number of Enclaves](src/main/java/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 | 6 | 68.24
40474051
| 1019 |[Next Greater Node In Linked List](src/main/java/g1001_1100/s1019_next_greater_node_in_linked_list)| Medium | Array, Stack, Linked_List, Monotonic_Stack | 11 | 96.35
@@ -4722,7 +4726,7 @@
47224726
| 0197 |[Rising Temperature](src/main/java/g0101_0200/s0197_rising_temperature)| Easy | Database, SQL_I_Day_6_Union | 342 | 93.76
47234727
| 0196 |[Delete Duplicate Emails](src/main/java/g0101_0200/s0196_delete_duplicate_emails)| Easy | Database, SQL_I_Day_2_Select_and_Order | 903 | 48.10
47244728
| 0195 |[Tenth Line](src/main/java/g0101_0200/s0195_tenth_line)| Easy | Shell | 26 | 99.36
4725-
| 0194 |[Transpose File](src/main/java/g0101_0200/s0194_transpose_file)| Medium | Shell | 630 | 28.43
4729+
| 0194 |[Transpose File](src/main/java/g0101_0200/s0194_transpose_file)| Medium | Shell | 61 | 88.19
47264730
| 0193 |[Valid Phone Numbers](src/main/java/g0101_0200/s0193_valid_phone_numbers)| Easy | Shell | 112 | 75.54
47274731
| 0192 |[Word Frequency](src/main/java/g0101_0200/s0192_word_frequency)| Medium | Shell | 143 | 54.55
47284732
| 0191 |[Number of 1 Bits](src/main/java/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 | 0 | 100.00
@@ -4775,7 +4779,7 @@
47754779
| 0136 |[Single Number](src/main/java/g0101_0200/s0136_single_number)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, LeetCode_75_Bit_Manipulation, Data_Structure_II_Day_1_Array, Algorithm_I_Day_14_Bit_Manipulation, Udemy_Integers, Top_Interview_150_Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 1 | 99.86
47764780
| 0135 |[Candy](src/main/java/g0101_0200/s0135_candy)| Hard | Array, Greedy, Top_Interview_150_Array/String | 3 | 83.95
47774781
| 0134 |[Gas Station](src/main/java/g0101_0200/s0134_gas_station)| Medium | Top_Interview_Questions, Array, Greedy, Top_Interview_150_Array/String | 2 | 97.52
4778-
| 0133 |[Clone Graph](src/main/java/g0101_0200/s0133_clone_graph)| Medium | Hash_Table, Depth_First_Search, Breadth_First_Search, Graph, Udemy_Graph, Top_Interview_150_Graph_General | 45 | 29.80
4782+
| 0133 |[Clone Graph](src/main/java/g0101_0200/s0133_clone_graph)| Medium | Hash_Table, Depth_First_Search, Breadth_First_Search, Graph, Udemy_Graph, Top_Interview_150_Graph_General | 25 | 68.87
47794783
| 0132 |[Palindrome Partitioning II](src/main/java/g0101_0200/s0132_palindrome_partitioning_ii)| Hard | String, Dynamic_Programming | 2 | 99.89
47804784
| 0131 |[Palindrome Partitioning](src/main/java/g0101_0200/s0131_palindrome_partitioning)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Backtracking, Big_O_Time_O(N\*2^N)_Space_O(2^N\*N) | 7 | 98.55
47814785
| 0130 |[Surrounded Regions](src/main/java/g0101_0200/s0130_surrounded_regions)| Medium | Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Algorithm_II_Day_8_Breadth_First_Search_Depth_First_Search, Top_Interview_150_Graph_General | 2 | 84.66

src/main/java/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/java/g1001_1100/s1022_sum_of_root_to_leaf_binary_numbers/readme.md

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ The test cases are generated so that the answer fits in a **32-bits** integer.
3838

3939
```java
4040
import com_github_leetcode.TreeNode;
41-
import java.util.ArrayList;
42-
import java.util.List;
4341

4442
/*
4543
* Definition for a binary tree node.
@@ -58,32 +56,18 @@ import java.util.List;
5856
*/
5957
public class Solution {
6058
public int sumRootToLeaf(TreeNode root) {
61-
List<List<Integer>> paths = new ArrayList<>();
62-
dfs(root, paths, new ArrayList<>());
63-
int sum = 0;
64-
for (List<Integer> list : paths) {
65-
int num = 0;
66-
for (int i : list) {
67-
num = (num << 1) + i;
68-
}
69-
sum += num;
70-
}
71-
return sum;
59+
return sumRootToLeaf(root, 0);
7260
}
7361

74-
private void dfs(TreeNode root, List<List<Integer>> paths, List<Integer> path) {
75-
path.add(root.val);
76-
if (root.left != null) {
77-
dfs(root.left, paths, path);
78-
path.remove(path.size() - 1);
79-
}
80-
if (root.right != null) {
81-
dfs(root.right, paths, path);
82-
path.remove(path.size() - 1);
62+
private int sumRootToLeaf(TreeNode root, int sum) {
63+
if (root == null) {
64+
return 0;
8365
}
66+
sum = 2 * sum + root.val;
8467
if (root.left == null && root.right == null) {
85-
paths.add(new ArrayList<>(path));
68+
return sum;
8669
}
70+
return sumRootToLeaf(root.left, sum) + sumRootToLeaf(root.right, sum);
8771
}
8872
}
8973
```

src/main/java/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
```java
4444
public class Solution {
4545
public long minimumReplacement(int[] nums) {
46-
int limit = nums[nums.length - 1];
46+
int n = nums.length;
47+
int prev = nums[n - 1];
4748
long ans = 0;
48-
for (int i = nums.length - 2; i >= 0; i--) {
49-
int replacements = nums[i] / limit - 1;
50-
if (nums[i] % limit != 0) {
51-
replacements++;
49+
for (int i = n - 2; i >= 0; i--) {
50+
int noOfTime = nums[i] / prev;
51+
if (nums[i] % prev != 0) {
52+
noOfTime++;
53+
prev = nums[i] / noOfTime;
5254
}
53-
ans += replacements;
54-
limit = nums[i] / (replacements + 1);
55+
ans += noOfTime - 1;
5556
}
5657
return ans;
5758
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/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+
```java
57+
public class Solution {
58+
public int maxProduct(int n) {
59+
int m1 = n % 10;
60+
n /= 10;
61+
int m2 = n % 10;
62+
n /= 10;
63+
while (n > 0) {
64+
int a = n % 10;
65+
if (a > m1) {
66+
if (m1 > m2) {
67+
m2 = m1;
68+
}
69+
m1 = a;
70+
} else {
71+
if (a > m2) {
72+
m2 = a;
73+
}
74+
}
75+
n /= 10;
76+
}
77+
return m1 * m2;
78+
}
79+
}
80+
```
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/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+
```java
75+
public class Solution {
76+
public int[][] specialGrid(int n) {
77+
if (n == 0) {
78+
return new int[][] { {0}};
79+
}
80+
int len = (int) Math.pow(2, n);
81+
int[][] ans = new int[len][len];
82+
int[] num = new int[] {(int) Math.pow(2, 2D * n) - 1};
83+
backtrack(ans, len, len, 0, 0, num);
84+
return ans;
85+
}
86+
87+
private void backtrack(int[][] ans, int m, int n, int x, int y, int[] num) {
88+
if (m == 2 && n == 2) {
89+
ans[x][y] = num[0];
90+
ans[x + 1][y] = num[0] - 1;
91+
ans[x + 1][y + 1] = num[0] - 2;
92+
ans[x][y + 1] = num[0] - 3;
93+
num[0] -= 4;
94+
return;
95+
}
96+
backtrack(ans, m / 2, n / 2, x, y, num);
97+
backtrack(ans, m / 2, n / 2, x + m / 2, y, num);
98+
backtrack(ans, m / 2, n / 2, x + m / 2, y + n / 2, num);
99+
backtrack(ans, m / 2, n / 2, x, y + n / 2, num);
100+
}
101+
}
102+
```

0 commit comments

Comments
 (0)