algorithm to determine minimum payments amongst a group

后端 未结 8 733
庸人自扰
庸人自扰 2020-12-05 12:09

The Problem

I was recently asked to calculate the money owed amongst a group of people who went on a trip together and came upon an interesting problem: given that

8条回答
  •  醉话见心
    2020-12-05 12:14

    My opinion: You're making this overly complicated.

    Think of it as a "pool" of money, and lose the relationships altogether:

    Instead of:

    • Mike owes John 100
    • John owes Rachel 200
    • Mike owes Rachel 400

    The algorithm only has to think:

    • Mike owes 100
    • John is owed 100
    • John owes 200
    • Rachel is owed 200
    • Mike owes 400
    • Rachel is owed 400

    Netting this:

    • Mike owes 500
    • John owes 100
    • Rachel is owed 600

    Separate this into a list of "givers" and "receivers". Each giver on the list will go through the list of receivers, giving each receiver what they need until the giver has payed up. When a receiver receives everything they need, they go off the list.

    Later Edit

    As other posters have observed, this simplifies the problem. However, there might be an optimal ordering of the "givers" and "receivers" lists, but we haven't yet identified a straightforward way to determine this ordering.

提交回复
热议问题