-
Notifications
You must be signed in to change notification settings - Fork 138
Add rust hash maps and sets #96
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
Changes from 3 commits
eb241a6
e3c22cf
dcb51d2
ba12eb1
2e40b7f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
pub fn zero_striping(matrix: &mut Vec<Vec<i32>>) { | ||
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; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use std::collections::HashSet; | ||
|
||
pub fn zero_striping_hash_sets(matrix: &mut Vec<Vec<i32>>) { | ||
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; | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
fn largest_container_brute_force(heights: Vec<i32>) -> 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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
fn next_lexicographical_sequence(s: &str) -> String { | ||
let mut letters: Vec<char> = 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The while-loop implementation looks slightly different to the python implementation |
||
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] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
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() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
fn pair_sum_sorted_brute_force(nums: Vec<i32>, target: i32) -> Vec<i32> { | ||
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![] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use std::collections::HashSet; | ||
|
||
fn triplet_sum_brute_force(nums: Vec<i32>) -> Vec<Vec<i32>> { | ||
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 {w | ||
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() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should it be
fn
instead ofpub fn
? Same forzero_striping.ts
.