# 仅显示总和小于或等于目标数的两个排列 arrays 的组合

[英]Show only the combinations of two permutated arrays that have a sum less than or equal to target number

``````    teams = [1,2,3]
drivers = [4,5,6]
team = teams.permutation(1).to_a
driver = drivers.permutation(2).to_a
array = team.product(driver)
target = 11

``````

`[[1], [4, 5]], [[1], [4, 6]], [[1], [5, 4]], [[1], [5, 6]], [[1], [6, 4]], [[1], [6, 5]], [[2], [4, 5]],`等等...

`@array = array[0].product(*array[1..-1]).select { |a| a.reduce(:+) <= target }`

``````result = array.combination(1).select{|combi| combi.sum <= target}

@array = result
``````

``````teams = [1,2,3]
drivers = [2,5,4,5,6,4,5,7]
max_driver_sum = 10
``````

``````counts = drivers.tally
#=> {2=>1, 5=>3, 4=>2, 6=>1, 7=>1}

dup_drivers, uniq_drivers = counts.partition { |_d,n| n > 1 }
.map { |arr| arr.map(&:first) }​
#=> [[5, 4], [2, 6, 7]]
``````

``````dup_drivers
#=> [5, 4]

uniq_drivers
#=> [2, 6, 7]
``````

``````counts.partition { |_d,n| n > 1 }
#=> [[[5, 3], [4, 2]], [[2, 1], [6, 1], [7, 1]]]
``````

``````dup_combos = teams.each_with_object([]) do |t,arr|
max_driver = (max_driver_sum - t)/2
dup_drivers.each do |d|
arr << [[t],[d,d]] if d <= max_driver
end
end
#=> [[[1], [4, 4]], [[2], [4, 4]]]
``````

``````all_uniq = uniq_drivers + dup_drivers
#=> [2, 6, 7, 5, 4]
``````
``````all_uniq_combos = all_uniq.combination(2).to_a
#=> [[2, 6], [2, 7], [2, 5], [2, 4], [6, 7], [6, 5],
#    [6, 4], [7, 5], [7, 4], [5, 4]]
``````
``````uniq_combos = teams.each_with_object([]) do |t,arr|
all_uniq_combos.each do |combo|
arr << [[t],combo] if combo.sum <= adj_driver_sum
end
end
#=> [[[1], [2, 6]], [[1], [2, 7]], [[1], [2, 5]], [[1], [2, 4]],
#   [[1], [5, 4]], [[2], [2, 6]], [[2], [2, 5]], [[2], [2, 4]],
#   [[3], [2, 5]], [[3], [2, 4]]]
``````

``````a1 = dup_combos + uniq_combos
#=> [[[1], [4, 4]], [[2], [4, 4]], [[1], [2, 6]], [[1], [2, 7]],
#    [[1], [2, 5]], [[1], [2, 4]], [[1], [5, 4]], [[2], [2, 6]],
#    [[2], [2, 5]], [[2], [2, 4]], [[3], [2, 5]], [[3], [2, 4]]]
``````

``````a1.sort
#=> [[[1], [2, 4]], [[1], [2, 5]], [[1], [2, 6]], [[1], [2, 7]],
#    [[1], [4, 4]], [[1], [5, 4]],
#    [[2], [2, 4]], [[2], [2, 5]], [[2], [2, 6]], [[2], [4, 4]],
#    [[3], [2, 4]], [[3], [2, 5]]]
``````

``````teams = [1,2,3]
drivers = [2,5,4,6,7]
max_driver_sum = 10
``````

``````all_uniq_combos = drivers.combination(2).to_a
#=> [[2, 5], [2, 4], [2, 6], [2, 7], [5, 4], [5, 6],
#    [5, 7], [4, 6], [4, 7], [6, 7]]

combos = teams.each_with_object([]) do |t,arr|
all_uniq_combos.each do |combo|
arr << [[t],combo] if combo.sum <= adj_driver_sum
end
end ​
#=> [[[1], [2, 5]], [[1], [2, 4]], [[1], [2, 6]], [[1], [2, 7]],
#    [[1], [5, 4]], [[2], [2, 5]], [[2], [2, 4]], [[2], [2, 6]],
#    [[3], [2, 5]], [[3], [2, 4]]]

combos.sort
#=> [[[1], [2, 4]], [[1], [2, 5]], [[1], [2, 6]], [[1], [2, 7]],
#    [[1], [5, 4]],
#    [[2], [2, 4]], [[2], [2, 5]], [[2], [2, 6]],
#    [[3], [2, 4]], [[3], [2, 5]]]
``````

``````teams = [1, 2, 3]
drivers = [2, 6, 5, 4]
team = teams.permutation(1).to_a
driver = drivers.permutation(2).to_a
array = team.product(driver)

target = 10

res = array.select {|i| i.map(&:sum).sum <= target}.compact
==> [[[1], [2, 6]], [[1], [2, 5]], [[1], [2, 4]], [[1], [6, 2]],
[[1], [5, 2]], [[1], [5, 4]], [[1], [4, 2]], [[1], [4, 5]],
[[2], [2, 6]], [[2], [2, 5]], [[2], [2, 4]], [[2], [6, 2]],
[[2], [5, 2]], [[2], [4, 2]], [[3], [2, 5]], [[3], [2, 4]],
[[3], [5, 2]], [[3], [4, 2]]]
``````

``````t1 = res.map {|i| i[0]}
d2 = res.map {|i| i[1].flatten.sort}

t1.zip(d2).uniq
==> [[[1], [2, 6]], [[1], [2, 5]], [[1], [2, 4]], [[1], [4, 5]],
[[2], [2, 6]], [[2], [2, 5]], [[2], [2, 4]], [[3], [2, 5]],
[[3], [2, 4]]]
``````

[英]Split an array of numbers to arrays whose sum is less or equal than a given number

[英]Count number of times two elements in array are less than or equal to a sum value - Javascript

2016-10-27 20:50:46 1 58   arrays

[英]Finding the number of sum combinations between two arrays that satisfy a condition

[英]Two Sorted Arrays, sum of 2 elements equal a certain number

2016-09-19 20:46:47 2 639   big-o

[英]How to find number of tuples whose sum is equal or less than a given number?

[英]Return all possible permutations and combinations of numbers from n arrays whose sum is less than m and greater than r

[英]Return the count of the number of times that the two elements in two different arrays differ by 2 or less, but are not equal

[英]Largest Subset whose sum is less than equal to a given sum

2016-12-22 14:03:35 4 6790   powerset
Java - 在排序数组中查找总和小于数字的三个数字的所有组合

[英]Java - find all combinations of three numbers in sorted array that have a sum smaller than a number

[英]finding the sum of two arrays elements that is equal to the input

2021-03-04 14:02:37 1 59   arrays