Skip to content

Instantly share code, notes, and snippets.

@igorvanloo
Created February 2, 2025 03:20
Show Gist options
  • Save igorvanloo/38ff6af8020f4b9baba7a37e450bace3 to your computer and use it in GitHub Desktop.
Save igorvanloo/38ff6af8020f4b9baba7a37e450bace3 to your computer and use it in GitHub Desktop.
p149
def MaxContiguousSubarray(A):
currMax = 0
currSum = 0
for i in range(len(A)):
currSum = max(currSum + A[i], A[i])
currMax = max(currMax, currSum)
return currMax
def MaxsumSubseq(M):
currMax = 0
l = len(M[0])
for i in range(l):
currMax = max(currMax,
MaxContiguousSubarray(M[i]), #Horizontal
MaxContiguousSubarray([M[j][i] for j in range(l)]), #Vertical
MaxContiguousSubarray([M[l - j - 1][i - j - 1] for j in range(i + 1)]), #Diagonal
MaxContiguousSubarray([M[i + j][i + j] for j in range(l - i)]), #Diagonal
MaxContiguousSubarray([M[l - j - 1][l - i + j - 1] for j in range(i + 1)]), #Anti-Diagonal
MaxContiguousSubarray([M[j][i - j] for j in range(i + 1)]) #Anti-Diagonal
)
return currMax
def genM():
M = [[0 for _ in range(2000)] for _ in range(2000)]
S = [0] + [0]*(4*10**6)
for k in range(1, 56):
S[k] = (100003 - 200003*k + 300007*pow(k, 3, 10**6)) % 10**6 - 500000
for k in range(56, 4*10**6 + 1):
S[k] = (S[k - 24] + S[k - 55]) % 10**6 - 500000
S.pop(0)
for i, s in enumerate(S):
M[i//2000][i % 2000] = s
print(MaxsumSubseq(genM())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment