K个产品数组

时间:2019-05-29 17:08:18

标签: algorithm divide-and-conquer

我正在研究算法问题。您有一个数组编号,数组t的大小,编号number_of_elements和编号multiplication_value。您必须找到该数组元素的number_of_elements个索引的任何集合,该乘积将等于multiplication_value。可以保证存在这样的索引集

该问题看起来像2个和,但我无法将其推断为我的情况。 我已经尝试过O(n)的朴素算法,但是当数组中的第一个数字不正确时,它会失败。我认为这里有一种使用递归的方法。我想这是一个众所周知的问题,但是我找不到解决方法

示例:

t = 7
number_of_elements = 2
multiplication_value = 27
numbers = [9,1,1,27,3,27,3]

示例:

1 3

我的代码提示:


    def return_index_values(numbers,multiplication_value,number_of_elements):
    cur_number = int(multiplication_value)
    list_of_indexes = []
    values = []
    for i in range(len(numbers)):
        if ((cur_number == 1) and (len(values) == number_of_elements)):
            print(values)
            #finishing if everything worked
            break
        else:               
            if (cur_number % int(numbers[i]) == 0):
                if(len(values) < number_of_elements):
            #pushing values if possible
                    values.append(int(numbers[i]))
                    list_of_indexes.append(i)
                    cur_number = int(cur_number / int(numbers[i]))
                    print(cur_number)
                else:
                    pass
            if(len(values) == number_of_elements):
                if mult_check(values,int(multiplication_value)):
               #mult_check checks if the array's element multiplication gives a value
                    break
                else:
               #started dealing with bad cases, but it doesn't work properly
                    values.sort()
                    val_popped = values.pop()
                    cur_number = cur_number * val_popped 

我的代码不正确

 numbers = [9,3,1,27,3,27,3]

2 个答案:

答案 0 :(得分:0)

您可以使用itertools.combinations()(https://www.geeksforgeeks.org/itertools-combinations-module-python-print-possible-combinations/)以所有可能的方式从列表中选择number_of_elements个条目,然后检查每个条目是否与所需数量相乘。

答案 1 :(得分:0)

这是一种实现。不一定是最好的解决方案,但是它使您对如何完成操作有所了解。

它首先通过保留索引信息的元素对numbers进行排序。然后执行递归调用。

number_of_elements = 2
multiplication_value = 27
numbers = [9,1,1,27,3,27,3]

def preprocess(numbers, multiplication_value, number_of_elements):
    l = []
    for i, num in enumerate(numbers):
        l.append((num, i))
    return sorted(l, key = lambda tup: tup[0])

def subroutine(numbers, multiplication_value, number_of_elements, idx_start, result):
    if idx_start >= len(numbers):
        return False
    if number_of_elements == 0:
        return True if multiplication_value == 1 else False
    for i in range(idx_start, len(numbers)):
        num = numbers[i][0]
        if num <= multiplication_value:
            if multiplication_value % num == 0:
                idx = numbers[i][1]
                result.append(idx)
                found = subroutine(numbers, multiplication_value / num, number_of_elements - 1, i + 1, result)
                if not found:
                    del result[-1]
                else:
                    return True
        else:
            return False
    return False

result = []
processed_numbers = preprocess(numbers, multiplication_value, number_of_elements)
subroutine(processed_numbers, multiplication_value, number_of_elements, 0, result)
print(result)