注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
代码如下,我觉得时间复杂度是n*k^2*log(k)请大神指正,谢谢!
- class Solution {
- public int minCostII(int[][] costs) {
- if (costs == null || costs.length == 0) return 0;
- if (costs[0] == null || costs[0].length == 0) return 0;
- int n = costs.length;
- int k = costs[0].length;
- int[][] dp = new int[n][k];
- for (int i = 0; i < k; i++) {
- dp[0][i] = costs[0][i];
- }
- for (int i = 1; i < n; i++) {
- for (int j = 0; j < k; j++) {
- dp[i][j] = costs[i][j] + findMin(i - 1, j, dp);
- }
- }
- int res = Integer.MAX_VALUE;
- for (int i = 0; i < k; i++) {
- res = Math.min(res, dp[n - 1][i]);
- }
- return res;
- }
-
- public int findMin(int house, int col, int[][] dp) {
- Queue<Integer> minheap = new PriorityQueue<>();
- for (int j = 0; j < dp[0].length; j++) {
- if (j == col) continue;
- minheap.add(dp[house][j]);
- }
- return minheap.poll();
- }
- }
复制代码
[/i][/i][/i][/i][/i] |