0
点赞
收藏
分享

微信扫一扫

1255. 得分最高的单词集合 状态压缩动态规划


方法:动态规划

  1. 计数,映射到26个字母
  2. 填充单个单词的得分情况,顺便验证能否用现有字符组成单词
  3. 尝试继续填写,dp前26代表占用字母数目,第27位代表得分

class Solution {
public long sellingWood(int m, int n, int[][] prices) {
long[][] dp = new long[m+1][n+1];
for(int [] price:prices){
dp[price[0]][price[1]]=price[2];
}

for(int i = 1; i <= m; i++){
for(int j = 1; j <= n; j++){
for(int t = 0 ; t<=j;t++) dp[i][j]=Math.max(dp[i][j],dp[i][t]+dp[i][j-t]);
for(int t = 0 ; t<=i;t++) dp[i][j]=Math.max(dp[i][j],dp[t][j]+dp[i-t][j]);
}
}
return dp[m][n];
}

}

时间复杂度:O(n*2^n)
空间复杂度:O(2^n)

举报

相关推荐

0 条评论