From 0a5969b2e05666639e3b91c45f967d62ab11506b Mon Sep 17 00:00:00 2001 From: daviddoji Date: Sun, 17 May 2026 17:10:31 +0200 Subject: [PATCH] Minimales en Go --- src/Go/004.go | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/Go/004.go diff --git a/src/Go/004.go b/src/Go/004.go new file mode 100644 index 0000000..36a57f7 --- /dev/null +++ b/src/Go/004.go @@ -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]] +}