Advertisement
FSan

First python 2 medium code

Jun 6th, 2024 (edited)
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.62 KB | None | 0 0
  1. #!/bin/python
  2. import math
  3. import os
  4. import random
  5. import re
  6. import sys
  7. # Complete the 'minimumNumberOfPages' function below.
  8. # The function is expected to return an INTEGER.
  9. # The function accepts following parameters:
  10. # 1. INTEGER_ARRAY pages
  11. # 2. INTEGER days
  12.  
  13. def minimumNumberOfPages(pages, days):
  14.     def minimumNumberOfPages(pages, days):
  15.     def can_finish_with_max_pages(max_pages):
  16.         days_needed = 1
  17.         current_pages = 0
  18.         for page_count in pages:
  19.             if current_pages + page_count > max_pages:
  20.                 days_needed += 1
  21.                 current_pages = 0
  22.             current_pages += page_count
  23.             if days_needed > days:
  24.                 return False
  25.         return True
  26.    
  27.     left, right = max(pages), sum(pages)
  28.     result = -1
  29.    
  30.     while left <= right:
  31.         mid = (left + right) // 2
  32.         if can_finish_with_max_pages(mid):
  33.             result = mid
  34.             right = mid - 1
  35.         else:
  36.             left = mid + 1
  37.            
  38.     return result
  39.  
  40. n = 4
  41. pages = [2, 3, 4, 5]
  42. days = 5
  43. print(minimumNumberOfPages(pages, days))  # Expected Output: 4
  44.  
  45. n = 5
  46. pages = [1, 2, 3, 3, 7]
  47. days = 2
  48. print(minimumNumberOfPages(pages, days))  # Expected Output: -1
  49.  
  50.  
  51.  
  52. if __name__ == '__main__':
  53.     fptr = open(os.environ['OUTPUT_PATH'], 'w')
  54.     pages_count = int(raw_input().strip())
  55.     pages = []
  56.     for _ in xrange(pages_count):
  57.         pages_item = int(raw_input().strip())
  58.         pages.append(pages_item)
  59.     days = int(raw_input().strip())
  60.     result = minimumNumberOfPages(pages, days)
  61.     fptr.write(str(result) + '\n')
  62.     fptr.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement