Skip to content

3687. Library Late Fee Calculator πŸ”’

DifficultyEasy

Description

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.

 

Example 1:

Input: daysLate = [5,1,7]

Output: 32

Explanation:

  • daysLate[0] = 5: Penalty is 2 * daysLate[0] = 2 * 5 = 10.
  • daysLate[1] = 1: Penalty is 1.
  • daysLate[2] = 7: Penalty is 3 * 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 is 1.
  • daysLate[1] = 1: Penalty is 1.
  • Thus, the total penalty is 1 + 1 = 2.

 

Constraints:

  • 1 <= daysLate.length <= 100
  • 1 <= daysLate[i] <= 100

Solutions

Solution 1: Simulation

Thinking

The fee is piecewise in the number of late days: \(1\) for one day, \(2x\) for \(2\ldots 5\), and \(3x\) beyond. Items are independent.

A helper \(f\) implements the three branches and is summed over \(\textit{daysLate}\). \(n\le 100\) needs no extra structure.

We define a function \(f(x)\) to calculate the late fee for each book:

\[ f(x) = \begin{cases} 1 & x = 1 \\ 2x & 2 \leq x \leq 5 \\ 3x & x > 5 \end{cases} \]

Then, for each element \(x\) in the array \(\textit{daysLate}\), we compute \(f(x)\) and sum them up to get the total late fee.

The time complexity is \(O(n)\), and the space complexity is \(O(1)\), where \(n\) is the length of the array \(\textit{daysLate}\).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution:
    def lateFee(self, daysLate: list[int]) -> int:
        def f(x: int) -> int:
            if x == 1:
                return 1
            if x > 5:
                return 3 * x
            return 2 * x

        return sum(f(x) for x in daysLate)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution {
    public int lateFee(int[] daysLate) {
        IntUnaryOperator f = x -> {
            if (x == 1) {
                return 1;
            } else if (x > 5) {
                return 3 * x;
            } else {
                return 2 * x;
            }
        };

        int ans = 0;
        for (int x : daysLate) {
            ans += f.applyAsInt(x);
        }
        return ans;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public:
    int lateFee(vector<int>& daysLate) {
        auto f = [](int x) {
            if (x == 1) {
                return 1;
            } else if (x > 5) {
                return 3 * x;
            } else {
                return 2 * x;
            }
        };

        int ans = 0;
        for (int x : daysLate) {
            ans += f(x);
        }
        return ans;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
func lateFee(daysLate []int) (ans int) {
    f := func(x int) int {
        if x == 1 {
            return 1
        } else if x > 5 {
            return 3 * x
        }
        return 2 * x
    }
    for _, x := range daysLate {
        ans += f(x)
    }
    return
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
function lateFee(daysLate: number[]): number {
    const f = (x: number): number => {
        if (x === 1) {
            return 1;
        } else if (x > 5) {
            return 3 * x;
        }
        return 2 * x;
    };
    return daysLate.reduce((acc, days) => acc + f(days), 0);
}

Comments