Easy LeetCode for Beginners: Library Late Fee Calculator in Python
Library Late Fee Calculator - Video Solution
Watch the video solution for Library Late Fee Calculator, a easy level problem involving Array, Simulation. This walkthrough by LindorCodes has No views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Problem Statement
You are given an integer array daysLate where daysLate[i] indicates how many days late the ith book was returned.
The penalty is calculated as follows:
- If
daysLate[i] == 1, penalty is 1. - If
2 <= daysLate[i] <= 5, penalty is2 * daysLate[i]. - If
daysLate[i] > 5, penalty is3 * daysLate[i].
Return the total penalty for all books.
Example 1:
Input: daysLate = [5,1,7]
Output: 32
Explanation:
daysLate[0] = 5: Penalty is2 * daysLate[0] = 2 * 5 = 10.daysLate[1] = 1: Penalty is1.daysLate[2] = 7: Penalty is3 * daysLate[2] = 3 * 7 = 21.- Thus, the total penalty is
10 + 1 + 21 = 32.
Example 2:
Input: daysLate = [1,1]
Output: 2
Explanation:
daysLate[0] = 1: Penalty is1.daysLate[1] = 1: Penalty is1.- Thus, the total penalty is
1 + 1 = 2.
Constraints:
1 <= daysLate.length <= 1001 <= daysLate[i] <= 100
Approach Overview
Problem Overview: You receive borrowing or return information for library books and must compute the total late fee according to the library's rules. The task is mainly about correctly applying the fee policy for each record and summing the result.
Approach 1: Direct Simulation (O(n) time, O(1) space)
The most natural solution is to simulate the fee calculation exactly as described. Iterate through the array of borrowing records, compute how many days each book was returned late, and apply the fee rule for that record. Each iteration performs constant work: compare dates or day counts, compute the overdue duration, and multiply by the per‑day penalty if the book is late.
This approach works well because the problem has no complicated dependencies between entries. Each record can be processed independently, which means a single pass over the input is sufficient. The time complexity is O(n) since you process each entry once, and the space complexity is O(1) because you only maintain a running total of fees.
The implementation is essentially a straightforward simulation. You replicate the real‑world rules programmatically: determine if the return date exceeds the allowed period, compute the overdue days, and accumulate the corresponding fee. This pattern shows up frequently in easy interview problems where the focus is correct rule handling rather than algorithmic optimization.
Approach 2: Structured Fee Calculation with Precomputed Rules (O(n) time, O(1) space)
If the fee policy contains multiple tiers (for example, different rates after certain day thresholds), you can encode those rules into simple condition checks or a small lookup structure. During iteration, determine the overdue duration and map it to the correct fee calculation logic.
This still processes each record exactly once, so the time complexity remains O(n). Space complexity stays O(1) because the rule set is fixed and small. The benefit of structuring the rules explicitly is cleaner code when fee policies contain several conditions or thresholds.
This version still relies on simulation of the rules but organizes the logic to make extensions easier. In production systems, fee policies often change, so separating rule logic from the main loop improves maintainability.
Recommended for interviews: The direct simulation approach is exactly what interviewers expect. It demonstrates that you can read problem constraints carefully and implement rule-based logic correctly. Mentioning that each record is independent and therefore solvable in a single pass shows good algorithmic reasoning, even though the implementation is simple.
Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Direct Simulation | O(n) | O(1) | Best for straightforward rule-based problems where each record can be processed independently |
| Structured Fee Rules with Lookup | O(n) | O(1) | Useful when fee policies have multiple tiers or conditions and cleaner rule organization is needed |