Skip to content

Improved tasks 133, 194, 1022, 2366 #1973

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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1188,7 +1188,7 @@ implementation 'com.github.javadev:leetcode-in-java:1.43'
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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
| 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
| 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
| 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

#### Udemy Dynamic Programming
Expand Down Expand Up @@ -1392,7 +1392,7 @@ implementation 'com.github.javadev:leetcode-in-java:1.43'
|-|-|-|-|-|-
| 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
| 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
| 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
| 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
| 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
| 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
| 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
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/g0101_0200/s0133_clone_graph/Solution.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package g0101_0200.s0133_clone_graph;

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

import com_github_leetcode.Node;
import java.util.ArrayList;
Expand Down
23 changes: 16 additions & 7 deletions src/main/java/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_06_28_Time_630_ms_(28.43%)_Space_3.9_MB_(71.08%)
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,10 +1,9 @@
package g1001_1100.s1022_sum_of_root_to_leaf_binary_numbers;

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

import com_github_leetcode.TreeNode;
import java.util.ArrayList;
import java.util.List;

/*
* Definition for a binary tree node.
Expand All @@ -23,31 +22,17 @@
*/
public class Solution {
public int sumRootToLeaf(TreeNode root) {
List<List<Integer>> paths = new ArrayList<>();
dfs(root, paths, new ArrayList<>());
int sum = 0;
for (List<Integer> list : paths) {
int num = 0;
for (int i : list) {
num = (num << 1) + i;
}
sum += num;
}
return sum;
return sumRootToLeaf(root, 0);
}

private void dfs(TreeNode root, List<List<Integer>> paths, List<Integer> path) {
path.add(root.val);
if (root.left != null) {
dfs(root.left, paths, path);
path.remove(path.size() - 1);
}
if (root.right != null) {
dfs(root.right, paths, path);
path.remove(path.size() - 1);
private int sumRootToLeaf(TreeNode root, int sum) {
if (root == null) {
return 0;
}
sum = 2 * sum + root.val;
if (root.left == null && root.right == null) {
paths.add(new 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 #2022_08_14_Time_10_ms_(28.57%)_Space_81.5_MB_(28.57%)
// #Hard #Array #Math #Greedy #2025_05_03_Time_3_ms_(98.58%)_Space_56.46_MB_(8.49%)

public class Solution {
public long minimumReplacement(int[] nums) {
int limit = nums[nums.length - 1];
int n = nums.length;
int prev = nums[n - 1];
long ans = 0;
for (int i = nums.length - 2; i >= 0; i--) {
int replacements = nums[i] / limit - 1;
if (nums[i] % limit != 0) {
replacements++;
for (int i = n - 2; i >= 0; i--) {
int noOfTime = nums[i] / prev;
if (nums[i] % prev != 0) {
noOfTime++;
prev = nums[i] / noOfTime;
}
ans += replacements;
limit = nums[i] / (replacements + 1);
ans += noOfTime - 1;
}
return ans;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ void sumRootToLeaf2() {
TreeNode root = TreeNode.create(Collections.singletonList(0));
assertThat(new Solution().sumRootToLeaf(root), equalTo(0));
}

@Test
void sumRootToLeaf3() {
assertThat(new Solution().sumRootToLeaf(null), equalTo(0));
}
}