aboutsummaryrefslogtreecommitdiff
path: root/challenge-214/lubos-kolouch/python/ch-2.py
diff options
context:
space:
mode:
author冯昶 <fengchang@novel-supertv.com>2023-05-02 14:47:44 +0800
committer冯昶 <fengchang@novel-supertv.com>2023-05-02 14:47:44 +0800
commitfb913ef4e973a29450b92ca89588e839bf6635f6 (patch)
tree94abff55c6006a2b0a4b1da93a5e661753e00fea /challenge-214/lubos-kolouch/python/ch-2.py
parentefea8410c80647939ae587536a4f1f66dd2c3a4a (diff)
parent6cc2e38f43011f65d7deaf1e03cf55e4306a53e5 (diff)
downloadperlweeklychallenge-club-fb913ef4e973a29450b92ca89588e839bf6635f6.tar.gz
perlweeklychallenge-club-fb913ef4e973a29450b92ca89588e839bf6635f6.tar.bz2
perlweeklychallenge-club-fb913ef4e973a29450b92ca89588e839bf6635f6.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-214/lubos-kolouch/python/ch-2.py')
-rw-r--r--challenge-214/lubos-kolouch/python/ch-2.py43
1 files changed, 43 insertions, 0 deletions
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