从具有重复元素的数组中随机找到一个组合,其和等于n

原学程将引见从具备反复元素的数组中随机找到1个组开,其以及即是n的处置办法,这篇学程是从其余处所瞅到的,而后减了1些海外法式员的疑问与解问,愿望能对于您有所赞助,佳了,上面开端进修吧。

从具有重复元素的数组中随机找到一个组合,其和等于n 教程 第1张

成绩描写

怎样从具备反复元素的array中随机找到1个组开,其总以及即是n

示例

    array[一, 二, 二, 三]以及n

    谜底为一+二一+二

    假如randomSubsetSum(array, n)为处理计划,则randomSubsetSum([一,二,二,三], 三)将前往一+二一+二之1。留意:一+二涌现的频次是的二倍

    真虚场景:从题库中随机选择试题

我发明了1些相似的成绩息争决计划:

    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(⑴);
  if (answerIndex != ⑴) {
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([三,四,8],一三))或者应用年夜质时光。怎样修复此毛病,或许能否有其余处理计划?

推举谜底

这里有1个略微改编自处理计划A的处理计划。

from random import random

def random_subset_sum(array, target):
 sign = 一
 array = sorted(array)
 if target < 0:
  array = reversed(array)
  sign = ⑴
 # Checkpoint A

 last_index = {0: [[⑴,一]]}
 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 ⑴ < 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 ⑴ < choice:
answer_indexes.append(choice)

 return [array[i] for i in reversed(answer_indexes)]

佳了闭于从具备反复元素的数组中随机找到1个组开,其以及即是n的学程便到这里便停止了,愿望趣模板源码网找到的这篇技巧文章能赞助到年夜野,更多技巧学程不妨在站内搜刮。