nums = [-5,2,4,1,2,3] dp = [[0 for i in range(len(nums))] for j in range(len(nums))] for i in range(0, len(nums)): dp[i][i] = nums[i] for i in range(0, len(nums)): for j in range(i+1, len(nums)): dp[i][j] = dp[i][j-1] * nums[j] max = dp[0][0] for i in dp: for j in i: if j > max: max = j print(max) print(dp)
Public
Calculating something multiple times is redundant - use variables to store result of some calculation.
len()
is called multiple times
Whatever you want to code, usually someone has already done that. And usually there are some tools to cover your case. And usually you can find them using Google.
[0] * 5 == [0, 0, 0, 0, 0]
length = len(nums) dp = [ [0] * length for _ in range(length)]
This code is not really needed or may be simplified
range()
already starts with 0
for i in range(len(nums)):
Whatever you want to code, usually someone has already done that. And usually there are some tools to cover your case. And usually you can find them using Google.
To count maximum of all elements, just use max()
function on all the elements.
from itertools import chain all_elements = chain.from_iterable(dp) max_value = max(all_elements)
Never overwrite built-in names, like list
, sum
or max
- if later someone wants to use built-in function, he will find it replaced with your variable. If you really need to use one of those names, append underscore: sum_ = 15
.
Create new review request