Generate Maximum revenue by selling K tickets from N windows
2021-07-09 23:07
标签:sort elements import number += EAP this hat eve Objective: Given ‘N’ windows where each window contains certain number of tickets at each window. Price of a ticket is equal to number of tickets remaining at that window. Write an algorithm to sell ‘k’ tickets from these windows in such a manner so that it generates the maximum revenue. This problem was asked in the Bloomberg for software developer position. Example: Say we have 6 windows and they have 5, 1, 7, 10, 11, 9 tickets respectively. Bloomberg 解法:类似于23. Merge k Sorted Lists 合并k个有序链表 用最大堆 Approach: 1. Create a max-heap of size of number of windows. (Click here read about max-heap and priority queue.) Java: Generate Maximum revenue by selling K tickets from N windows 标签:sort elements import number += EAP this hat eve 原文地址:https://www.cnblogs.com/lightwindy/p/9693000.html
Window Number 1 2 3 4 5 6
Tickets 5 1 7 10 11 9
2. Insert the number of tickets at each window in the heap.
3. Extract the element from the heap k times (number of tickets to be sold).
4. Add these extracted elements to the revenue. It will generate the max revenue since extracting for heap will give you the max element which is the maximum number of tickets at a window among all other windows, and price of a ticket will be number of tickets remaining at each window.
5. Each time we extract an element from heap and add it to the revenue, reduce the element by 1 and insert it again to the heap since after number of tickets will be one less after selling.import java.util.Comparator;
import java.util.PriorityQueue;
public class MaxRevenueTickets {
PriorityQueue
文章标题:Generate Maximum revenue by selling K tickets from N windows
文章链接:http://soscw.com/index.php/essay/102960.html