From c9649ccd3cfb48e26c19886bb399bd1820715f8c Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Mon, 24 Apr 2023 16:02:44 +0200 Subject: Challenge 214 LK Perl Python --- challenge-214/lubos-kolouch/python/ch-2.py | 43 ++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 challenge-214/lubos-kolouch/python/ch-2.py (limited to 'challenge-214/lubos-kolouch/python/ch-2.py') diff --git a/challenge-214/lubos-kolouch/python/ch-2.py b/challenge-214/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..72d7207b50 --- /dev/null +++ b/challenge-214/lubos-kolouch/python/ch-2.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + + +def max_score(numbers): + + def helper(numbers, memo): + if not numbers: + return 0 + + key = tuple(numbers) + if key in memo: + return memo[key] + + max_score = 0 + i = 0 + while i < len(numbers): + j = i + while j < len(numbers) and numbers[j] == numbers[i]: + j += 1 + + length = j - i + next_numbers = numbers[:i] + numbers[j:] + score = length * length + helper(next_numbers, memo) + max_score = max(max_score, score) + i = j + + memo[key] = max_score + return max_score + + return helper(numbers, {}) + + +# Test cases +numbers1 = [2, 4, 3, 3, 3, 4, 5, 4, 2] +numbers2 = [1, 2, 2, 2, 2, 1] +numbers3 = [1] +numbers4 = [2, 2, 2, 1, 1, 2, 2, 2] + +print(max_score(numbers1)) # Output: 23 +print(max_score(numbers2)) # Output: 20 +print(max_score(numbers3)) # Output: 1 +print(max_score(numbers4)) # Output: 40 -- cgit