-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRailwayTicketSell.java
212 lines (185 loc) · 4.64 KB
/
RailwayTicketSell.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package com.rrohit.hakerrank;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
* 1. There are ‘n’ ticket windows in the railway station, ith window has a[i] tickets available.
* Price of a ticket is equal to the number of tickets remaining in that window at that time.
* When ‘m’ tickets have been sold, what’s the maximum amount of money the railway station can earn?
e.g.
INPUT: n=2, m=4
a1=2 , a2=5
OUTPUT: 14(2nd window sold 4 tickets so 5+4+3+2).
* @author rrohit
*/
public class RailwayTicketSell {
public class TicketData{
private int windows;
private long tickets;
private int[] avail;
public TicketData(){}
public TicketData(int windows, long tickets){
this.windows = windows;
this.tickets = tickets;
this.avail = new int[windows];
}
}
public class MaxHeap {
private int[] heap;
private int count;
private int capacity;
public MaxHeap(){}
public MaxHeap(int count, int capacity) {
this.capacity = capacity;
this.count= count;
this.heap = new int[capacity];
}
public int getLeft(int i) {
int left = 2*i+1;
if (left >= this.count){
return -1;
}
return left;
}
public int getParent(int i) {
int parent = (i-1)/2;
if (parent >= this.count){
return -1;
}
return parent;
}
public int getRight(int i) {
int right = 2*i+2;
if (right >= this.count){
return -1;
}
return right;
}
public int getMax() {
if (this.count ==0){
return -1;
}
return this.heap[0];
}
public void heapify(int i) {
int left, right, max, temp;
left = getLeft(i);
right = getRight(i);
if ( (left != -1) && this.heap[left]>this.heap[i]){
max = left;
}else{
max = i;
}
if ( (right != -1) && (this.heap[right]>max)){
max = right;
}
if (max != i) { // swap i with max, then heapify on max
temp = this.heap[i];
this.heap[i] = this.heap[max];
this.heap[max] = temp;
heapify(max);
}
}
/*
* 1 Copy top element into temp
* 2 Copy last element onto top
* 3 decrease heap count
* 4 now heapify top
* Time : O(lgN)
*/
public int deleteMax() {
if (this.count == 0){
return -1;
}
int data = this.heap[0];
this.heap[0] = this.heap[this.count-1];
this.count--;
heapify(0);
return data;
}
public void insert(int data){
if (this.count == this.capacity){
//resizeHeap();
}
this.count++;
int i = count-1;
while (i>=0 && data>this.heap[(i-1)/2]){
this.heap[i] = this.heap[(i-1)/2];
i = (i-1)/2;
}
this.heap[i] = data;
}
public void buildheap(int []arr, int n){
this.capacity = n;
this.count = n;
this.heap = arr;
/*
* 1. Consider arr as a heap
* 2. And start heapify from last parent
*/
for (int i=(n-1)/2; i>=0; i--){
this.heapify(i);
}
}
}
public TicketData readInput() {
TicketData td = new TicketData();
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
try{
String[] inputArr = input.readLine().trim().split(" ");
td.windows = Integer.parseInt(inputArr[0]);
td.tickets = Long.parseLong(inputArr[1]);
td.avail = new int[td.windows];
inputArr = input.readLine().trim().split(" ");
int len = inputArr.length;
if (len>td.windows) {
throw new IllegalArgumentException("Tickets must be given for all windows");
}
int index=0;
while(index<len){
td.avail[index] = Integer.parseInt(inputArr[index]);
index++;
}
}catch(NumberFormatException nfe){
System.out.println("Unable to parse input");
nfe.getStackTrace();
}catch(IOException ioe){
System.out.println("Unable to read input");
ioe.getStackTrace();
}finally{
try{
input.close();
}catch(IOException ioe){
System.out.println("Unable to close input");
ioe.getStackTrace();
}
}
return td;
}
private long getMaxAmtFromTicketSell(TicketData td) {
int i =0;
long maxAmount =0;
MaxHeap heap = new MaxHeap();
heap.buildheap(td.avail, td.windows);
int data;
while (i<td.tickets) {
data = heap.deleteMax();
maxAmount += data;
data--;
i++; // increase ticket count as well
while (data>heap.getMax() && i<td.tickets){ // sell ticket till its rate are highest
maxAmount += data;
data--;
i++; // increase the ticket count as well
}
heap.insert(data); // since rate are not highest insert back to heap
}
return maxAmount;
}
public static void main(String[] args) {
RailwayTicketSell rt = new RailwayTicketSell();
TicketData td = rt.readInput();
long maxAmount = rt.getMaxAmtFromTicketSell(td);
System.out.println(maxAmount);
}
}