703818. Find the fine
Find the Fine4353
Summary
The problem involves calculating a fine for a set of parking tickets. The fine is calculated based on the number of hours the car was parked in an unauthorized zone. The algorithm must iterate through an array of parking times and calculate the total fine.
Detailed Explanation
To solve this problem, we can iterate through the array of parking times and calculate the total fine by adding up the fines for each hour of parking time that exceeds the authorized parking time (1 hour). We assume that the fine is $10 per hour beyond the authorized time.
Here's a step-by-step breakdown of the solution:
- Initialize the total fine to 0.
- Iterate through the array of parking times.
- For each parking time, calculate the difference between the parking time and the authorized time (1 hour).
- If the difference is greater than 0, add the difference multiplied by $10 to the total fine.
- Return the total fine.
Time complexity: O(n), where n is the number of elements in the array. Space complexity: O(1), as we only use a constant amount of space to store the total fine and temporary variables.
Here's an ASCII art diagram illustrating the algorithm:
+---------------+
| Authorized Time |
+---------------+
^
|
v
+---------------+ +---------------+
| Parking Time 1| | Parking Time 2|
+---------------+ +---------------+
^ ^ ^
| | |
v v v
+---------------+ +---------------+ +---------------+
| Calculate Fine | | Calculate Fine | | Calculate Fine |
+---------------+ +---------------+ +---------------+
Optimized Solutions
Java
public int findFine(int[] parkingTimes) {
int totalFine = 0;
for (int time : parkingTimes) {
if (time > 1) {
totalFine += (time - 1) * 10;
}
}
return totalFine;
}