Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/python
- import math
- import os
- import random
- import re
- import sys
- # Complete the 'minimumNumberOfPages' function below.
- # The function is expected to return an INTEGER.
- # The function accepts following parameters:
- # 1. INTEGER_ARRAY pages
- # 2. INTEGER days
- def minimumNumberOfPages(pages, days):
- def minimumNumberOfPages(pages, days):
- def can_finish_with_max_pages(max_pages):
- days_needed = 1
- current_pages = 0
- for page_count in pages:
- if current_pages + page_count > max_pages:
- days_needed += 1
- current_pages = 0
- current_pages += page_count
- if days_needed > days:
- return False
- return True
- left, right = max(pages), sum(pages)
- result = -1
- while left <= right:
- mid = (left + right) // 2
- if can_finish_with_max_pages(mid):
- result = mid
- right = mid - 1
- else:
- left = mid + 1
- return result
- n = 4
- pages = [2, 3, 4, 5]
- days = 5
- print(minimumNumberOfPages(pages, days)) # Expected Output: 4
- n = 5
- pages = [1, 2, 3, 3, 7]
- days = 2
- print(minimumNumberOfPages(pages, days)) # Expected Output: -1
- if __name__ == '__main__':
- fptr = open(os.environ['OUTPUT_PATH'], 'w')
- pages_count = int(raw_input().strip())
- pages = []
- for _ in xrange(pages_count):
- pages_item = int(raw_input().strip())
- pages.append(pages_item)
- days = int(raw_input().strip())
- result = minimumNumberOfPages(pages, days)
- fptr.write(str(result) + '\n')
- fptr.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement