To solve this problem, we need to calculate the total fare for a passenger's metro rides while considering the daily fee limit based on the highest station number the passenger visits during the day.
Step-by-Step Approach:
-
Identify the Maximum Station Visited (Y):
- Traverse through both the
start and dest arrays to find the highest station number the passenger visits. This will determine the applicable fee limit from the limit array.
-
Calculate the Total Fare:
- For each ride, compute the fare using the formula:
[
\text{Fare} = 1 + 2 \times |\text{dest}[i] - \text{start}[i]|
]
- 1 is the boarding fee.
- (2 \times |\text{dest}[i] - \text{start}[i}|) accounts for the incremental fee based on the number of adjacent station travels.
- Sum up the fares for all rides to get the total fare.
-
Apply the Fee Limit:
- Compare the calculated total fare with the fee limit corresponding to the maximum station visited (
limit[Y]).
- If the total fare exceeds the limit, charge only the limit amount. Otherwise, charge the total fare.
Example Walkthrough:
Example 1:
- Inputs:
start = [1, 0, 2, 4]
dest = [2, 2, 0, 5]
limit = [3, 17, 7, 4, 5, 17]
- Process:
- Maximum station visited (
Y) = 5.
- Calculated fares:
- Ride 1: 1 → 2 → Fare = 1 + 2*(1) = 3
- Ride 2: 0 → 2 → Fare = 1 + 2*(2) = 5
- Ride 3: 2 → 0 → Fare = 1 + 2*(2) = 5
- Ride 4: 4 → 5 → Fare = 1 + 2*(1) = 3
- Total Fare = 3 + 5 + 5 + 3 = 16
- Fee Limit =
limit[5] = 17
- Result: Since 16 ≤ 17, the passenger is charged 16.
Example 2:
- Inputs:
start = [1, 2, 0, 2, 3]
dest = [2, 1, 2, 1, 2]
limit = [4, 8, 18, 16, 201]
- Process:
- Maximum station visited (
Y) = 3.
- Calculated fares:
- Ride 1: 1 → 2 → Fare = 3
- Ride 2: 2 → 1 → Fare = 3
- Ride 3: 0 → 2 → Fare = 5
- Ride 4: 2 → 1 → Fare = 3
- Ride 5: 3 → 2 → Fare = 3
- Total Fare = 3 + 3 + 5 + 3 + 3 = 17
- Fee Limit =
limit[3] = 16
- Result: Since 17 > 16, the passenger is charged 16.
Implementation:
Below is the implementation of the solution in C:
#include <stdio.h>
#include <stdlib.h>
int solution(int start[], int dest[], int N, int limit[], int K) {
int max_station = 0;
long long total_fare = 0;
// Find the maximum station number visited
for(int i = 0; i < N; i++) {
if(start[i] > max_station) {
max_station = start[i];
}
if(dest[i] > max_station) {
max_station = dest[i];
}
}
// Ensure max_station does not exceed K-1
if(max_station >= K) {
// Handle invalid case where station number exceeds limit array
// Depending on requirements, you might return an error or handle differently
// Here, we'll assume it's within bounds as per problem statement
max_station = K - 1;
}
// Calculate total fare
for(int i = 0; i < N; i++) {
int distance = abs(dest[i] - start[i]);
long long fare = 1 + 2 * distance;
total_fare += fare;
}
// Apply the fee limit
if(total_fare > limit[max_station]) {
return limit[max_station];
} else {
return (int)total_fare;
}
}
// Example usage
int main() {
// Example 1
int start1[] = {1, 0, 2, 4};
int dest1[] = {2, 2, 0, 5};
int limit1[] = {3, 17, 7, 4, 5, 17};
int K1 = 6;
int N1 = 4;
printf("Example 1 Output: %d\n", solution(start1, dest1, N1, limit1, K1)); // Expected: 16
// Example 2
int start2[] = {1, 2, 0, 2, 3};
int dest2[] = {2, 1, 2, 1, 2};
int limit2[] = {4, 8, 18, 16, 201};
int K2 = 5;
int N2 = 5;
printf("Example 2 Output: %d\n", solution(start2, dest2, N2, limit2, K2)); // Expected: 16
return 0;
}
Explanation of the Code:
-
Finding the Maximum Station (max_station):
- Iterate through each ride and update
max_station if a higher station number is found in either start or dest.
-
Calculating the Total Fare (total_fare):
- For each ride, compute the fare using the formula
1 + 2 * distance, where distance is the absolute difference between dest[i] and start[i].
- Accumulate the fares to get the
total_fare.
-
Applying the Fee Limit:
- Compare
total_fare with limit[max_station].
- Return the smaller of the two values.
-
Handling Edge Cases:
- The code assumes that the maximum station number does not exceed
K-1. If there's a possibility of exceeding, additional error handling can be implemented as needed.
Conclusion
This approach efficiently calculates the total fare while respecting the daily fee limit based on the highest station visited. By breaking down the problem into manageable steps—identifying the maximum station, calculating individual fares, summing them up, and applying the limit—we ensure clarity and correctness in the solution.