-
Notifications
You must be signed in to change notification settings - Fork 126
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
Open
Peponks9
wants to merge
4
commits into
ByteByteGoHq:main
Choose a base branch
from
Peponks9:feature/add-rust-hash-maps-and-sets
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
eb241a6
Add missing Rust implementations for Two Pointers
e3c22cf
Add missing comment to triplet_sum_brute_force.rs
dcb51d2
feat: Add first two Rust implementations for Hash Maps and Sets algor…
ba12eb1
feat: Add geometric_sequence_triplets and longest_chain_of_consecutiv…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use std::collections::HashMap; | ||
|
||
fn geometric_sequence_triplets(nums: Vec<i32>, r: i32) -> i32 { | ||
// Use HashMap to track frequencies, with default value of 0 | ||
let mut left_map: HashMap<i32, i32> = HashMap::new(); | ||
let mut right_map: HashMap<i32, i32> = 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 | ||
} |
23 changes: 23 additions & 0 deletions
23
rust/Hash Maps and Sets/longest_chain_of_consecutive_numbers_brute_force.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
fn longest_chain_of_consecutive_numbers_brute_force(nums: Vec<i32>) -> 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 | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Please make the comment closer to the original comment
