Paint HouseII lc原题需bug free 可能还会问你时间复杂度 最好用标准答案
严氏北美IT公司面试真题汇总和解答论坛 :: LinkedIn公司面试真题: 注册用户可以看到隐藏题目:2017年下半年上机题,8 9 10三个月的onsite面试真题 :: 动态规划:二维数组:PaintHouse系列
Page 1 of 1
Paint HouseII lc原题需bug free 可能还会问你时间复杂度 最好用标准答案
//time O(n*k) optimized way ,space O(1)
public int minCostII(int[][] matrix) {
if (matrix == null || matrix.length==0||matrix[0].length == 0)
return 0;
int preMin = 0, preSecondMin = 0, preColor = -1;
for (int i = 0; i < matrix.length; i++) {
int curMin = Integer.MAX_VALUE;
int curSecndMin = Integer.MAX_VALUE;
int curColor = -1;
for (int j = 0; j < matrix[0].length; j++) {
if(preColor == j)
matrix[i][j] =matrix[i][j]+preSecondMin;
else
matrix[i][j] =matrix[i][j]+preMin;
if (matrix[i][j] < curMin) {
curSecndMin = curMin;
curMin = matrix[i][j];
curColor = j;
} else if (matrix[i][j] < curSecndMin) {
curSecndMin = matrix[i][j];
}
}
preMin = curMin;
preSecondMin = curSecndMin;
preColor = curColor;
}
return preMin;
}
public int minCostII(int[][] matrix) {
if (matrix == null || matrix.length==0||matrix[0].length == 0)
return 0;
int preMin = 0, preSecondMin = 0, preColor = -1;
for (int i = 0; i < matrix.length; i++) {
int curMin = Integer.MAX_VALUE;
int curSecndMin = Integer.MAX_VALUE;
int curColor = -1;
for (int j = 0; j < matrix[0].length; j++) {
if(preColor == j)
matrix[i][j] =matrix[i][j]+preSecondMin;
else
matrix[i][j] =matrix[i][j]+preMin;
if (matrix[i][j] < curMin) {
curSecndMin = curMin;
curMin = matrix[i][j];
curColor = j;
} else if (matrix[i][j] < curSecndMin) {
curSecndMin = matrix[i][j];
}
}
preMin = curMin;
preSecondMin = curSecndMin;
preColor = curColor;
}
return preMin;
}
Similar topics
» Paint House I lc原题 需bug free
» Is Somorphic高频题需要bug free
» Combination SumII 不难最好bug free
» Combination SumIV 不难最好bug free 面试官可能让你还要用DP实现 dp在其它post中
» Length Of Longest Common Subsequence should within 3 minutes+bug free b4 onsite
» Is Somorphic高频题需要bug free
» Combination SumII 不难最好bug free
» Combination SumIV 不难最好bug free 面试官可能让你还要用DP实现 dp在其它post中
» Length Of Longest Common Subsequence should within 3 minutes+bug free b4 onsite
严氏北美IT公司面试真题汇总和解答论坛 :: LinkedIn公司面试真题: 注册用户可以看到隐藏题目:2017年下半年上机题,8 9 10三个月的onsite面试真题 :: 动态规划:二维数组:PaintHouse系列
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|