For example, in the array: {code...}, find the array element: {code...} for the sum of 100, such as array:
var arr = [99.1, 92.2, 60, 50, 49.5, 45.7, 25.1, 20, 17.4, 13, 10, 7, 2.1, 2, 1];
Find the array element with the sum of 100:
[60,20,10,7,2,1]
Reply content:
For example, array:
var arr = [99.1, 92.2, 60, 50, 49.5, 45.7, 25.1, 20, 17.4, 13, 10, 7, 2.1, 2, 1];
Find the array element with the sum of 100:
[60,20,10,7,2,1]
Function f ($ n, $ arr) {// $ n is the target number, and $ arr is an array composed of numbers if (empty ($ arr) return false; // if the array has no elements, return if (in_array ($ n, $ arr) return [$ n]; // if the expected value already exists, directly return the value foreach ($ arr as $ k => $ v) {// traverse the array if ($ v> $ n) continue; // It is larger than the specified number, through $ copy = $ arr; // copy the array unset ($ copy [$ k]); // remove the selected number from the copy array $ next = f ($ n-$ v, $ copy); // recursively calculate if (! Empty ($ next) return array_merge ([$ v], $ next); // merge result set} return false; // not found} $ arr = [99.1, 92.2, 60, 50, 49.5, 45.7, 25.1, 20, 7.4, 13, 10, 7, 2.1, 2, 1]; $ data = f (100, $ arr ); print_r ($ data); // Array ([0] => 60 [1] => 20 [2] => 13 [3] => 7) $ data = f (105, $ arr); print_r ($ data ); // Array ([0] => 60 [1] => 20 [2] => 13 [3] => 10 [4] => 2)
Coming Python version:
def subsetsum(elements, target): if target==0: return True, [] elif not elements or target < 0: return False, None result, subset = subsetsum(elements[:-1], target-elements[-1]) return (True, subset + [elements[-1]]) if result else subsetsum(elements[:-1], target)
The idea is very simple. when I want to askelements
Can I add?target
There are only two possibilities:
I want to useelement[-1]
Can be added outtarget
-> I want to be able to renewelements[:-1]
Add outtarget-elements[-1]
Yes
I do not need to useelement[-1]
You can addtarget
-> I want to be able to renewelements[:-1]
Add outtarget
Yes
Boundary condition is:
Whentarget
For0
It means that I can add anything without any need, soreturn True, []
Whenelements
Null ortarget
When the parameter value is set, it indicates that the value is never added.return False, None
Zookeeper:
elements = [99.1, 92.2, 60, 50, 49.5, 45.7, 25.1, 20, 17.4, 13, 10, 7, 2.1, 2, 1]target = 100result, subset = subsetsum(elements, target)print(result, subset)
Result:
True [60, 20, 10, 7, 2, 1]
In other words, I think this topic is very familiar, and it will be more interesting if I want to test the speed to solve it.
I have done some research in this area, and I have raised a problem of transformation. you can think about it as follows:
Today, we have set a set of multiple duplicates (representing an integer that is OK), which is called elements
In order to assign another integer multiple duplicatestargets
, Explain whether there are several sub-Multi-replica sets. the elements of each sub-Multi-replica set and exactly one element intargets
Target.
There is no difference in definition. I have provided an example:
elements = (1,4,6,4,1)targets = (5,10,1)
This example has a solution:
(1,4) -> 5(4,6) -> 10(1) -> 1
Note thatelements
The element in can only be used once!
Problem combination. Let's take a look at the original question of leetcode:
Function t100 (array) {var tempArray = array. concat ([]). sort (function (a, B) {return a-B;}); var temp = 0; var result = []; for (var I = 0; I
100) {tempArray. length = I ;}for (var I = 0; I
Recursive nested loop