A taxi driver knows the pick-up and drop-off locat...
Created on: February 8, 2025
Created on: February 8, 2025
A taxi driver knows the pick-up and drop-off locations of people who are requesting taxi services.
All the locations are in km from the starting point.
The starting point is at 0 km.
For each km travelled by a passenger, the driver charges 1 unit of money per passenger. Moreover, the taxi itself is very fancy. Therefore some people are even willing to pay an extra tip if they get to travel in the taxi. At any point of time, the taxi can only accommodate one passenger. Determine the maximum amount the driver can earn.
Example
pickup = [0, 2, 9, 10, 11, 12]
drop = [5, 9, 11, 11, 14, 17)
tip = [1, 2, 3, 2, 2, 1]
The way to earn the most money is by accepting passengers at indices 1, 2 and 5.
• The amount paid by the passenger at index 1:9 - 2 +
2=9
• The amount paid by the passenger at index 2: 11 - 9
in java
We can solve this problem by realizing that it is a weighted interval scheduling problem. Each ride can be seen as an interval with a profit (or weight) given by
Because the taxi can only serve one passenger at a time (i.e. the rides cannot overlap), we want to select a set of non‐overlapping rides that maximizes the sum of profits.
A common way to solve weighted interval scheduling is as follows:
Sort the rides by their drop-off location.
For each ride, use binary search to find the last ride that ends at or before the current ride’s pickup. Let’s call its index . This tells us that if we take the current ride, then we can add its profit to the best answer obtained up to ride .
Use dynamic programming (DP) to choose whether to take or skip a ride. If we let dp[i]
be the maximum earning possible using the first rides (when the rides are sorted by drop), then the recurrence is
(Here we use because we use a DP array of size with dp[0]=0
.)
Below is one acceptable Java solution:
javaimport java.util.Arrays; public class TaxiDriver { // Helper class to hold ride information. static class Ride { long pickup; long drop; long profit; // profit = (drop - pickup) + tip public Ride(long pickup, long drop, int tip) { this.pickup = pickup; this.drop = drop; this.profit = (drop - pickup) + tip; } } /** * Given arrays of pickup locations, drop-off locations, and tips, * returns the maximum amount the taxi driver can earn. * * @param pickup an array of pickup locations (in km) * @param drop an array of drop-off locations (in km) * @param tip an array of tips offered by the riders * @return the maximum total earnings */ public static long taxiDriver(long[] pickup, long[] drop, int[] tip) { int n = pickup.length; // Create an array of rides. Ride[] rides = new Ride[n]; for (int i = 0; i < n; i++) { rides[i] = new Ride(pickup[i], drop[i], tip[i]); } // Sort the rides by drop-off location. Arrays.sort(rides, (a, b) -> Long.compare(a.drop, b.drop)); // Create a DP array where dp[i] will be the maximum earnings using the first i rides. long[] dp = new long[n + 1]; dp[0] = 0; // For binary search, store the drop times in a separate array. long[] endTimes = new long[n]; for (int i = 0; i < n; i++) { endTimes[i] = rides[i].drop; } // Process each ride. for (int i = 1; i <= n; i++) { // Current ride is rides[i-1] (because dp array is 1-indexed). Ride currentRide = rides[i - 1]; // Find the last ride that ends at or before currentRide.pickup. // We want the rightmost index j such that endTimes[j] <= currentRide.pickup. int j = Arrays.binarySearch(endTimes, 0, i - 1, currentRide.pickup); if (j < 0) { // If not found, binarySearch returns (-(insertion point) - 1). // The insertion point is the index of the first element greater than currentRide.pickup. j = -j - 2; } // Candidate earnings if we take the current ride: // dp[j+1] is the best earnings before current ride's pickup. long takeRide = (j >= 0 ? dp[j + 1] : 0) + currentRide.profit; // Option 1: Skip current ride; Option 2: Take current ride. dp[i] = Math.max(dp[i - 1], takeRide); } return dp[n]; } // Example usage public static void main(String[] args) { long[] pickup = {0, 2, 9, 10, 11, 12}; long[] drop = {5, 9, 11, 11, 14, 17}; int[] tip = {1, 2, 3, 2, 2, 1}; long maxEarnings = taxiDriver(pickup, drop, tip); System.out.println("Maximum earnings: " + maxEarnings); // Expected output: 20 } }
Ride Class:
Ride
class holds the pickup, drop, and computed profit for each ride.Sorting:
Dynamic Programming Array:
dp[i]
holds the maximum earnings possible by considering the first rides (after sorting).endTimes
array to find the ride with the largest drop-off that is less than or equal to the current ride’s pickup. Let that index be .dp[j+1] + currentRide.profit
.dp[i-1]
(skipping the current ride).Example:
This solution meets the constraints and should work efficiently even when there are up to rides.