Minimales en Rust
This commit is contained in:
57
src/Rust/004.rs
Normal file
57
src/Rust/004.rs
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
|
fn minimales(values: &[Vec<i32>]) -> Vec<Vec<i32>> {
|
||||||
|
let mut res: Vec<Vec<i32>> = Vec::new();
|
||||||
|
|
||||||
|
for (index, candidate) in values.iter().enumerate() {
|
||||||
|
let candidate_set: HashSet<i32> = candidate.iter().copied().collect();
|
||||||
|
|
||||||
|
// let mut is_subset_of_another = false;
|
||||||
|
|
||||||
|
// for (other_index, other) in values.iter().enumerate() {
|
||||||
|
// let result = if index == other_index {
|
||||||
|
// false
|
||||||
|
// } else {
|
||||||
|
// let other_set: HashSet<i32> = other.iter().copied().collect();
|
||||||
|
// candidate_set.is_subset(&other_set)
|
||||||
|
// };
|
||||||
|
|
||||||
|
// if result {
|
||||||
|
// is_subset_of_another = true;
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
let is_subset_of_another = values.iter().enumerate().any(|(other_index, other)| {
|
||||||
|
if index == other_index {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
let other_set: HashSet<i32> = other.iter().copied().collect();
|
||||||
|
candidate_set.is_subset(&other_set)
|
||||||
|
});
|
||||||
|
|
||||||
|
if is_subset_of_another {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let already_present = res.iter().any(|existing| {
|
||||||
|
let existing_set: HashSet<i32> = existing.iter().copied().collect();
|
||||||
|
candidate_set == existing_set
|
||||||
|
});
|
||||||
|
|
||||||
|
if !already_present {
|
||||||
|
res.push(candidate.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
res
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let check = vec![vec![1, 3], vec![2, 3, 1], vec![3, 2, 5]];
|
||||||
|
println!("{:?}", minimales(&check)); // [[2, 3, 1], [3, 2, 5]]
|
||||||
|
|
||||||
|
let check = vec![vec![1, 3], vec![2, 3, 1], vec![3, 2, 5], vec![3, 1]];
|
||||||
|
println!("{:?}", minimales(&check)); // [[2, 3, 1], [3, 2, 5]]
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user