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. nginx中ngx_http_ssl_module模块

    此模块为HTTPS提供必要的⽀支持worker_processes auto;http {...server {listen 443 ssl;keepalive_timeout 70;ssl_prot ...

  2. date对象设置set

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 使用谷歌chrome浏览器查看任何标签的固有属性

    查看任何标签的固有属性property: 使用谷歌浏览器:Ctrl+Shift+I 开发者工具----点击Elements----点击a标签----点击Properties属性及其属性值. 自定义属性 ...

  4. PyPI教程

    Wiki PyPI The Python Package Index, abbreviated as PyPI and also known as the Cheese Shop (a referen ...

  5. 010_STM32程序移植之_lib库建立

    STM32之lib库建立 1. 测试环境:STM32C8T6 2. 测试接口: 3.串口使用串口一,波特率9600 单片机引脚------------CH340引脚 VCC-------------- ...

  6. React事件处理和原生JS事件处理

    1.原生JS 事件触发调用有三种方式: 1. on[event]事件属性,手动触发  ❗️on[event]事件是Window对象上的方法. 2. on[event]事件属性,通过htmlElemen ...

  7. java如何向数组里添加元素

    向数组里添加一个元素怎么添加,这儿总结有三种方法: 1.一般数组是不能添加元素的,因为他们在初始化时就已定好长度了,不能改变长度.但有个可以改变大小的数组为ArrayList,即可以定义一个Array ...

  8. PRIMARY KEY,key,unique key

    主键索引(必须指定为“PRIMARY KEY”,没有PRIMARY Index). 唯一索引(unique index,一般写成unique key). 普通索引(index,只有这一种才是纯粹的in ...

  9. 将.mat文件中的数据转换成图片

    %% 如何将.mat文件中的数据转换成图片 clc;clear all; addpath F_data/MAT;load('D:\face.mat') for i=1:q   img=uint8(re ...

  10. laotech老师唠科mac 深入浅出MAC OS X

    laotech老师唠科mac 深入浅出MAC OS X http://study.163.com/plan/planLearn.htm?id=1637004#/learn/resVideo?lesso ...