LeetCode 题解工作台

学生出勤记录 II

可以用字符串表示一个学生的出勤记录,其中的每个字符用来标记当天的出勤情况(缺勤、迟到、到场)。记录中只含下面三种字符: 'A' :Absent,缺勤 'L' :Late,迟到 'P' :Present,到场 如果学生能够 同时 满足下面两个条件,则可以获得出勤奖励: 按 总出勤 计,学生缺勤( 'A…

category

1

题型

code_blocks

4

代码语言

hub

3

相关题

当前训练重点

困难 · 状态·转移·动态规划

bolt

答案摘要

我们设计一个函数 $dfs(i, j, k)$,表示从第 个出勤记录开始,当前缺勤次数为 ,目前最后连续迟到次数为 时,可获得出勤奖励的情况数量。那么答案就是 $dfs(0, 0, 0)$。 函数 $dfs(i, j, k)$ 的执行过程如下:

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

可以用字符串表示一个学生的出勤记录,其中的每个字符用来标记当天的出勤情况(缺勤、迟到、到场)。记录中只含下面三种字符:
  • 'A':Absent,缺勤
  • 'L':Late,迟到
  • 'P':Present,到场

如果学生能够 同时 满足下面两个条件,则可以获得出勤奖励:

  • 总出勤 计,学生缺勤('A'严格 少于两天。
  • 学生 不会 存在 连续 3 天或 连续 3 天以上的迟到('L')记录。

给你一个整数 n ,表示出勤记录的长度(次数)。请你返回记录长度为 n 时,可能获得出勤奖励的记录情况 数量 。答案可能很大,所以返回对 109 + 7 取余 的结果。

 

示例 1:

输入:n = 2
输出:8
解释:
有 8 种长度为 2 的记录将被视为可奖励:
"PP" , "AP", "PA", "LP", "PL", "AL", "LA", "LL" 
只有"AA"不会被视为可奖励,因为缺勤次数为 2 次(需要少于 2 次)。

示例 2:

输入:n = 1
输出:3

示例 3:

输入:n = 10101
输出:183236316

 

提示:

  • 1 <= n <= 105
lightbulb

解题思路

方法一:记忆化搜索

我们设计一个函数 dfs(i,j,k)dfs(i, j, k),表示从第 ii 个出勤记录开始,当前缺勤次数为 jj,目前最后连续迟到次数为 kk 时,可获得出勤奖励的情况数量。那么答案就是 dfs(0,0,0)dfs(0, 0, 0)

函数 dfs(i,j,k)dfs(i, j, k) 的执行过程如下:

  • 如果 ini \ge n,说明已经遍历完所有出勤记录,返回 11
  • 如果 j=0j = 0,说明当前缺勤次数为 00,那么可以选择缺勤,即 dfs(i+1,j+1,0)dfs(i + 1, j + 1, 0)
  • 如果 k<2k \lt 2,说明当前连续迟到次数小于 22,那么可以选择迟到,即 dfs(i+1,j,k+1)dfs(i + 1, j, k + 1)
  • 无论如何,都可以选择到场,即 dfs(i+1,j,0)dfs(i + 1, j, 0)

我们将上述三种情况的结果相加,即为 dfs(i,j,k)dfs(i, j, k) 的结果。

为了避免重复计算,我们可以使用记忆化搜索。

时间复杂度 O(n)O(n),空间复杂度 O(n)O(n)。其中 nn 为出勤记录的长度。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution:
    def checkRecord(self, n: int) -> int:
        @cache
        def dfs(i, j, k):
            if i >= n:
                return 1
            ans = 0
            if j == 0:
                ans += dfs(i + 1, j + 1, 0)
            if k < 2:
                ans += dfs(i + 1, j, k + 1)
            ans += dfs(i + 1, j, 0)
            return ans % mod

        mod = 10**9 + 7
        ans = dfs(0, 0, 0)
        dfs.cache_clear()
        return ans
speed

复杂度分析

指标
时间O(n)
空间O(1)
psychology

面试官常问的追问

外企场景
  • question_mark

    Check if the candidate understands how state transitions work in dynamic programming.

  • question_mark

    Ensure they are aware of the modulo operation to handle large numbers.

  • question_mark

    Evaluate their approach to optimizing space complexity in this problem.

warning

常见陷阱

外企场景
  • error

    Not handling the modulo operation properly could lead to incorrect results.

  • error

    Misunderstanding the state transitions can result in missing valid sequences.

  • error

    Using excessive space for the DP array instead of optimizing it to O(1).

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Consider variants where the student can have more than one late arrival, increasing the complexity of the state transitions.

  • arrow_right_alt

    Modify the problem by changing the number of absences allowed (e.g., 3 absences instead of 1).

  • arrow_right_alt

    Extend the problem to account for different types of attendance records (e.g., early arrivals, half-day attendance).

help

常见问题

外企场景

学生出勤记录 II题解:状态·转移·动态规划 | LeetCode #552 困难