SRM144_DIV2

第一次面试被伤害了。。
发奋努力的开始记录下自己刷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);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值