#3687
Easy Algorithms Library late fee calculator
Array Simulation
94.3% acceptance
Mar 31, 2026
5
3
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 is 2 * daysLate[i].
If daysLate[i] > 5, penalty is 3 * daysLate[i].
Return the total penalty for all books.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn late_fee(days_late: Vec<i32>) -> i32 {
days_late.iter().map(|&d| {
if d == 1 { 1 }
else if d >= 2 && d <= 5 { 2 * d }
else { 3 * d }
}).sum()
}
}