MCPcopy Index your code
hub / github.com/careercup/ctci / findAvailableSpots

Method findAvailableSpots

java/Chapter 8/Question8_4/Level.java:57–77  ·  view source on GitHub ↗
(Vehicle vehicle)

Source from the content-addressed store, hash-verified

55
56 /* find a spot to park this vehicle. Return index of spot, or -1 on failure. */
57 private int findAvailableSpots(Vehicle vehicle) {
58 int spotsNeeded = vehicle.getSpotsNeeded();
59 int lastRow = -1;
60 int spotsFound = 0;
61 for (int i = 0; i < spots.length; i++) {
62 ParkingSpot spot = spots[i];
63 if (lastRow != spot.getRow()) {
64 spotsFound = 0;
65 lastRow = spot.getRow();
66 }
67 if (spot.canFitVehicle(vehicle)) {
68 spotsFound++;
69 } else {
70 spotsFound = 0;
71 }
72 if (spotsFound == spotsNeeded) {
73 return i - (spotsNeeded - 1);
74 }
75 }
76 return -1;
77 }
78
79 public void print() {
80 int lastRow = -1;

Callers 1

parkVehicleMethod · 0.95

Calls 3

getRowMethod · 0.95
canFitVehicleMethod · 0.95
getSpotsNeededMethod · 0.80

Tested by

no test coverage detected