Compare commits
4 Commits
aa20952dc2
...
0a5969b2e0
| Author | SHA1 | Date | |
|---|---|---|---|
| 0a5969b2e0 | |||
| c1a5ce4494 | |||
| 55705a796a | |||
| aa176f7038 |
70
src/Go/004.go
Normal file
70
src/Go/004.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func toSet(values []int) map[int]struct{} {
|
||||
set := make(map[int]struct{}, len(values))
|
||||
for _, value := range values {
|
||||
set[value] = struct{}{}
|
||||
}
|
||||
return set
|
||||
}
|
||||
|
||||
func isSubset(left map[int]struct{}, right map[int]struct{}) bool {
|
||||
for value := range left {
|
||||
if _, ok := right[value]; !ok {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func minimales(values [][]int) [][]int {
|
||||
res := [][]int{}
|
||||
|
||||
for index, candidate := range values {
|
||||
candidateSet := toSet(candidate)
|
||||
|
||||
isSubsetOfAnother := false
|
||||
for otherIndex, other := range values {
|
||||
if index == otherIndex {
|
||||
continue
|
||||
}
|
||||
|
||||
otherSet := toSet(other)
|
||||
if isSubset(candidateSet, otherSet) {
|
||||
isSubsetOfAnother = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if isSubsetOfAnother {
|
||||
continue
|
||||
}
|
||||
|
||||
alreadyPresent := false
|
||||
for _, existing := range res {
|
||||
existingSet := toSet(existing)
|
||||
if len(candidateSet) == len(existingSet) &&
|
||||
isSubset(candidateSet, existingSet) {
|
||||
alreadyPresent = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !alreadyPresent {
|
||||
res = append(res, candidate)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
func main() {
|
||||
check := [][]int{[]int{1, 3}, []int{2, 3, 1}, []int{3, 2, 5}}
|
||||
fmt.Println(minimales(check)) // [[2, 3, 1], [3, 2, 5]]
|
||||
|
||||
check = [][]int{[]int{1, 3}, []int{2, 3, 1}, []int{3, 2, 5}, []int{3, 1}}
|
||||
fmt.Println(minimales(check)) // [[2, 3, 1], [3, 2, 5]]
|
||||
}
|
||||
27
src/Julia/004.jl
Normal file
27
src/Julia/004.jl
Normal file
@@ -0,0 +1,27 @@
|
||||
function minimales(lst)
|
||||
res = Vector{Vector{Int}}()
|
||||
for (index, canditate) in enumerate(lst)
|
||||
canditate_set = Set(canditate)
|
||||
|
||||
is_subset_of_another = any(
|
||||
index != other_index && canditate_set <= Set(other)
|
||||
for (other_index, other) in enumerate(lst)
|
||||
)
|
||||
|
||||
if is_subset_of_another
|
||||
continue
|
||||
end
|
||||
|
||||
if !any(canditate_set == Set(existing) for existing in res)
|
||||
push!(res, canditate)
|
||||
end
|
||||
|
||||
end
|
||||
return res
|
||||
end
|
||||
|
||||
check = [[1, 3], [2, 3, 1], [3, 2, 5]]
|
||||
println(minimales(check)) # [[2, 3, 1], [3, 2, 5]]
|
||||
|
||||
check = [[1, 3], [2, 3, 1], [3, 2, 5], [3, 1]]
|
||||
println(minimales(check)) # [[2, 3, 1], [3, 2, 5]]
|
||||
22
src/Python/004.py
Normal file
22
src/Python/004.py
Normal file
@@ -0,0 +1,22 @@
|
||||
def minimales(lst: list[list[int]]) -> list[list[int]]:
|
||||
res = []
|
||||
for index, candidate in enumerate(lst):
|
||||
candidate_set = set(candidate)
|
||||
|
||||
is_subset_of_another = any(
|
||||
index != other_index and candidate_set <= set(other)
|
||||
for other_index, other in enumerate(lst)
|
||||
)
|
||||
if is_subset_of_another:
|
||||
continue
|
||||
|
||||
if not any(candidate_set == set(existing) for existing in res):
|
||||
res.append(candidate)
|
||||
|
||||
return res
|
||||
|
||||
check = [[1, 3], [2, 3, 1], [3, 2, 5]]
|
||||
print(minimales(check)) # [[2, 3, 1], [3, 2, 5]]
|
||||
|
||||
check = [[1, 3], [2, 3, 1], [3, 2, 5], [3, 1]]
|
||||
print(minimales(check)) # [[2, 3, 1], [3, 2, 5]]
|
||||
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