forked from super30admin/Design-5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparkingLot.java
103 lines (80 loc) · 3.23 KB
/
parkingLot.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
//Question:
//Design a parking lot system where you need to provide a token with the parking space number on it to each new entry for
// the space closest to the entrance. When someone leave you need update this space as empty. What data structures will
// you use to perform the closest empty space tracking, plus finding what all spaces are occupied at a give time.
// Time Complexity : O(log n), since we add all the parking spots once which would cost log n to construct the min heap
// Space Complexity : O(n), where n is the number of parking spots
// Did this code successfully run on Leetcode :Yes
// Any problem you faced while coding this :No
// Your code here along with comments explaining your approach
import java.io.*;
import java.util.*;
class GFG {
public static class ParkingLot {
int maxFloors, spotsPerFloor;
PriorityQueue<ParkingSpot> pq;
public ParkingLot (int floors, int spotsPerFloor) {
this.maxFloors = floors;
this.spotsPerFloor = spotsPerFloor;
pq = new PriorityQueue<>((a, b) -> {
if(a.floor == b.floor) {
return a.spot - b.spot;
}
return a.floor - b.floor;
});
}
public void addParkingSpot(int floor, int spot) {
if (floor > maxFloors) {
throw new IllegalArgumentException("Invalid floor");
}
if (spot > spotsPerFloor) {
throw new IllegalArgumentException("Invalid spot");
}
pq.add(new ParkingSpot(floor, spot));
}
public ParkingSpot getNextAvailableSpot() {
return pq.peek();
}
public ParkingSpot park() {
if (pq.isEmpty()) {
throw new IllegalArgumentException("Parking full !");
}
return pq.poll();
}
public void unpark(int floor, int spot) {
pq.add(new ParkingSpot(floor, spot));
}
}
public static class ParkingSpot {
int floor, spot;
public ParkingSpot (int floor, int spot) {
this.floor = floor;
this.spot = spot;
}
public int getFloor() {
return this.floor;
}
public int getSpot() {
return this.spot;
}
}
public static void main (String[] args) {
ParkingLot lot = new ParkingLot(10, 10);
lot.addParkingSpot(0,1);
lot.addParkingSpot(0,2);
lot.addParkingSpot(4,4);
lot.addParkingSpot(5,9);
lot.addParkingSpot(8,3);
ParkingSpot spot = lot.park();
System.out.println("Parked at floor: " + spot.getFloor() + " and spot: " + spot.getSpot());
spot = lot.park();
System.out.println("Parked at floor: " + spot.getFloor() + " and spot: " + spot.getSpot());
spot = lot.park();
System.out.println("Parked at floor: " + spot.getFloor() + " and spot: " + spot.getSpot());
spot = lot.park();
System.out.println("Parked at floor: " + spot.getFloor() + " and spot: " + spot.getSpot());
lot.unpark(0,2);
spot = lot.park();
System.out.println("Parked at floor: " + spot.getFloor() + " and spot: " + spot.getSpot());
}
}