Advertisement
Guest User

Untitled

a guest
Oct 26th, 2022
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.93 KB | Source Code | 0 0
  1. def _size_of_valley(valley: tuple) -> int:
  2.     return valley[2] - valley[0] + 1
  3. def _is_valley_or_flat(valley: tuple, blocks: list) -> bool:
  4.     if blocks[valley[1]] <= blocks[valley[0]] and blocks[valley[2]] >= blocks[valley[1]]:
  5.         if blocks[valley[2] - 1] > blocks[valley[1]]:
  6.             # Climbing upwards
  7.             if blocks[valley[2]] >= blocks[valley[2] - 1]:
  8.                 return True
  9.             else:
  10.                 return False
  11.         else:
  12.             # Going downards or staying flat
  13.             return True
  14.     else:
  15.         return False
  16. def solution(blocks):
  17.     """
  18.        blocks which gives widest valley/flat shape will be the maximum distance
  19.        Code is calculating widest valley/flat in linear time so that frogs can
  20.        go two sides of the valley which would give maximum distance
  21.        valley data structure => (beginning, deepest_point, end, size)
  22.    """
  23.     widest_valley = (0, 0, 0, 1)
  24.     current_valley = (0, 0, 0, 1)
  25.     # (point and height)
  26.     flatness_beginning_point = (1, blocks[0])
  27.     for i in range(1, len(blocks)):
  28.         # Change deepest point if necessary
  29.         if blocks[current_valley[1]] <= blocks[i]:
  30.             tmp_valley = (current_valley[0], current_valley[1], i, current_valley[3] + 1)
  31.         else:
  32.             tmp_valley = (current_valley[0], i, i, current_valley[3] + 1)
  33.         if _is_valley_or_flat(tmp_valley, blocks):
  34.             current_valley = tmp_valley
  35.             if blocks[i] != flatness_beginning_point[1]:
  36.                 flatness_beginning_point = (i, blocks[i])
  37.             if _size_of_valley(widest_valley) < _size_of_valley(current_valley):
  38.                 widest_valley = current_valley
  39.         else:
  40.             if flatness_beginning_point[1] >= blocks[i]:
  41.                 current_valley = (flatness_beginning_point[0], i, i, 1)
  42.             else:
  43.                 current_valley = (i, i, i, 1)
  44.     return _size_of_valley(widest_valley)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement