carlosbf
1 min readJan 9, 2019

--

Hi please disregard my previous comment. I thought the question was “why?” which is ambiguous.

Answering the real question:
- Why i%k? How did you come to this solution?

  • The friends have to buy all flowers mimizing the cost.
  • The problem becomes how to assign friends to flowers.
  • This is easy, we just buy the cheapest flower first.
  • Thats why we sort by price
  • Now to minimize the cost of purchasing all flowers which increases with each additional flower purchased. We assign the ith flower to the i%k th friend.

--

--

carlosbf
carlosbf

Written by carlosbf

Software developer that publishes his interview prep and leetcode hobby on this blog

No responses yet