Created
November 29, 2021 07:22
-
-
Save Shaddyjr/b759ea36701323f1e1cf6a864e5fb33a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# source: https://www.hackerrank.com/challenges/lisa-workbook/problem | |
# video: https://youtu.be/G2_o-FqdCTs | |
def workbook(n, k, arr): | |
count = 0 | |
current_page = 1 # NOTE: we're indexing starting at 1 | |
for question_count in arr: # O(n) | |
pages_added, remaining_questions = divmod(question_count, k) | |
for new_chapter_page in range(1, pages_added + 1): # O(pmodk) => O(100mod1) => O(100) => O(1) | |
first_question_on_page = (new_chapter_page - 1) * k + 1 | |
last_question_on_page = first_question_on_page + (k - 1) | |
# check for "special" question => page index will be within question range | |
count += first_question_on_page <= current_page <= last_question_on_page | |
current_page += 1 | |
# handle remainder questions | |
if remaining_questions: # will add another page | |
first_question_on_page = pages_added * k + 1 | |
last_question_on_page = first_question_on_page + (remaining_questions - 1) | |
# check for "special" question => page index will be within question range | |
count += first_question_on_page <= current_page <= last_question_on_page | |
current_page += 1 | |
return count # Total Time Complexity: O(n) * O(pmodk) => O(n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hii sir , it does not give the output