[algorithm thousand question case] daily LeetCode punch in - 83. Student attendance record I

preface

Algorithm problem

  • Punching out an algorithm problem every day is not only a learning process, but also a sharing process
  • Tip: the problem-solving programming languages in this column are C# and Java
  • To maintain a state of learning every day, let's work together to become the great God of algorithm
  • Today is the 83rd day of punching out the force deduction algorithm

Algorithm problem

Example of original question: student attendance record I

Give you a string s to represent the attendance record of a student, and each character is used to mark the attendance of the day (absence, lateness, presence). The record contains only the following three characters:

  • 'A': Absent
  • 'L': Late
  • 'P': Present, Present

If students can meet the following two conditions at the same time, they can get attendance reward:

  • According to the total attendance, the student's absence from work ('A ') is strictly less than two days.
  • Students will not have a record of being late ('L ') for 3 consecutive days or more.

If the student can get attendance reward, return true; Otherwise, false is returned.

Example 1:

Input: s = "PPALLP"
Output: true
 Explanation: students are absent less than 2 times, and there is no record of continuous lateness for 3 days or more.

Example 2:

Input: s = "PPALLL"
Output: false
 Explanation: students are late for the last three consecutive days, so they do not meet the conditions of attendance reward.

Tips:

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

C# method: one-time traversal

Attendance records that can be rewarded require less than 2 absences and less than 3 consecutive lateness. To judge whether attendance records can be rewarded, you only need to traverse attendance records to judge whether these two conditions are met at the same time.

During the traversal, record the number of absences and continuous lateness, and update the number of absences and continuous lateness according to the traversed characters:

  • If 'A', i.e. absence, the number of absences will be increased by 1, otherwise the number of absences will remain unchanged;
  • If 'L' is encountered, i.e. late, the number of consecutive lateness will be increased by 1, otherwise the number of consecutive lateness will be cleared.

If the number of absences is greater than or equal to 2 or the number of consecutive latencies is greater than or equal to 3 after updating the number of absences and consecutive latencies, the attendance record does not meet the requirements for reward and returns false. If the attendance record does not meet the requirements for reward at the end of traversal, return true.

code:

public class Solution {
    public bool CheckRecord(string s) {
        int absents = 0, lates = 0;
        int n = s.Length;
        for (int i = 0; i < n; i++) {
            char c = s[i];
            if (c == 'A') {
                absents++;
                if (absents >= 2) {
                    return false;
                }
            }
            if (c == 'L') {
                lates++;
                if (lates >= 3) {
                    return false;
                }
            } else {
                lates = 0;
            }
        }
        return true;
    }
}

results of enforcement

adopt
 Execution time: 80 ms,At all C# Defeated 11.86% of users in submission
 Memory consumption: 36.4 MB,At all C# Beat 5.14% of users in submission

Java method: one-time traversal

Train of thought analysis Attendance records that can be rewarded require less than 2 absences and less than 3 consecutive lateness. To judge whether attendance records can be rewarded, you only need to traverse attendance records to judge whether these two conditions are met at the same time.

During the traversal, record the number of absences and continuous lateness, and update the number of absences and continuous lateness according to the traversed characters:

  • If 'A', i.e. absence, the number of absences will be increased by 1, otherwise the number of absences will remain unchanged;
  • If 'L' is encountered, i.e. late, the number of consecutive lateness will be increased by 1, otherwise the number of consecutive lateness will be cleared.

If the number of absences is greater than or equal to 2 or the number of consecutive latencies is greater than or equal to 3 after updating the number of absences and consecutive latencies, the attendance record does not meet the requirements for reward and returns false. If the attendance record does not meet the requirements for reward at the end of traversal, return true.

code:

class Solution {
    public boolean checkRecord(String s) {
        int absents = 0, lates = 0;
        int n = s.length();
        for (int i = 0; i < n; i++) {
            char c = s.charAt(i);
            if (c == 'A') {
                absents++;
                if (absents >= 2) {
                    return false;
                }
            }
            if (c == 'L') {
                lates++;
                if (lates >= 3) {
                    return false;
                }
            } else {
                lates = 0;
            }
        }
        return true;
    }
}

results of enforcement

adopt
 Execution time: 0 ms,At all Java  Defeated 100 in submission.00%User
 Memory consumption: 36.6 MB,At all Java Defeated 22 in submission.03%User

Complexity analysis

Time complexity: O( n )
Space complexity: O(1) 

summary

  • Today is the 83rd day of punching out the force deduction algorithm!
  • This paper uses C# and Java programming languages to solve problems
  • Some methods are also written by Likou God, and they are also shared while learning. Thanks again to the algorithm bosses

Posted on Wed, 24 Nov 2021 22:51:31 -0500 by wonderman