Mini Uber

Implement a mini-Uber

  1. Drivers provide their location
  2. A user request, and then returns a matching driver

Implement the following functions

  • report(driver_id, lat, lng)
    • If no match is found the trip, return null
    • Otherwise match trip information
  • request(rider_id, lat, lng)
    1. The establishment of a trip
    2. Recently found a driver, the driver marked as unavailable
    3. The driver id filled trip
    4. Return trip

Java trip in the definition

public class Trip {
    public int id; // trip's id, primary key
    public int driver_id, rider_id; // foreign key
    public double lat, lng; // pick up location
}

Ideas: Integer to Trip, Integer to Location, already is used to return a trip of driver, one is no return trip to the driver, for the minimum rider with the most recent is the desire of existing driver; that is updated two hashmap can;

/**
 * Definition of Trip:
 * public class Trip {
 *     public int id; // trip's id, primary key
 *     public int driver_id, rider_id; // foreign key
 *     public double lat, lng; // pick up location
 *     public Trip(int rider_id, double lat, double lng);
 * }
 * Definition of Helper
 * class Helper {
 *     public static double get_distance(double lat1, double lng1,
                                         double lat2, double lng2) {
 *         // return distance between (lat1, lng1) and (lat2, lng2)
 *     }
 * };
 */
public class MiniUber {
    
    private class Location {
        public double lat;
        public double lng;
        public Location(double lat, double lng) {
            this.lat = lat;
            this.lng = lng;
        }
    }

    private HashMap<Integer, Trip> driver2Trip;
    private HashMap<Integer, Location> driver2Location;
    public MiniUber() {
        driver2Trip = new HashMap<Integer, Trip>();
        driver2Location = new HashMap<Integer, Location>();
    }

    // @param driver_id an integer
    // @param lat, lng driver's location
    // return matched trip information if there have matched rider or null
    public Trip report(int driver_id, double lat, double lng) {
        if(driver2Trip.containsKey(driver_id)) {
            return driver2Trip.get(driver_id);
        } else {
            // update location hashmap;
            if(driver2Location.containsKey(driver_id)) {
                driver2Location.get(driver_id).lat = lat;
                driver2Location.get(driver_id).lng = lng;
            } else {
                driver2Location.put(driver_id, new Location(lat, lng));
            }
            // return null trip info;
            return null;
        }
    }

    // @param rider_id an integer
    // @param lat, lng rider's location
    // return a trip
    public Trip request(int rider_id, double lat, double lng) {
        Trip trip = new Trip(rider_id, lat, lng);
        double distance = -1;
        int driver_id = -1;
        for(Integer driver: driver2Location.keySet()) {
            Location location = driver2Location.get(driver);
            double dis = Helper.get_distance(lat, lng, location.lat, location.lng);
            if(distance == -1 || dis < distance) {
                distance = dis;
                driver_id = driver;
            }
        }
        
        if(driver_id != -1) {
            driver2Location.remove(driver_id);
        }
        trip.driver_id = driver_id;
        driver2Trip.put(driver_id, trip);
        return trip;
    }
}

 

Published 570 original articles · won praise 13 · views 170 000 +

Guess you like

Origin blog.csdn.net/u013325815/article/details/104090901