第一次面试被伤害了。。
发奋努力的开始记录下自己刷topcoder。
200
public class Time{
public String whatTime(int seconds){
return String.format("%s:%s:%s", (seconds /3600), (seconds / 60 % 60), seconds % 60 );
}
}
简单的一道题,String.format,小白的我学到了这个
500
译码的问题,可以直接导出解码的递推公式,注意的是就是前后各多加一个位保存0,最开始的0是为了保证递推公式,最后边的0是为了保证有解,在没有解的情况下最后一位就会为‘1’
public class BinaryCode{
public String[] decode(String message){
String[] ret = new String[] {"", ""};
int[] a = new int[message.length() + 2];
for (int i = 0; i < 2; i++){
a[a.length - 1] = 0;
a[1] = i;
for (int j = 0; j < message.length(); j++){
a[j + 2] = (message.charAt(j) - '0') - a[j] - a[j + 1];
if (a[j + 2] < 0 || a[j + 2] > 1 || a[a.length - 1] != 0){
ret[i] = "NONE";
break;
}
ret[i] += a[j + 1];
}
}
return ret;
}
}
1100
一个图的遍历,每个边有cost,因为是个树,问题可以简化为,所有的边的cost的两倍和 - 从root的最大花销路径。因为最后的路径只走了一次,其他都是走了还要返回来。
public class PowerOutage{
int[] f;
int[] t;
int[] d;
int dfs(int s){
int res = 0;
for (int i = 0; i < f.length; i++){
if (f[i] == s){
res = Math.max(res, dfs(t[i]) + d[i]);
}
}
return res;
}
public int estimateTimeOut(int[] fromJunction, int[] toJunction, int[] ductLength){
f = fromJunction;
t = toJunction;
d = ductLength;
int n = fromJunction.length;
int total = 0;
for (int i = 0; i < n; i++){
total += 2*d[i];
}
return total - dfs(0);
}
}