美团9.3日 4+1编程题

第一题100%.  第二题86%. 第三题91%. 第四题64%没时间改进了。专项编程没写。



第二题:
import java.util.*;

/**
 * @author bty
 * @date 2022/9/3
 * @since 1.8
 **/
public class Main {


    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();

        Map<Integer, Integer> val2Idx = new HashMap<>();

        for (int i = 0; i < n; i++) {
            int temp = in.nextInt();
            val2Idx.put(temp,i);
        }


        int[] res = new int[n];

        for (Integer val : val2Idx.keySet()) {
            int mex = calcMex(val2Idx.keySet(), val);
            Integer idx = val2Idx.get(val);
            res[idx] = mex;
        }

        for (int i : res) {
            System.out.print(i);
            System.out.print(" ");
        }


    }

    public static int calcMex(Set<Integer> set,int val){

        int result = 0;
        while(set.contains(result)){
            if(val==result) return val;
            result++;
        }
        return result;
    }
}






第三题
import java.util.*;

/**
 * @author bty
 * @date 2022/9/3
 * @since 1.8
 * 
 **/
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] parents = new int[n + 1];
        parents[1] = 1;
        for (int i = 2; i <= n; i++) {
            int parent = in.nextInt();
            parents[i] = parent;
        }
        in.nextLine();
        String marks = in.nextLine();
        char[] markArr = marks.toCharArray();

        Map<Integer, Set<Character>> result = new HashMap<>();

        Map<Integer,Set<Integer>> fathers = new HashMap<>();
        for (int i = 1; i <= n; i++) {
            fathers.put(i,new HashSet<>());
            result.put(i,new HashSet<>());
        }

        for (int i = 1; i <= n; i++) {
            int tempJ = i;
            while (parents[tempJ] != tempJ) {
                fathers.get(i).add(parents[tempJ]);
                tempJ = parents[tempJ];
            }
            fathers.get(i).add(i);
        }
        for (Integer son : fathers.keySet()) {
            Set<Integer> fatherSet = fathers.get(son);
            for (Integer father : fatherSet) {
                result.get(father).add(markArr[son-1]);
            }
        }


        for (int i = 1; i <= n; i++) {
            System.out.print(result.get(i).size());
            System.out.print(" ");
        }


    }
}


第四题
import java.util.Scanner;

/**
 * @author bty
 * @date 2022/9/3
 * @since 1.8
 **/
public class Main3 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        int k = in.nextInt();

        int[] position = new int[m+1];
        for (int i = 1; i <= m; i++) {
            int curPosition = in.nextInt();
            position[i] = curPosition;
        }
        int[] ideaSalary = new int[m+1];
        for (int i = 1; i <= m; i++) {
            int curSalary = in.nextInt();
            ideaSalary[i] = curSalary;
        }
        int[] lowSalary = new int[m+1];
        for (int i = 1; i <=m; i++) {
            int curSalary = in.nextInt();
            lowSalary[i] = curSalary;
        }

        int[] dp = new int[m+1];

        if(position[1]==k){
            dp[1] = ideaSalary[1];
        } else{
            dp[1] = lowSalary[1];
        }
        if(m==1){
            System.out.println(dp[1]);
            return;
        }

        for (int i = 2; i <= m; i++) {


            for (int j = 1; j < i; j++) {
                if(position[j]!=position[i]){
                    dp[i] = Math.max(dp[j]+lowSalary[i],dp[i]);
                } else {
                    dp[i] = Math.max(dp[j]+ideaSalary[i],dp[i]);
                }
            }
        }
        System.out.println(dp[m]);
    }
}



#美团笔试#
全部评论
hi~同学,秋招遇“寒气”,牛客送温暖啦!23届秋招笔面经有奖征集中,参与就得牛客会员7天免费体验,最高赢300元京东卡!戳我去看>>>https://www.nowcoder.com/link/zhengjipinglun
点赞 回复 分享
发布于 2022-09-03 20:32 北京

相关推荐

2024-12-29 15:37
已编辑
西华大学 图像识别
程序员牛肉:去不了,大厂算法卡学历吧
点赞 评论 收藏
分享
2024-12-26 13:00
太原理工大学 Java
会飞的猿:简历没啥大问题啊,感觉是缺少了实习经历。多投投先找个中小厂过渡一下吧
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务