Skip to content

Commit

Permalink
Reto mouredev#28 - Go
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianortiz committed Oct 9, 2023
1 parent e7d700d commit dea4a31
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions Retos/Reto #38 - LAS SUMAS [Media]/go/cristianortiz.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

import (
"fmt"
"sort"
)

func sumCombinations(candidates []int, target int) [][]int {
//slice final de resultados
var result [][]int
//slice para ir guardando combinaciones validas
var combination []int
//ordenar los elementos del slice candidates
sort.Ints(candidates)

//funcion recursiva para encontrar combinaciones validas
//start represent el indice inicial para comenzar la busqueda
//remain is el valor actual que se esta comparando con el target para verificar la suma
var backtrack func(start, remain int)
backtrack = func(start, remain int) {

//si remain es 0 hemos encontrado una combinacion valida, agregarla al slice de resultados
if remain == 0 {
result = append(result, append([]int{}, combination...))
return
}
//loop principal
for i := start; i < len(candidates); i++ {

//si target es un valor negativo o se ha alcanzado el final del slice candidates, no hay solucion
//se interrumpe el ciclo
if target < 0 || start == len(candidates) {
break
}

//verifico
if i > start && candidates[i] == candidates[i-1] {
continue
}
//añadir el valor del candidato actual al slice de posibles combinaciones validas
combination = append(combination, candidates[i])
//llamada a la funcion recursiva
backtrack(i+1, remain-candidates[i])
//remover del slice de combinaciones el ultimo valor añadido
//NOTA: aqui usamos la sintaxis de go para manipular los indices de un slice, no existe funcion pop()
combination = combination[:len(combination)-1]
}
}

//llamada a la funcion recursiva con parametros iniciales
backtrack(0, target)
return result
}

func main() {
candidates := []int{1, 2, 1, 1, 1, 1, 3, 1}
target := 4
combination := sumCombinations(candidates, target)
fmt.Println(combination)
}

0 comments on commit dea4a31

Please sign in to comment.