Skip to content

Latest commit

 

History

History
45 lines (36 loc) · 2.2 KB

풀이_LC551.md

File metadata and controls

45 lines (36 loc) · 2.2 KB

🧙🏼 551. Student Attendance Record I

  • Date : 2022.02.07(월)
  • Time : 15분

Problem

  • You are given a string s representing an attendance record for a student where each character signifies whether the student was absent, late, or present on that day. The record only contains the following three characters:
    • 'A': Absent.
    • 'L': Late.
    • 'P': Present.
  • The student is eligible for an attendance award if they meet both of the following criteria:
    • The student was absent ('A') for strictly fewer than 2 days total.
    • The student was never late ('L') for 3 or more consecutive days.
  • Return true if the student is eligible for an attendance award, or false otherwise.

Constraints

  • 1 <= s.length <= 1000
  • s[i] is either 'A', 'L', or 'P'.

Example

  • Input: s = "PPALLP"
  • Output: true
  • Explanation: The student has fewer than 2 absences and was never late 3 or more consecutive days.

풀이

    def checkRecord(self, s: str) -> bool:
        count = 0
        for i in range(len(s)):
            if s[i] == 'A':
                count += 1
            if count == 2:
                return False
            if s[i] == 'L' and i <= len(s) - 3:
                if s[i + 1] == 'L' and s[i + 2] == 'L':
                    return False
        return True

        

: 조건을 그대로 풀이하면 되는 쉬운 문제였다. 먼저 A(결석)이 두 번 이상 있으면 안된다. 그래서 A를 만나면 count를 1씩 증가시키고 2개 이상일때를 계속 검사해준 후 바로 return False를 때려주었다. 그리고 지각이 3번 연속 되는 순간 탈락이기 때문에 먼저 L(지각)을 만나는 순간 그 뒤에 2개가 더 올 수 있는지 검사릏 했다. 만약 맨 마지막날에 지각을 했다면 그 뒤에는 연속이 될 수 없으니깐 검사를 안해도 되는 것이기 때문이다. 그래서 길이 또한 검사를 한 후에 뒤에 연속으로 지각할 수 있는 조건이 된다면 다음과 그 다음날도 검사를 해서 모두 지각이라면 False를 바로 return 해주었다. 이 과정을 find 함수로 진행해도 되지만 그것은 효율성이 좋지 않기때문에 이런방식으로 진행해주었다.