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的更多相关文章

  1. [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 ...

  2. 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 ...

  3. [LeetCode] Create Maximum Number 创建最大数

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  4. [LintCode] Create Maximum Number 创建最大数

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  5. Leetcode: Create Maximum Number

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  6. [Swift]LeetCode321. 拼接最大数 | Create Maximum Number

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  7. Create Maximum Number

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  8. C - Maximum of Maximums of Minimums(数学)

    C - Maximum of Maximums of Minimums You are given an array a1, a2, ..., an consisting of n integers, ...

  9. 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 ...

随机推荐

  1. zend studio 13.6.1中文乱码的解决办法

    进入窗口--->首选项--->常规--->工作空间 window=>preference=>General=>workspace 照图设置后重启软件即可

  2. 《JavaScript DOM编程艺术》(第二版)学习笔记(一)

    这本书去年就买了但一直没看,闲暇的时候看了下,发现里面写的内容还真是不错,所以决定一边在博客上记录些学习的笔记,以便以后观看及查找方便. js文件最好的做法是放在< body>标签里,这样 ...

  3. JDK源码那些事儿之红黑树基础上篇

    说到HashMap,就一定要说到红黑树,红黑树作为一种自平衡二叉查找树,是一种用途较广的数据结构,在jdk1.8中使用红黑树提升HashMap的性能,今天就来说一说红黑树. 前言 限于篇幅,本文只对红 ...

  4. JavaScript004,输出

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  5. [ 转载 ] Java基础

    1.String a = “123”; String b = “123”; a==b的结果是什么? 这包含了内存,String存储方式等诸多知识点.ans:同样序列的字符串直接量为一个实例,所以其实引 ...

  6. JavaScript查漏补缺

    js函数定义的三种方式: 函数声明 function sum(a,b){ return a+b } 函数表达式 var sum = function(a,b){ return a+b } Functi ...

  7. Hibernate初探之单表映射——创建对象-关系映射文件

    编写一个Hibernate例子 第三步:创建对象-关系映射文件 以下是具体实现步骤: 找到我们要持久化的学生类Sudents 生成对象-关系映射文档Students.hbm.xml: <?xml ...

  8. python_网络编程hmac模块验证客户端的合法性

    hmac模块: 比较两个函数的密钥是否一致: import hmac import os msg = os.urandom(32) #生成32位随机字节码 def wdc(): key = b'wdc ...

  9. Vue基础认识

     一:什么是Vue? vue是一个渐进式的JavaScript框架,采用的是MVVM模式.Vue 被设计为可以自底向上逐层应用.Vue 的核心库只关注视图层,不仅易于上手,还便于与第三方库或既有项目整 ...

  10. Linux查看公网IP

    curl cip.cc   查看公网IP curl  -s  icanhazip.com  查看公网IP, 只显示IP,没有供应商信息