dp
of size (n+1) x (n+1)
with all elements set to 0.len
, loop over all possible starting points i
of the range.j
of the range as i + len - 1
.(i, j)
, try every possible pivot k
from i
to j
.k + max(dp[i][k-1], dp[k+1][j])
.dp[i][j]
.dp
table, return dp[1][n]
as the answer.