We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Algorithms
- Greedy
- Greedy Florist
- Discussions
Greedy Florist
Greedy Florist
Sort by
recency
|
776 Discussions
|
Please Login in order to post a comment
The idea is if there are k friends, we want each friend to purchase as less as possibile followers in order to reduce cost.
So we are going to sort the price in descending order and let each friend first buy expensive flower and graduly move toward more cheaper one, because multiple of less priced flower will be less.
Just O(n) time and O(log(n)) space complexity
Arrays.sort(c); int i = 1; int res = 0; int count = 0; for(int j = c.length-1; j>=0; j--){ res += c[j] *i; count++; if(count >= k){ count = 0; i++; } } return res;