Problem: 2312. 卖木头块
文章目录
- 思路
- 复杂度
- Code
思路
👨🏫 灵神题解
复杂度
时间复杂度: O ( n m ( m + n ) ) O(nm(m+n)) O(nm(m+n))
空间复杂度: O ( n m ) O(nm) O(nm)
Code
class Solution {public long sellingWood(int n, int m, int[][] prices) {int[][] pr = new int[n+1][m + 1];for(int[] p : prices)pr[p[0]][p[1]] = p[2];long[][] f = new long[n+1][m+1];for(int i = 1; i <= n; i++)for(int j = 1; j <= m; j++){f[i][j] = pr[i][j];for(int k = 1; k <= j/2; k++)//垂直切割 f[i][j] = Math.max(f[i][j],f[i][k] + f[i][j-k]);for(int k = 1; k <= i/2; k++)f[i][j] = Math.max(f[i][j],f[k][j] + f[i - k][j]);}return f[n][m];}
}