Find valid assignments of integers in arrays (permutations with given order)

前端 未结 5 1427
醉梦人生
醉梦人生 2021-01-06 06:33

I am having a general problem finding a good algorithm for generating each possible assignment for some integers in different arrays.

Lets say I have n arrays and m

5条回答
  •  佛祖请我去吃肉
    2021-01-06 07:28

    It breaks down in this case to where the 2 partitions are. There are 4 possible locations so that would be 16 combinations but it isn't because you remove "duplicates". A bit like domino tiles. You have 4 "doubles" here and the 12 singles reduce to 6 so you have 10 combinations.

    You can generate it selecting the first one, then generating the second as >= the first.

    The first can be 0, 1, 2 or 3. 0 means it appears before the 1. 3 means it appears after the 3.

    In your 10 solutions above the partitions are at:

    1: 3 and 3 2: 0 and 3 3: 0 and 0 4: 2 and 3 5: 2 and 2 6: 0 and 2 7: 1 and 3 8: 1 and 1 9: 0 and 1 10: 1 and 2

    If you generated in algorithmic order you would probably produce them 0 and 0, 0 and 1, 0 and 2, 0 and 3, 1 and 1, 1 and 2, 1 and 3, 2 and 2, 2 and 3, 3 and 3 although you could of course do them in reverse order.

    In your examples above look at the positions of the commas and the number immediately to their left. If there are no numbers immediately to their left then it is 0.

提交回复
热议问题