Algorithm to find the best combination of items under certain constraints

前端 未结 7 1589
长情又很酷
长情又很酷 2021-01-06 14:22

I\'ll try to explain the problem in the math language.
Assume I have a set of items X = {x_1, x_2, ..., x_n}. Each item of X belongs to one of

7条回答
  •  星月不相逢
    2021-01-06 14:43

    Let's think of x_i as if it's interval with integer endpoints. f1 returns true if 2 intervals do not intersect and false otherwise. f2 compares sum lengths of intervals in subsets.

    If I understand correctly, this means we can assign a value(its length) to each x_i from X. There is then no need to evaluate f2 on each possible solution / subset.

    It's very unlikely that the smallest 5 x_i form the best subset. Depending on the actual data, the best subset might be the 5 biggest intervals. So I'd suggest sorting X by value. General idea is to start with the highest x and try adding more x's (highest first) till you got 5 nonoverlapping. Most likely you will find the best subset, before even generating a fraction of all the possible subsets (depends on the specific problem of course). But in worst case this is not faster than your solution.

提交回复
热议问题