LeetCode Weekly Contest 28
[2017-04-16]
Student Attendance Record I
You are given a string representing an attendance record for a student. The record only contains the following three characters:
- ‘A’ : Absent.
- ‘L’ : Late.
- ‘P’ : Present.
A student could be rewarded if his attendance record doesn’t contain more than one ‘A’ (absent) or more than two continuous ‘L’ (late).
You need to return whether the student could be rewarded according to his attendance record.
Example 1:
Example 2:
Code
|
|
Optimal Division
Given a list of positive integers, the adjacent integers will perform the float division. For example, [2,3,4] -> 2 / 3 / 4.
However, you can add any number of parenthesis at any position to change the priority of operations. You should find out how to add parenthesis to get the maximum result, and return the corresponding expression in string format. Your expression should NOT contain redundant parenthesis.
Example:
Note:
- The length of the input array is [1, 10].
- Elements in the given array will be in range [2, 1000].
- There is only one optimal division for each test case.
code
|
|
Split Assembled Strings
Given a list of strings, you could assemble these strings together into a loop. Among all the possible loops, you need to find the lexicographically biggest string after cutting and making one breakpoint of the loop, which will make a looped string into a regular one.
So, to find the lexicographically biggest string, you need to experience two phases:
- Assemble all the strings into a loop, where you can reverse some strings or not and connect them in the same order as given.
- Cut and make one breakpoint in any place of the loop, which will make a looped string into a regular string starting from the character at the cutting point.
And your job is to find the lexicographically biggest one among all the regular strings.
Example:
Note:
- The input strings will only contain lowercase letters.
- The total length of all the strings will not over 1000.
Code:
|
|
Student Attendance Record II
Given a positive integer n, return the number of all possible attendance records with length n, which will be regarded as rewardable. The answer may be very large, return it after mod 109 + 7.
A student attendance record is a string that only contains the following three characters:
- ‘A’ : Absent.
- ‘L’ : Late.
- ‘P’ : Present.
A record is regarded as rewardable if it doesn’t contain more than one ‘A’ (absent) or more than two continuous ‘L’ (late).
Example 1:
Note: The value of n won’t exceed 100,000.
code
dp[i]the number of all possible attendance (without ‘A’) records with length i :
- end with “P”: dp[i-1]
- end with “PL”: dp[i-2]
- end with “PLL”: dp[i-3]
- end with “LLL”: is not allowed
so dp[i] = dp[i-1] + dp[i-2] + dp[i-3]
the number of all possible attendance (with ‘A’) records with length n:
∑dp[i] *dp[n-1-i] i = 0,1,…,n-1
Time Complexity O(n)
Space Complexity O(n)
(In code nums[i+1] means dp[i])
|
|