This problem is a genuinely good one. It requires unbiased and patient consideration of all the possible edge cases.
Solution:
If we write down a few examples, we sense intuitively that we need to keep track of a few things in for every chapter.
We need to keep track of:
I feel just putting the code would be the best idea for this one. The basic principle is while keeping track of those 3 things, we loop through all the question numbers of a given chapter, incrementing "page" whenever question number exceeds limit for each chapter, and moving to the next chapter when the number of questions in that chapter is over.
Implementation is slightly tricky so I'll just post my code for this one:
Pretty good!
But my friend Tejas Rao made it even easier:
Solution:
If we write down a few examples, we sense intuitively that we need to keep track of a few things in for every chapter.
We need to keep track of:
- The current page number
- The number of pages required by each chapter
- The question numbers on each page
I feel just putting the code would be the best idea for this one. The basic principle is while keeping track of those 3 things, we loop through all the question numbers of a given chapter, incrementing "page" whenever question number exceeds limit for each chapter, and moving to the next chapter when the number of questions in that chapter is over.
Implementation is slightly tricky so I'll just post my code for this one:
def workbook(n, k, arr):
special = 0
page = 1
for i in range(len(arr)):
finalpage = page + math.ceil(arr[i] / k)
j = 1
offset = 0
while True:
if j > k:
page += 1
j = j - k
offset += k
if page > finalpage:
break
if (j + offset) > arr[i]:
break
if (j + offset) == page:
special += 1
j += 1
page = finalpage
return special
Pretty good!
But my friend Tejas Rao made it even easier:
def workbook(n, k, arr):
b = list()
c= 0
pg = 1
for i in range((n)):
x = arr[i]
for j in range(1,x+1):
print(j,pg)
if j==pg:
c+=1
if j == x:
continue
if j%k ==0:
pg+=1
pg+=1
return c
No comments:
Post a Comment