Student Attendance Record II
up vote
4
down vote
favorite
I'm currently working on the Student Attendance Record II problem:
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 aftermod 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).
The idea is to use Dynamic Programming to use the results for smaller n to calculate the results of bigger n. I'm currently keeping two lists - one to track L and P and the other - to track A:
MODULO = 10 ** 9 + 7
class Solution(object):
def checkRecord(self, n):
lates = [[0, 0, 0], [1, 1, 0]] + [[0, 0, 0] for _ in xrange(2, n + 1)]
absences = [[0, 0, 0], [1, 0, 0]] + [[0, 0, 0] for _ in xrange(2, n + 1)]
for i in xrange(2, n + 1):
last_late_row = lates[i - 1]
last_late_row_sum = last_late_row[0] + last_late_row[1] + last_late_row[2]
last_absence_row = absences[i - 1]
last_absence_row_sum = last_absence_row[0] + last_absence_row[1] + last_absence_row[2]
lates[i] = last_late_row_sum % MODULO, last_late_row[0], last_late_row[1]
absences[i] = ((last_late_row_sum + last_absence_row_sum) % MODULO, last_absence_row[0], last_absence_row[1])
return (sum(lates[n]) + sum(absences[n])) % MODULO
The code works, but it does not pass the Time Limit requirements for big n. Even though, for n = 100000 LeetCode OJ runs it for only ~220ms.
How would you recommend to improve on running time?
python performance python-2.x dynamic-programming
add a comment |
up vote
4
down vote
favorite
I'm currently working on the Student Attendance Record II problem:
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 aftermod 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).
The idea is to use Dynamic Programming to use the results for smaller n to calculate the results of bigger n. I'm currently keeping two lists - one to track L and P and the other - to track A:
MODULO = 10 ** 9 + 7
class Solution(object):
def checkRecord(self, n):
lates = [[0, 0, 0], [1, 1, 0]] + [[0, 0, 0] for _ in xrange(2, n + 1)]
absences = [[0, 0, 0], [1, 0, 0]] + [[0, 0, 0] for _ in xrange(2, n + 1)]
for i in xrange(2, n + 1):
last_late_row = lates[i - 1]
last_late_row_sum = last_late_row[0] + last_late_row[1] + last_late_row[2]
last_absence_row = absences[i - 1]
last_absence_row_sum = last_absence_row[0] + last_absence_row[1] + last_absence_row[2]
lates[i] = last_late_row_sum % MODULO, last_late_row[0], last_late_row[1]
absences[i] = ((last_late_row_sum + last_absence_row_sum) % MODULO, last_absence_row[0], last_absence_row[1])
return (sum(lates[n]) + sum(absences[n])) % MODULO
The code works, but it does not pass the Time Limit requirements for big n. Even though, for n = 100000 LeetCode OJ runs it for only ~220ms.
How would you recommend to improve on running time?
python performance python-2.x dynamic-programming
Hey. Can you explain your algorithm?
– Raghuram Vadapalli
Sep 12 '17 at 17:37
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I'm currently working on the Student Attendance Record II problem:
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 aftermod 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).
The idea is to use Dynamic Programming to use the results for smaller n to calculate the results of bigger n. I'm currently keeping two lists - one to track L and P and the other - to track A:
MODULO = 10 ** 9 + 7
class Solution(object):
def checkRecord(self, n):
lates = [[0, 0, 0], [1, 1, 0]] + [[0, 0, 0] for _ in xrange(2, n + 1)]
absences = [[0, 0, 0], [1, 0, 0]] + [[0, 0, 0] for _ in xrange(2, n + 1)]
for i in xrange(2, n + 1):
last_late_row = lates[i - 1]
last_late_row_sum = last_late_row[0] + last_late_row[1] + last_late_row[2]
last_absence_row = absences[i - 1]
last_absence_row_sum = last_absence_row[0] + last_absence_row[1] + last_absence_row[2]
lates[i] = last_late_row_sum % MODULO, last_late_row[0], last_late_row[1]
absences[i] = ((last_late_row_sum + last_absence_row_sum) % MODULO, last_absence_row[0], last_absence_row[1])
return (sum(lates[n]) + sum(absences[n])) % MODULO
The code works, but it does not pass the Time Limit requirements for big n. Even though, for n = 100000 LeetCode OJ runs it for only ~220ms.
How would you recommend to improve on running time?
python performance python-2.x dynamic-programming
I'm currently working on the Student Attendance Record II problem:
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 aftermod 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).
The idea is to use Dynamic Programming to use the results for smaller n to calculate the results of bigger n. I'm currently keeping two lists - one to track L and P and the other - to track A:
MODULO = 10 ** 9 + 7
class Solution(object):
def checkRecord(self, n):
lates = [[0, 0, 0], [1, 1, 0]] + [[0, 0, 0] for _ in xrange(2, n + 1)]
absences = [[0, 0, 0], [1, 0, 0]] + [[0, 0, 0] for _ in xrange(2, n + 1)]
for i in xrange(2, n + 1):
last_late_row = lates[i - 1]
last_late_row_sum = last_late_row[0] + last_late_row[1] + last_late_row[2]
last_absence_row = absences[i - 1]
last_absence_row_sum = last_absence_row[0] + last_absence_row[1] + last_absence_row[2]
lates[i] = last_late_row_sum % MODULO, last_late_row[0], last_late_row[1]
absences[i] = ((last_late_row_sum + last_absence_row_sum) % MODULO, last_absence_row[0], last_absence_row[1])
return (sum(lates[n]) + sum(absences[n])) % MODULO
The code works, but it does not pass the Time Limit requirements for big n. Even though, for n = 100000 LeetCode OJ runs it for only ~220ms.
How would you recommend to improve on running time?
python performance python-2.x dynamic-programming
python performance python-2.x dynamic-programming
asked Apr 16 '17 at 3:41
alecxe
14.6k53377
14.6k53377
Hey. Can you explain your algorithm?
– Raghuram Vadapalli
Sep 12 '17 at 17:37
add a comment |
Hey. Can you explain your algorithm?
– Raghuram Vadapalli
Sep 12 '17 at 17:37
Hey. Can you explain your algorithm?
– Raghuram Vadapalli
Sep 12 '17 at 17:37
Hey. Can you explain your algorithm?
– Raghuram Vadapalli
Sep 12 '17 at 17:37
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
It's often helpful to explain the problem to someone else just to see things clearer and come up with a possible solution. Here is what I've just realized.
We don't actually need to keep the complete list during our calculations and only need a single row at a time. Let's reuse it and recalculate the cell values in the loop:
MODULO = 10 ** 9 + 7
class Solution(object):
def checkRecord(self, n):
lates = [1, 1, 0]
absences = [1, 0, 0]
for i in xrange(2, n + 1):
sum_lates = sum(lates)
lates = sum_lates % MODULO, lates[0], lates[1]
absences = (sum_lates + sum(absences) % MODULO, absences[0], absences[1])
return (sum(lates) + sum(absences)) % MODULO
I think I've seen this technique before and, if I remember correctly, it is called "Rolling Array".
add a comment |
up vote
0
down vote
accepted
Testing WB hats award (will remove the answer shortly).
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f160887%2fstudent-attendance-record-ii%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
It's often helpful to explain the problem to someone else just to see things clearer and come up with a possible solution. Here is what I've just realized.
We don't actually need to keep the complete list during our calculations and only need a single row at a time. Let's reuse it and recalculate the cell values in the loop:
MODULO = 10 ** 9 + 7
class Solution(object):
def checkRecord(self, n):
lates = [1, 1, 0]
absences = [1, 0, 0]
for i in xrange(2, n + 1):
sum_lates = sum(lates)
lates = sum_lates % MODULO, lates[0], lates[1]
absences = (sum_lates + sum(absences) % MODULO, absences[0], absences[1])
return (sum(lates) + sum(absences)) % MODULO
I think I've seen this technique before and, if I remember correctly, it is called "Rolling Array".
add a comment |
up vote
3
down vote
It's often helpful to explain the problem to someone else just to see things clearer and come up with a possible solution. Here is what I've just realized.
We don't actually need to keep the complete list during our calculations and only need a single row at a time. Let's reuse it and recalculate the cell values in the loop:
MODULO = 10 ** 9 + 7
class Solution(object):
def checkRecord(self, n):
lates = [1, 1, 0]
absences = [1, 0, 0]
for i in xrange(2, n + 1):
sum_lates = sum(lates)
lates = sum_lates % MODULO, lates[0], lates[1]
absences = (sum_lates + sum(absences) % MODULO, absences[0], absences[1])
return (sum(lates) + sum(absences)) % MODULO
I think I've seen this technique before and, if I remember correctly, it is called "Rolling Array".
add a comment |
up vote
3
down vote
up vote
3
down vote
It's often helpful to explain the problem to someone else just to see things clearer and come up with a possible solution. Here is what I've just realized.
We don't actually need to keep the complete list during our calculations and only need a single row at a time. Let's reuse it and recalculate the cell values in the loop:
MODULO = 10 ** 9 + 7
class Solution(object):
def checkRecord(self, n):
lates = [1, 1, 0]
absences = [1, 0, 0]
for i in xrange(2, n + 1):
sum_lates = sum(lates)
lates = sum_lates % MODULO, lates[0], lates[1]
absences = (sum_lates + sum(absences) % MODULO, absences[0], absences[1])
return (sum(lates) + sum(absences)) % MODULO
I think I've seen this technique before and, if I remember correctly, it is called "Rolling Array".
It's often helpful to explain the problem to someone else just to see things clearer and come up with a possible solution. Here is what I've just realized.
We don't actually need to keep the complete list during our calculations and only need a single row at a time. Let's reuse it and recalculate the cell values in the loop:
MODULO = 10 ** 9 + 7
class Solution(object):
def checkRecord(self, n):
lates = [1, 1, 0]
absences = [1, 0, 0]
for i in xrange(2, n + 1):
sum_lates = sum(lates)
lates = sum_lates % MODULO, lates[0], lates[1]
absences = (sum_lates + sum(absences) % MODULO, absences[0], absences[1])
return (sum(lates) + sum(absences)) % MODULO
I think I've seen this technique before and, if I remember correctly, it is called "Rolling Array".
answered Apr 16 '17 at 3:49
alecxe
14.6k53377
14.6k53377
add a comment |
add a comment |
up vote
0
down vote
accepted
Testing WB hats award (will remove the answer shortly).
add a comment |
up vote
0
down vote
accepted
Testing WB hats award (will remove the answer shortly).
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Testing WB hats award (will remove the answer shortly).
Testing WB hats award (will remove the answer shortly).
answered 4 hours ago
alecxe
14.6k53377
14.6k53377
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f160887%2fstudent-attendance-record-ii%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Hey. Can you explain your algorithm?
– Raghuram Vadapalli
Sep 12 '17 at 17:37