Created
October 3, 2015 22:20
-
-
Save thmain/4bd0b6e82c50cc5eab17 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment