Created
August 12, 2018 16:57
-
-
Save domarps/fa3390a1e7d9f563447ec55683980450 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def maximum_under_budget_1d(array, budget): | |
st, max_sum, max_len = 0, 0, 0 | |
#print(array) | |
for ed in range(len(array)): | |
max_sum += array[ed] | |
while max_sum > budget and st <= ed: | |
max_sum -= array[st] | |
st += 1 | |
max_len = max(max_len, ed - st + 1) | |
return max_len | |
def maximum_under_budget_2d(matrix, budget): | |
M,N = len(matrix), len(matrix[0]) | |
max_area = 0 | |
for i in range(M): | |
array1d = [0 for _ in range(N)] | |
for j in range(i,M): | |
array1d = [array1d[k] + matrix[j][k] for k in range(N)] | |
width = maximum_under_budget_1d(array1d, budget) | |
height = j - i + 1 | |
max_area = max(max_area, height * width) | |
return max_area | |
print(maximum_under_budget_2d([[6,6,2,5],[3,5,9,7],[2,2,2,3],[1,4,3,3]], 20)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment