阿里笔试7.29解法

2.n 个客栈依次排列,给出 n - 1 条路的权重。从任意一处出发,每走过一条路,该路的权重减 1,但得到 1 点利益。不能走权重为 0 的路。求最大利益。


解法代码
import java.util.*;

public class Main{
    public static void main(String[] args) {
        List<Integer> line = new LinkedList(Arrays.asList(5,2,4));
        Map<String,Integer> dp = new HashMap<>();
        Integer maxValue = -1;
        for(int i = 0;i < line.size() + 1;++i){
            List<Integer> lineTmp = new LinkedList<>(line);
            String key = toStringKey(lineTmp,i);
            dp.put(key,0);
        }
        while(!dp.isEmpty()){
            Map<String,Integer> preDp = dp;
            dp = new HashMap<>();
            for(Map.Entry<String,Integer> entry : preDp.entrySet()){
                String key = entry.getKey();
                List<Integer> tmpLine = getLineFromKey(key);
                Integer tmpIndex = getNodeIndexFromKey(key);
                Integer tmpVal = entry.getValue();
                if(tmpIndex == tmpLine.size()){
                    Integer lineSize = tmpLine.get(tmpIndex - 1);
                    if(lineSize > 0){
                        tmpLine.set(tmpIndex - 1,lineSize - 1);
                        tmpVal += 1;
                        tmpIndex -= 1;
                        String tmpKey = toStringKey(tmpLine,tmpIndex);
                        if(dp.containsKey(tmpKey)){
                            dp.put(tmpKey,Math.max(dp.get(tmpKey),tmpVal));
                            maxValue = Math.max(tmpVal,maxValue);
                        }else{
                            dp.put(tmpKey,tmpVal);
                            maxValue = Math.max(tmpVal,maxValue);
                        }
                    }
                }else if(tmpIndex == 0){
                    Integer lineSize = tmpLine.get(tmpIndex);
                    if(lineSize > 0){
                        tmpLine.set(tmpIndex,lineSize - 1);
                        tmpVal += 1;
                        tmpIndex += 1;
                        String tmpKey = toStringKey(tmpLine,tmpIndex);
                        if(dp.containsKey(tmpKey)){
                            dp.put(tmpKey,Math.max(dp.get(tmpKey),tmpVal));
                            maxValue = Math.max(tmpVal,maxValue);
                        }else{
                            dp.put(tmpKey,tmpVal);
                            maxValue = Math.max(tmpVal,maxValue);
                        }
                    }
                }else{
                    Integer lineSize1 = tmpLine.get(tmpIndex);
                    Integer lineSize2 = tmpLine.get(tmpIndex - 1);
                    if(lineSize1 > 0){
                        tmpLine.set(tmpIndex,lineSize1 - 1);
                        tmpVal += 1;
                        tmpIndex += 1;
                        String tmpKey = toStringKey(tmpLine,tmpIndex);
                        if(dp.containsKey(tmpKey)){
                            dp.put(tmpKey,Math.max(dp.get(tmpKey),tmpVal));
                            maxValue = Math.max(tmpVal,maxValue);
                        }else{
                            dp.put(tmpKey,tmpVal);
                            maxValue = Math.max(tmpVal,maxValue);
                        }
                        tmpIndex -= 1;
                        tmpVal -= 1;
                        tmpLine.set(tmpIndex,lineSize1);
                    }
                    if(lineSize2 > 0){
                        tmpLine.set(tmpIndex - 1,lineSize2 - 1);
                        tmpVal += 1;
                        tmpIndex -= 1;
                        String tmpKey = toStringKey(tmpLine,tmpIndex);
                        if(dp.containsKey(tmpKey)){
                            dp.put(tmpKey,Math.max(dp.get(tmpKey),tmpVal));
                            maxValue = Math.max(tmpVal,maxValue);
                        }else{
                            dp.put(tmpKey,tmpVal);
                            maxValue = Math.max(tmpVal,maxValue);
                        }
                    }
                }
            }
        }
        System.out.println(maxValue);

    }

    public static String toStringKey(List<Integer> line,Integer index){
        StringBuilder sb = new StringBuilder();
        for(Integer i : line){
            sb.append(i).append(":");
        }
        sb.append(index);
        return sb.toString();
    }

    public static List<Integer> getLineFromKey(String key){
        String[] lineAndIndex = key.split(":");
        List<Integer> data = new LinkedList<>();
        for(int i = 0;i < lineAndIndex.length - 1;++i){
            data.add(Integer.parseInt(lineAndIndex[i]));
        }
        return data;
    }

    public static Integer getNodeIndexFromKey(String key){
        String[] lineAndIndex = key.split(":");
        return Integer.parseInt(lineAndIndex[lineAndIndex.length - 1]);
    }
    
}


#笔试题目##阿里巴巴#
全部评论
看这代码长度,已经被吓到了
点赞 回复 分享
发布于 2020-07-30 11:57

相关推荐

11-14 16:13
已编辑
重庆科技大学 测试工程师
Amazarashi66:不进帖子我都知道🐮❤️网什么含金量
点赞 评论 收藏
分享
沉淀一会:**圣经 1.同学你面试评价不错,概率很大,请耐心等待;2.你的排名比较靠前,不要担心,耐心等待;3.问题不大,正在审批,不要着急签其他公司,等等我们!4.预计9月中下旬,安心过节;5.下周会有结果,请耐心等待下;6.可能国庆节前后,一有结果我马上通知你;7.预计10月中旬,再坚持一下;8.正在走流程,就这两天了;9.同学,结果我也不知道,你如果查到了也告诉我一声;10.同学你出线不明朗,建议签其他公司保底!11.同学你找了哪些公司,我也在找工作。
点赞 评论 收藏
分享
头像
11-26 16:06
已编辑
中南大学 后端
快手电商 后端 23k-35k
点赞 评论 收藏
分享
评论
2
1
分享
牛客网
牛客企业服务