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 ...
随机推荐
- 用正则表达式处理一个复杂字符串(类似json格式)
#利用正则输出{}中的内容 str1="""var local=[{provinceCode:'310000', cityCode:'310100', text: ...
- MYSQL参数说明
[mysqld] character_set_server=utf8 #慢日志时间 long_query_time=1 #开启慢日志 slow_query_log=TRUE #慢日志位置 slow_q ...
- JS 禁用按钮10秒方法
方式一:禁用10秒,10秒钟后可用 /** * 按钮禁用10秒 * @param submitButtonName 按钮ID名 */ function disabledSubmitButton(sub ...
- 基于Flask和百度AI实现与机器人对话
实现对话机器人主要有个步骤 : 一.前端收集语音传入后端 二.后端基于百度AI接口进行语音识别,转换成文字 三.对文字进行自定义验证或通过图灵端口进行处理,生成回复内容 四.将文字通过百度AI接口合成 ...
- Python基础篇--输入与输出
站长资讯平台:Python基础篇--输入与输出在任何语言中,输入和输出都是代码最基础的开始,so,先来聊一聊输入和输出输出输入END在任何语言中,输入和输出都是代码最基础的开始,so,先来聊一聊输入和 ...
- 日常note
1.插入使相同的最少次数 给定两个序列A,B 每次只能进行把一个元素插入特定位置的操作 求至少对A进行多少次才能使A等于B 设A,B的长度为Len,那么答案为 \(Len-LCS(A,B)\) 2.\ ...
- vue 想关工具 及组件
vue-cli vue的脚手架工具 (1) 安装通过 npm install -g vue-cil (2)常用模板 browserify - 拥有高级功能的 Browserify ...
- java内存溢出定位
一.内存溢出问题分类 瞬时流量过大造成的创建大量对象 内存泄漏导致的内存溢出,一般就是程序编码的BUG引起的 二.内存泄漏问题分析 step1: 收集内存泄漏的堆内存异常日志 > 添加HeapD ...
- Flutter 踩坑集
1.Flutter Packages Get 一直重试或一直失败的问题 翻车原因:万恶之源-----天朝的长城防火墙 解决方法 详见:https://flutter.dev/community/chi ...
- [Luogu] 八数码难题
https://www.luogu.org/problemnew/show/P1379 long long ago 暴力bfs #include <iostream> #include & ...