LeetCode 3166: Calculate Parking Fees and Duration Solution
Master LeetCode problem 3166 (Calculate Parking Fees and Duration), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
3166. Calculate Parking Fees and Duration
Problem Explanation
Explanation
To solve this problem, we can simulate the process of calculating parking fees and duration based on the given rules. We will iterate over the timestamps of each car's entry and exit times, calculate the parking duration, and then based on the parking fee structure, determine the total parking fee for each car.
- Iterate over each car's entry and exit times.
- Calculate the parking duration by subtracting the entry time from the exit time.
- Based on the parking fee structure (e.g., first hour is free, subsequent hours have a fixed fee), calculate the total parking fee for each car.
- Return the total parking fees for all cars.
Time complexity: O(n), where n is the number of cars. Space complexity: O(1)
Solution Code
class Solution {
public double calculateParkingFeesAndDuration(String[] records, double hourlyRate) {
double totalFees = 0.0;
for (String record : records) {
String[] parts = record.split(":");
int entryTime = Integer.parseInt(parts[0]);
int exitTime = Integer.parseInt(parts[1]);
int duration = exitTime - entryTime;
double fee = calculateFee(duration, hourlyRate);
totalFees += fee;
}
return totalFees;
}
private double calculateFee(int duration, double hourlyRate) {
int firstHour = 60; // minutes
double firstHourFee = 0.0;
double subsequentHourFee = hourlyRate / 60.0;
if (duration <= firstHour) {
return firstHourFee;
} else {
int additionalHours = (duration - firstHour) / 60;
return firstHourFee + additionalHours * subsequentHourFee;
}
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 3166 (Calculate Parking Fees and Duration)?
This page provides optimized solutions for LeetCode problem 3166 (Calculate Parking Fees and Duration) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 3166 (Calculate Parking Fees and Duration)?
The time complexity for LeetCode 3166 (Calculate Parking Fees and Duration) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 3166 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3166 in Java, C++, or Python.