gpt4 book ai didi

algorithm - UTXO选择策略

转载 作者:行者123 更新时间:2023-12-01 22:33:29 25 4
gpt4 key购买 nike

在选择UTXO时,我需要应用特定的策略。
该策略应尽可能减少utxo的使用。
应当设定此策略的边界,理想情况下,应减少utxo的数量,直到比率达到10倍。
为了使问题更简单,我们假设有一个整数列表:= [] int {},我需要在以下位置找到元素“target”:
list [index] = target,如果这样的元素不存在,那么我需要从 slice 中找到第一个大于target的元素,但需要小于等于target * 10
如果找不到该元素,则需要找到两个元素x,y,其中:
x + y =目标,如果不存在此类元素,我需要从 slice 中找到前两个大于目标的元素,但需要<=目标* 10
如果找不到此类元素,则需要找到三个元素x,y,z,其中:
x + y + z =目标,如果不存在这样的元素,我需要从 slice 中找到前三个元素,这些元素大于目标但需要<=目标* 10
如果找不到这三个元素,则需要找到四个,五个...直至len(list)。
范例1:

target = 6
list := []int {1,2, 6, 10}
result = list[2] = 6
范例2:
target = 6
list := []int {1,2, 3, 10}
result = list[3] = 10
范例3:
target = 6
list := []int {1,2, 3, 10}
result = list[3] = 10
范例4:
target = 6
list := []int {1,3, 3, 61}
result = list[1] + list[2]= 6
请参阅下面的测试用例,我需要通过递归或某种方式进行改进才能获得通用解决方案:
func Test_SelectUtxo(t *testing.T){
x := 6

list := []int{1, 2, 3, 64, 65, 62, 62, 62, 61, 59}

fmt.Println("ONE = x")
for i := 0; i < len(list) - 1; i ++ {
if list[i] == x {
fmt.Println(i)
break
}
}

fmt.Println("ONE <= x*10")
for i := 0; i < len(list); i ++ {
if list[i] > x {
if list[i] <= x*10 && list[i] > x {
fmt.Println(list[i])
break
}
}
}


fmt.Println("TWO = x")
out:
for i := 0; i < len(list) - 1; i ++ {
for j:=i + 1; j < len(list); j ++ {
if list[i] + list[j] == x {
fmt.Println(i)
fmt.Println(j)
break out
}
}
}

fmt.Println()

fmt.Println("TWO <= x*10")
out1:
for i := 0; i < len(list) - 1; i ++ {
for j:=i + 1; j < len(list); j ++ {
if list[i] + list[j] <= x*10 && list[i] + list[j] > x {
fmt.Println(i)
fmt.Println(j)
break out1
}
}
}

fmt.Println()

fmt.Println("THREE = x")
out2:
for i := 0; i < len(list) - 2; i ++ {
for j:=i + 1; j < len(list) - 1; j ++ {
for k:= j + 1; k < len(list); k ++ {
if list[i] + list[j] + list[k] == x {
fmt.Println(i)
fmt.Println(j)
fmt.Println(k)
break out2
}
}
}
}

}

最佳答案

一种解决方案:

  • 设置size = 1
  • 使用递归(以下代码段中的函数名称= getCombination )来获取输入数组中size元素的所有组合。
  • 检查每个组合是否满足 0-> i 的要求,如果是,则返回它(完成)
  • (如果组合都不匹配),则size++,然后转到步骤2

  • 代码段:
    import (
    "fmt"
    )
    var combination = []int{}
    func GetCombination(src []int,size int, offset int) [][]int { // get all combinations for **size** elements in the elements of src array
    result := [][]int{}
    if size == 0 {
    temp := make([]int, len(combination))
    copy(temp, combination)
    return append(result, temp)
    }
    for i:=offset; i<=len(src) - size; i++ {
    combination = append(combination, src[i])
    temp := GetCombination(src, size-1, i+1)
    result = append(result, temp...)
    combination = combination[:len(combination)-1]
    }
    return result[:]
    }
    func sum(items []int) int {
    total := 0
    for _, v := range items {
    total += v
    }
    return total
    }
    func GetBestPair(items []int, target int) []int {
    for i := 1; i < len(items)+1; i++ {
    result := GetCombination(items, i, 0) // get all possible combinations for 1 -> len(items) elements of Array=items
    // fmt.Println("Combinations for ", i, " elements:", result)
    for j := 0; j < len(result); j++ {
    total := sum(result[j])
    if total < target {
    continue
    }
    if total == target {
    return result[j]
    }
    if total < target*10 {
    return result[j]
    }
    }
    }
    return []int{}
    }

    func main () {
    fmt.Println("Result", GetBestPair([]int{1, 3, 3, 61}, 6))
    }
    上面的测试案例的输出
    Combinations for  1  elements: [[1] [3] [3] [61]]
    Combinations for 2 elements: [[1 3] [1 3] [1 61] [3 3] [3 61] [3 61]]
    Result: [3 3]

    关于algorithm - UTXO选择策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63624059/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com