本文介紹了從具有重復元素的數組中隨機找到一個組合,其和等于n的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
如何從具有重復元素的array
中隨機找到一個組合,其總和等于n
。
示例
array
為[1, 2, 2, 3]
和n
為3
答案為1+2
、1+2
、3
如果randomSubsetSum(array, n)
為解決方案,則randomSubsetSum([1,2,2,3], 3)
將返回1+2
、1+2
、3
之一。注意:1+2
出現的頻率是3
的兩倍
真實場景:從題庫中隨機選擇試題
我發現了一些類似的問題和解決方案:
q:Finding all possible combinations of numbers to reach a given sum
A:solution A和solution B
q:Rank and unrank integer partition with k parts
A:solution C
缺陷
solution A
和solution B
無法隨機找到組合。solution C
不允許重復元素。
我的Java解決方案
public List<Integer> randomSubsetSum(List<Integer> list, Integer n) {
list.removeIf(e -> e > n);
int maxSum = list.stream().reduce(0, Integer::sum);
if (maxSum < n) {
throw new RuntimeException("maxSum of list lower than n!");
}
if (maxSum == n) {
return list;
}
final SecureRandom random = new SecureRandom();
// maybe helpful, not important
final Map<Integer, List<Integer>> map = list.stream().collect(Collectors.groupingBy(Function.identity()));
final List<Integer> keys = new ArrayList<>(map.keySet());
final List<Integer> answers = new ArrayList<>();
int sum = 0;
while (true) {
int keyIndex = random.nextInt(keys.size());
Integer key = keys.get(keyIndex);
sum += key;
// sum equal n
if (sum == n) {
List<Integer> elements = map.get(key);
answers.add(elements.get(random.nextInt(elements.size())));
break;
}
// sum below n
if (sum < n) {
List<Integer> elements = map.get(key);
answers.add(elements.remove(random.nextInt(elements.size())));
if (elements.isEmpty()) {
map.remove(key);
keys.remove(keyIndex);
}
continue;
}
// sum over n: exists (below = n - sum + key) in keys
int below = n - sum + key;
if (CollectionUtils.isNotEmpty(map.get(below))) {
List<Integer> elements = map.get(below);
answers.add(elements.get(random.nextInt(elements.size())));
break;
}
// sum over n: exists (over = sum - n) in answers
int over = sum - n;
int answerIndex =
IntStream.range(0, answers.size())
.filter(index -> answers.get(index) == over)
.findFirst().orElse(-1);
if (answerIndex != -1) {
List<Integer> elements = map.get(key);
answers.set(answerIndex, elements.get(random.nextInt(elements.size())));
break;
}
// Point A. BUG: may occur infinite loop
// sum over n: rollback sum
sum -= key;
// sum over n: remove min element in answer
Integer minIndex =
IntStream.range(0, answers.size())
.boxed()
.min(Comparator.comparing(answers::get))
// never occurred
.orElseThrow(RuntimeException::new);
Integer element = answers.remove((int) minIndex);
sum -= element;
if (keys.contains(element)) {
map.get(element).add(element);
} else {
keys.add(element);
map.put(element, new ArrayList<>(Collections.singleton(element)));
}
}
return answers;
}
在Point A
處,可能會出現無限循環(例如randomSubsetSum([3,4,8],13)
)或使用大量時間。如何修復此錯誤,或者是否有其他解決方案?
推薦答案
這里有一個稍微改編自解決方案A的解決方案。
from random import random
def random_subset_sum(array, target):
sign = 1
array = sorted(array)
if target < 0:
array = reversed(array)
sign = -1
# Checkpoint A
last_index = {0: [[-1,1]]}
for i in range(len(array)):
for s in list(last_index.keys()):
new_s = s + array[i]
total = 0
for index, count in last_index[s]:
total += count
if 0 < (new_s - target) * sign:
pass # Cannot lead to target
elif new_s in last_index:
last_index[new_s].append([i,total])
else:
last_index[new_s] = [[i, total]]
# Checkpoint B
answer_indexes = []
last_choice = len(array)
while -1 < last_choice:
choice = None
total = 0
for i, count in last_index[target]:
if last_choice <= i:
break
total += count
if random() <= count / total:
choice = i
target -= array[choice]
last_choice = choice
if -1 < choice:
answer_indexes.append(choice)
return [array[i] for i in reversed(answer_indexes)]
這篇關于從具有重復元素的數組中隨機找到一個組合,其和等于n的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,