Minimales en Go
This commit is contained in:
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]]
|
||||
}
|
||||
Reference in New Issue
Block a user