Участник:ArtemTovkes/construct-target-array-with-multiple-sums

Материал из DISCOPAL
Перейти к: навигация, поиск

Python 3 https://leetcode.com/problems/construct-target-array-with-multiple-sums

class Solution:
  def isPossible(self, target: List[int]) -> bool:
    n = len(target)
    if n < 1:
      return False
    if n == 1:
      return target[0] == 1
    s = sum(target)
    while True:
      index_max = max(range(n), key=target.__getitem__)
      m = target[index_max]
      if m == 1:
        return True
      t = s - m
      if m < 1 + t:
        break
      r = s % t
      if r == 0:
        r = t
      target[index_max] = r
      s = r + t
    return False