Skip to content

Commit 9ee0585

Browse files
authored
Improved tasks 133, 194, 1022, 2366
1 parent 9ddda61 commit 9ee0585

File tree

6 files changed

+42
-42
lines changed

6 files changed

+42
-42
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,7 @@ implementation 'com.github.javadev:leetcode-in-java:1.43'
11881188
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11891189
|-|-|-|-|-|-
11901190
| 0200 |[Number of Islands](src/main/java/g0101_0200/s0200_number_of_islands/Solution.java)| 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
1191-
| 0133 |[Clone Graph](src/main/java/g0101_0200/s0133_clone_graph/Solution.java)| Medium | Hash_Table, Depth_First_Search, Breadth_First_Search, Graph | 45 | 29.80
1191+
| 0133 |[Clone Graph](src/main/java/g0101_0200/s0133_clone_graph/Solution.java)| Medium | Hash_Table, Depth_First_Search, Breadth_First_Search, Graph | 25 | 68.87
11921192
| 0417 |[Pacific Atlantic Water Flow](src/main/java/g0401_0500/s0417_pacific_atlantic_water_flow/Solution.java)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix | 5 | 92.62
11931193

11941194
#### Udemy Dynamic Programming
@@ -1392,7 +1392,7 @@ implementation 'com.github.javadev:leetcode-in-java:1.43'
13921392
|-|-|-|-|-|-
13931393
| 0200 |[Number of Islands](src/main/java/g0101_0200/s0200_number_of_islands/Solution.java)| 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
13941394
| 0130 |[Surrounded Regions](src/main/java/g0101_0200/s0130_surrounded_regions/Solution.java)| Medium | Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 2 | 84.66
1395-
| 0133 |[Clone Graph](src/main/java/g0101_0200/s0133_clone_graph/Solution.java)| Medium | Hash_Table, Depth_First_Search, Breadth_First_Search, Graph | 45 | 29.80
1395+
| 0133 |[Clone Graph](src/main/java/g0101_0200/s0133_clone_graph/Solution.java)| Medium | Hash_Table, Depth_First_Search, Breadth_First_Search, Graph | 25 | 68.87
13961396
| 0399 |[Evaluate Division](src/main/java/g0301_0400/s0399_evaluate_division/Solution.java)| Medium | Array, Depth_First_Search, Breadth_First_Search, Graph, Union_Find, Shortest_Path, LeetCode_75_Graphs/DFS | 1 | 99.52
13971397
| 0207 |[Course Schedule](src/main/java/g0201_0300/s0207_course_schedule/Solution.java)| 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
13981398
| 0210 |[Course Schedule II](src/main/java/g0201_0300/s0210_course_schedule_ii/Solution.java)| Medium | Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort | 4 | 91.07

src/main/java/g0101_0200/s0133_clone_graph/Solution.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package g0101_0200.s0133_clone_graph;
22

33
// #Medium #Hash_Table #Depth_First_Search #Breadth_First_Search #Graph #Udemy_Graph
4-
// #Top_Interview_150_Graph_General #2022_06_24_Time_45_ms_(29.80%)_Space_42.7_MB_(77.96%)
4+
// #Top_Interview_150_Graph_General #2025_05_03_Time_25_ms_(68.87%)_Space_43.26_MB_(7.02%)
55

66
import com_github_leetcode.Node;
77
import java.util.ArrayList;
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_06_28_Time_630_ms_(28.43%)_Space_3.9_MB_(71.08%)
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,10 +1,9 @@
11
package g1001_1100.s1022_sum_of_root_to_leaf_binary_numbers;
22

3-
// #Easy #Depth_First_Search #Tree #Binary_Tree #2022_02_26_Time_3_ms_(28.58%)_Space_43.6_MB_(5.47%)
3+
// #Easy #Depth_First_Search #Tree #Binary_Tree
4+
// #2025_05_03_Time_0_ms_(100.00%)_Space_42.08_MB_(64.36%)
45

56
import com_github_leetcode.TreeNode;
6-
import java.util.ArrayList;
7-
import java.util.List;
87

98
/*
109
* Definition for a binary tree node.
@@ -23,31 +22,17 @@
2322
*/
2423
public class Solution {
2524
public int sumRootToLeaf(TreeNode root) {
26-
List<List<Integer>> paths = new ArrayList<>();
27-
dfs(root, paths, new ArrayList<>());
28-
int sum = 0;
29-
for (List<Integer> list : paths) {
30-
int num = 0;
31-
for (int i : list) {
32-
num = (num << 1) + i;
33-
}
34-
sum += num;
35-
}
36-
return sum;
25+
return sumRootToLeaf(root, 0);
3726
}
3827

39-
private void dfs(TreeNode root, List<List<Integer>> paths, List<Integer> path) {
40-
path.add(root.val);
41-
if (root.left != null) {
42-
dfs(root.left, paths, path);
43-
path.remove(path.size() - 1);
44-
}
45-
if (root.right != null) {
46-
dfs(root.right, paths, path);
47-
path.remove(path.size() - 1);
28+
private int sumRootToLeaf(TreeNode root, int sum) {
29+
if (root == null) {
30+
return 0;
4831
}
32+
sum = 2 * sum + root.val;
4933
if (root.left == null && root.right == null) {
50-
paths.add(new ArrayList<>(path));
34+
return sum;
5135
}
36+
return sumRootToLeaf(root.left, sum) + sumRootToLeaf(root.right, sum);
5237
}
5338
}

src/main/java/g2301_2400/s2366_minimum_replacements_to_sort_the_array/Solution.java

+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 #2022_08_14_Time_10_ms_(28.57%)_Space_81.5_MB_(28.57%)
3+
// #Hard #Array #Math #Greedy #2025_05_03_Time_3_ms_(98.58%)_Space_56.46_MB_(8.49%)
44

55
public class Solution {
66
public long minimumReplacement(int[] nums) {
7-
int limit = nums[nums.length - 1];
7+
int n = nums.length;
8+
int prev = nums[n - 1];
89
long ans = 0;
9-
for (int i = nums.length - 2; i >= 0; i--) {
10-
int replacements = nums[i] / limit - 1;
11-
if (nums[i] % limit != 0) {
12-
replacements++;
10+
for (int i = n - 2; i >= 0; i--) {
11+
int noOfTime = nums[i] / prev;
12+
if (nums[i] % prev != 0) {
13+
noOfTime++;
14+
prev = nums[i] / noOfTime;
1315
}
14-
ans += replacements;
15-
limit = nums[i] / (replacements + 1);
16+
ans += noOfTime - 1;
1617
}
1718
return ans;
1819
}

src/test/java/g1001_1100/s1022_sum_of_root_to_leaf_binary_numbers/SolutionTest.java

+5
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,9 @@ void sumRootToLeaf2() {
2020
TreeNode root = TreeNode.create(Collections.singletonList(0));
2121
assertThat(new Solution().sumRootToLeaf(root), equalTo(0));
2222
}
23+
24+
@Test
25+
void sumRootToLeaf3() {
26+
assertThat(new Solution().sumRootToLeaf(null), equalTo(0));
27+
}
2328
}

0 commit comments

Comments
 (0)