from itertools import accumulate,combinations
def solution(cookie):
acc = set(accumulate([0] + cookie))
answer = [abs(x - y) // 2 for x, y in combinations(acc, 2) if (m := x + y) % 2 == 0 and m // 2 in acc]
return max(answer, default=0)
알고리즘
쿠키 갯수의 누적합만 구해준다면 수식만으로 답을 풀 수 있다.
from itertools import accumulate,combinations
def solution(cookie):
acc = set(accumulate([0] + cookie))
answer = [abs(x - y) // 2 for x, y in combinations(acc, 2) if (m := x + y) % 2 == 0 and m // 2 in acc]
return max(answer, default=0)