Generate Maximum revenue by selling K tickets from N windows
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.
Window Number 1 2 3 4 5 6
Tickets 5 1 7 10 11 9
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.)
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.
Java:
import java.util.Comparator;
import java.util.PriorityQueue; public class MaxRevenueTickets { PriorityQueue<Integer> pq; // we will create a max heap
public MaxRevenueTickets(int length) {
pq = new PriorityQueue<>(length, new Comparator<Integer>() { @Override
public int compare(Integer o1, Integer o2) {
// TODO Auto-generated method stub
return o2 - o1;
}
});
} public int calculate(int[] windowsTickets, int tickets) { int revenue = 0;
// insert the all the elements of an array into the priority queue
for (int i = 0; i < windowsTickets.length; i++) {
pq.offer(windowsTickets[i]);
} while (tickets > 0) {
int ticketPrice = pq.poll();
revenue += ticketPrice;
pq.offer(--ticketPrice);
tickets--;
}
return revenue;
} public static void main(String[] args) {
int[] windowsTickets = { 5, 1, 7, 10, 11, 9 };
int noOfTickets = 5;
MaxRevenueTickets mx = new MaxRevenueTickets(windowsTickets.length);
System.out.println("Max revenue generated by selling " + noOfTickets
+ " tickets: " + mx.calculate(windowsTickets, noOfTickets)); }
}
Generate Maximum revenue by selling K tickets from N windows的更多相关文章
- [LeetCode] 643. Maximum Average Subarray I_Easy tag: Dynamic Programming(Sliding windows)
Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...
- 402. Remove K Digits/738.Monotone Increasing Digits/321. Create Maximum Number
Given a non-negative integer num represented as a string, remove k digits from the number so that th ...
- [LeetCode] Create Maximum Number 创建最大数
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- [LintCode] Create Maximum Number 创建最大数
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- Leetcode: Create Maximum Number
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- [Swift]LeetCode321. 拼接最大数 | Create Maximum Number
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- Create Maximum Number
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- C - Maximum of Maximums of Minimums(数学)
C - Maximum of Maximums of Minimums You are given an array a1, a2, ..., an consisting of n integers, ...
- 321. Create Maximum Number (c++ ——> lexicographical_compare)
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
随机推荐
- lvs原理及安装部署详解(参考)
LVS安装使用详解 摘至:http://www.cnblogs.com/MacoLee/p/5856858.html 简介 LVS是Linux Virtual Server的简称,也就是Linux虚拟 ...
- SPFA的优化
[为什么要优化] 关于SPFA,他死了(懂的都懂) 进入正题... 一般来说,我们有三种优化方法. SLF优化: SLF优化,即 Small Label First 策略,使用 双端队列 进行优 ...
- 从客户端中检测到有潜在危险的 request.form值
这里只说ASP.NET MVC的解决方法,ASP.NET已经不碰了. 给控制器加上[ValidateInput(false)]特性即可忽略含有HTML标记的内容. [HttpPost] [Valida ...
- Java并发包--线程池框架
转载请注明出处:http://www.cnblogs.com/skywang12345/p/3509903.html 线程池架构图 线程池的架构图如下: 1. Executor 它是"执行者 ...
- 使用 uni-app 开发遇到的问题
想法: uni-app 给我的感觉就像是把微信小程序的API,组件和vue的语法揉捏在一起所组成框架,没有原生小程序和vue那种流畅感,官方的 uni-ui 组件库不够成熟,坑比较多. 问题一:自 ...
- Jekyll添加FancyBox 插件
一.简要 这是之前在GitHub Page上面使用博客功能的完善,之前每次传到GitHub上面的图片在博客里面都是显示压缩后的,导致很多代码细节都看不清. Markdown 语法中的图片我们一般是如此 ...
- AtCoder Beginner Contest 148
ABC 148 第一次打abc,记录一下 Task Name Time Limit Memory Limit A Round One 2 sec 1024 MB B Strings with the ...
- day42_Oracle学习笔记_01
一.Oracle Database 的基本概念 1.1.一个Oracle服务器 详解如下: 一个Oracle服务器是一个关系型数据管理系统(RDBMS),它提供开放的,全面的,近乎完整的信息管理. ...
- 03_每周 5 使用 tar 命令备份/var/log 下的所有日志文件
]# vim /root/logbak.shtar -czf log-`date +%Y%m%d`.tar.gz /var/log ]# crontab -e -u root00 03 * * 5 / ...
- C++问题--fread文件读不完整问题解决
今天突然遇到一个问题,用fwrite/fread读写文件,发现当fread读取文件时只能读一半, 即使用foef()查看是否读到文件结尾,也是显示文件已经读取到文件末尾,查看文件的返回值发现文件只读取 ...