From eb241a61f521ae4696a63869ff92bde180342efc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Vel=C3=A1zquez?= Date: Sat, 2 Aug 2025 18:41:29 -0600 Subject: [PATCH 1/4] Add missing Rust implementations for Two Pointers - Add largest_container_brute_force.rs - Add next_lexicographical_sequence.rs - Add pair_sum_sorted_brute_force.rs - Add triplet_sum_brute_force.rs Completes the Rust Two Pointers implementation to match Python3 structure --- .../largest_container_brute_force.rs | 13 +++++++ .../next_lexicographical_sequence.rs | 35 +++++++++++++++++++ .../pair_sum_sorted_brute_force.rs | 12 +++++++ rust/Two Pointers/triplet_sum_brute_force.rs | 22 ++++++++++++ 4 files changed, 82 insertions(+) create mode 100644 rust/Two Pointers/largest_container_brute_force.rs create mode 100644 rust/Two Pointers/next_lexicographical_sequence.rs create mode 100644 rust/Two Pointers/pair_sum_sorted_brute_force.rs create mode 100644 rust/Two Pointers/triplet_sum_brute_force.rs diff --git a/rust/Two Pointers/largest_container_brute_force.rs b/rust/Two Pointers/largest_container_brute_force.rs new file mode 100644 index 0000000..70bd0ea --- /dev/null +++ b/rust/Two Pointers/largest_container_brute_force.rs @@ -0,0 +1,13 @@ +fn largest_container_brute_force(heights: Vec) -> i32 { + let n = heights.len(); + let mut max_water = 0; + + // Find the maximum amount of water stored between all pairs of lines. + for i in 0..n { + for j in (i + 1)..n { + let water = heights[i].min(heights[j]) * (j as i32 - i as i32); + max_water = max_water.max(water); + } + } + max_water +} diff --git a/rust/Two Pointers/next_lexicographical_sequence.rs b/rust/Two Pointers/next_lexicographical_sequence.rs new file mode 100644 index 0000000..d06c233 --- /dev/null +++ b/rust/Two Pointers/next_lexicographical_sequence.rs @@ -0,0 +1,35 @@ +fn next_lexicographical_sequence(s: &str) -> String { + let mut letters: Vec = s.chars().collect(); + + // Locate the pivot, which is the first character from the right that breaks + // non-increasing order. Start searching from the second-to-last position. + let mut pivot = letters.len() - 2; + while pivot < letters.len() && letters[pivot] >= letters[pivot + 1] { + if pivot == 0 { + break; + } + pivot -= 1; + } + + // If pivot is not found, the string is already in its largest permutation. In + // this case, reverse the string to obtain the smallest permutation. + if pivot == 0 && letters[pivot] >= letters[pivot + 1] { + letters.reverse(); + return letters.into_iter().collect(); + } + + // Find the rightmost successor to the pivot. + let mut rightmost_successor = letters.len() - 1; + while letters[rightmost_successor] <= letters[pivot] { + rightmost_successor -= 1; + } + + // Swap the rightmost successor with the pivot to increase the lexicographical + // order of the suffix. + letters.swap(pivot, rightmost_successor); + + // Reverse the suffix after the pivot to minimize its permutation. + letters[(pivot + 1)..].reverse(); + + letters.into_iter().collect() +} diff --git a/rust/Two Pointers/pair_sum_sorted_brute_force.rs b/rust/Two Pointers/pair_sum_sorted_brute_force.rs new file mode 100644 index 0000000..de77dd8 --- /dev/null +++ b/rust/Two Pointers/pair_sum_sorted_brute_force.rs @@ -0,0 +1,12 @@ +fn pair_sum_sorted_brute_force(nums: Vec, target: i32) -> Vec { + let n = nums.len(); + + for i in 0..n { + for j in (i + 1)..n { + if nums[i] + nums[j] == target { + return vec![i as i32, j as i32]; + } + } + } + vec![] +} diff --git a/rust/Two Pointers/triplet_sum_brute_force.rs b/rust/Two Pointers/triplet_sum_brute_force.rs new file mode 100644 index 0000000..08ac899 --- /dev/null +++ b/rust/Two Pointers/triplet_sum_brute_force.rs @@ -0,0 +1,22 @@ +use std::collections::HashSet; + +fn triplet_sum_brute_force(nums: Vec) -> Vec> { + let n = nums.len(); + let mut triplets = HashSet::new(); + + // Iterate through the indexes of all triplets. + for i in 0..n { + for j in (i + 1)..n { + for k in (j + 1)..n { + if nums[i] + nums[j] + nums[k] == 0 { + // Sort the triplet before including it in the hash set. + let mut triplet = vec![nums[i], nums[j], nums[k]]; + triplet.sort(); + triplets.insert(triplet); + } + } + } + } + + triplets.into_iter().collect() +} From e3c22cf16f76271117c85f1471afd9257bae3944 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Vel=C3=A1zquez?= Date: Mon, 4 Aug 2025 09:16:36 -0600 Subject: [PATCH 2/4] Add missing comment to triplet_sum_brute_force.rs --- rust/Two Pointers/triplet_sum_brute_force.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rust/Two Pointers/triplet_sum_brute_force.rs b/rust/Two Pointers/triplet_sum_brute_force.rs index 08ac899..20a3971 100644 --- a/rust/Two Pointers/triplet_sum_brute_force.rs +++ b/rust/Two Pointers/triplet_sum_brute_force.rs @@ -2,10 +2,11 @@ use std::collections::HashSet; fn triplet_sum_brute_force(nums: Vec) -> Vec> { let n = nums.len(); + // Use a hash set to ensure we don't add duplicate triplets. let mut triplets = HashSet::new(); // Iterate through the indexes of all triplets. - for i in 0..n { + for i in 0..n {w for j in (i + 1)..n { for k in (j + 1)..n { if nums[i] + nums[j] + nums[k] == 0 { From dcb51d2c556562c81ba1ac95d1acc972fa1df527 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Vel=C3=A1zquez?= Date: Mon, 4 Aug 2025 22:24:40 -0600 Subject: [PATCH 3/4] feat: Add first two Rust implementations for Hash Maps and Sets algorithms --- rust/Hash Maps and Sets/zero_striping.rs | 49 +++++++++++++++++++ .../zero_striping_hash_sets.rs | 34 +++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 rust/Hash Maps and Sets/zero_striping.rs create mode 100644 rust/Hash Maps and Sets/zero_striping_hash_sets.rs diff --git a/rust/Hash Maps and Sets/zero_striping.rs b/rust/Hash Maps and Sets/zero_striping.rs new file mode 100644 index 0000000..fa025d0 --- /dev/null +++ b/rust/Hash Maps and Sets/zero_striping.rs @@ -0,0 +1,49 @@ +pub fn zero_striping(matrix: &mut Vec>) { + if matrix.is_empty() || matrix[0].is_empty() { + return; + } + + let m = matrix.len(); + let n = matrix[0].len(); + + // Check if the first row initially contains a zero + let first_row_has_zero = matrix[0].iter().any(|&x| x == 0); + + // Check if the first column initially contains a zero + let first_col_has_zero = matrix.iter().any(|row| row[0] == 0); + + // Use the first row and column as markers. If an element in the + // submatrix is zero, mark its corresponding row and column in the + // first row and column as 0. + for r in 1..m { + for c in 1..n { + if matrix[r][c] == 0 { + matrix[0][c] = 0; + matrix[r][0] = 0; + } + } + } + + // Update the submatrix using the markers in the first row and column + for r in 1..m { + for c in 1..n { + if matrix[0][c] == 0 || matrix[r][0] == 0 { + matrix[r][c] = 0; + } + } + } + + // If the first row had a zero initially, set all elements in the first row to zero + if first_row_has_zero { + for c in 0..n { + matrix[0][c] = 0; + } + } + + // If the first column had a zero initially, set all elements in the first column to zero + if first_col_has_zero { + for r in 0..m { + matrix[r][0] = 0; + } + } +} diff --git a/rust/Hash Maps and Sets/zero_striping_hash_sets.rs b/rust/Hash Maps and Sets/zero_striping_hash_sets.rs new file mode 100644 index 0000000..1149d2d --- /dev/null +++ b/rust/Hash Maps and Sets/zero_striping_hash_sets.rs @@ -0,0 +1,34 @@ +use std::collections::HashSet; + +pub fn zero_striping_hash_sets(matrix: &mut Vec>) { + if matrix.is_empty() || matrix[0].is_empty() { + return; + } + + let m = matrix.len(); + let n = matrix[0].len(); + let mut zero_rows = HashSet::new(); + let mut zero_cols = HashSet::new(); + + // Pass 1: Traverse through the matrix to identify the rows and + // columns containing zeros and store their indexes in the + // appropriate hash sets. + for r in 0..m { + for c in 0..n { + if matrix[r][c] == 0 { + zero_rows.insert(r); + zero_cols.insert(c); + } + } + } + + // Pass 2: Set any cell in the matrix to zero if its row index is + // in 'zero_rows' or its column index is in 'zero_cols'. + for r in 0..m { + for c in 0..n { + if zero_rows.contains(&r) || zero_cols.contains(&c) { + matrix[r][c] = 0; + } + } + } +} From ba12eb1215553176efe01793b9bb47681fbd7be3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Vel=C3=A1zquez?= Date: Wed, 6 Aug 2025 12:37:04 -0600 Subject: [PATCH 4/4] feat: Add geometric_sequence_triplets and longest_chain_of_consecutive_numbers_brute_force Rust implementations --- .../geometric_sequence_triplets.rs | 32 +++++++++++++++++++ ...hain_of_consecutive_numbers_brute_force.rs | 23 +++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 rust/Hash Maps and Sets/geometric_sequence_triplets.rs create mode 100644 rust/Hash Maps and Sets/longest_chain_of_consecutive_numbers_brute_force.rs diff --git a/rust/Hash Maps and Sets/geometric_sequence_triplets.rs b/rust/Hash Maps and Sets/geometric_sequence_triplets.rs new file mode 100644 index 0000000..055de93 --- /dev/null +++ b/rust/Hash Maps and Sets/geometric_sequence_triplets.rs @@ -0,0 +1,32 @@ +use std::collections::HashMap; + +fn geometric_sequence_triplets(nums: Vec, r: i32) -> i32 { + // Use HashMap to track frequencies, with default value of 0 + let mut left_map: HashMap = HashMap::new(); + let mut right_map: HashMap = HashMap::new(); + let mut count = 0; + + // Populate 'right_map' with the frequency of each element in the array + for x in &nums { + *right_map.entry(*x).or_insert(0) += 1; + } + + // Search for geometric triplets that have x as the center + for x in nums { + // Decrement the frequency of x in 'right_map' since x is now being + // processed and is no longer to the right + *right_map.get_mut(&x).unwrap() -= 1; + + if x % r == 0 { + let left_val = *left_map.get(&(x / r)).unwrap_or(&0); + let right_val = *right_map.get(&(x * r)).unwrap_or(&0); + count += left_val * right_val; + } + + // Increment the frequency of x in 'left_map' since it'll be a part of the + // left side of the array once we iterate to the next value of x + *left_map.entry(x).or_insert(0) += 1; + } + + count +} diff --git a/rust/Hash Maps and Sets/longest_chain_of_consecutive_numbers_brute_force.rs b/rust/Hash Maps and Sets/longest_chain_of_consecutive_numbers_brute_force.rs new file mode 100644 index 0000000..1b64775 --- /dev/null +++ b/rust/Hash Maps and Sets/longest_chain_of_consecutive_numbers_brute_force.rs @@ -0,0 +1,23 @@ +fn longest_chain_of_consecutive_numbers_brute_force(nums: Vec) -> i32 { + if nums.is_empty() { + return 0; + } + + let mut longest_chain = 0; + + // Look for chains of consecutive numbers that start from each number + for num in &nums { + let mut current_num = *num; + let mut current_chain = 1; + + // Continue to find the next consecutive numbers in the chain + while nums.contains(&(current_num + 1)) { + current_num += 1; + current_chain += 1; + } + + longest_chain = longest_chain.max(current_chain); + } + + longest_chain +}