c++ - Maximum children happiness -


this question has answer here:

given n different candies , m children. each of children demands k[i] different candies , only happy iff different candies demanded.

now want maximize number children happy. how should distribute candies?

example: let's have n=4 candies , m=3 children:

  • 1st child requires 2 (k[1]) candies are: 1 , 2
  • 2nd child requires 2 (k[2]) candies are: 2 , 3
  • 3rd child requires 2 (k[3]) candies are: 3 , 4

the answer here 2 can @ best make 1st , 3rd child happy.

my attempt:

give candies children in order of amounts require happy (i.e. k[i]). in other words, should give candy child if have made happy children demand less, , every time give candy 1 of them have give them whole amount require.

is solution correct?

yes, wrong. consider:

  1. wants candies 1, 2, 3, , 4.

  2. wants candies 1, 5, 6, 7, 8, , 9.

  3. wants candies 2, 10, 11, 12, 13, 14, , 15.

  4. wants candies 3, 16, 17, 18, 19, 20, , 21.

  5. wants candies 4, 22, 23, 24, 25, 26, , 27.

your algorithm make child 1 happy. can make 2, 3, 4, , 5 happy.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -