华为 4.17 笔试AK

暑期实习投到现在大大小小笔试做了好多,基本都只过一题多点,昨天刚刚被xhs虐完,今天做华子的笔试,AK了,头一回AK。算是这段时间找实习处处碰壁唯一能稍微舒缓一下情绪的事情了。

希望华子能给机会

第一题 数据量不大,狠狠暴力。我这做法并不优。不过数据量小

// 4.17 1
import java.util.HashMap;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        HashMap<String,Integer> m = new HashMap<>();
        scan.nextLine();
        String s = scan.nextLine();
        String str = s;
        String t = rem(str);
        while(!str.equals(t)){
            str = t;
            t = rem(t);
            if(t.equals("0")) {
                str = t;
                break;
            }
        }
        System.out.print(str);

    }
    static String rem(String s){
        String[] cards = s.split(" ");
        int n = cards.length;
        StringBuilder ans = new StringBuilder();
        int i = 0;
        while(i<n){
            String t = cards[i];
            int ti = i;
            int count = 0;
            while(i<n&&t.equals(cards[i])){
                count++;
                i++;
            }
            if(count==3){
                continue;
            }else if(count == 4){
                ans.append(t);
                ans.append(' ');
            }else{
                for (int j = ti; j < i; j++) {
                    ans.append(t);
                    ans.append(' ');
                }
            }
        }
        if(ans.length() == 0)return "0";
        ans.deleteCharAt(ans.length()-1);
        return ans.toString();
    }

}

第二题 建树+bfs写的。因为是字符串,建树挺麻烦的,应该还有更好的方法,例如并查集应该能做。

//4.17 2
import java.util.*;

public class Main {
    static Set<String> fa = new HashSet<>();
    static HashMap<String, Set<String>> lm = new HashMap<>();
    static HashMap<String, int[]> nm = new HashMap<>();
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int M = scan.nextInt();
        int N = scan.nextInt();
        scan.nextLine();
        String[] problems = new String[N];
        for (int i = 0; i < N; i++) {
            problems[i] = scan.nextLine();
        }
        for(String line:problems){
            String[] items = line.split(" ");
            String father = items[1];
            String child = items[0];
            int lev = Integer.parseInt(items[2]);
            int num = Integer.parseInt(items[3]);

            if(father.equals("*")){
                fa.add(child);
                if(!lm.containsKey(child)) lm.put(child,new HashSet<>());
                if(!nm.containsKey(child)) nm.put(child,new int[]{0,0});
            }else {
                if(!lm.containsKey(father))lm.put(father,new HashSet<>());
                if(!nm.containsKey(father)) nm.put(father,new int[]{0,0});
                if(!lm.containsKey(child))lm.put(child,new HashSet<>());
                if(!nm.containsKey(child)) nm.put(child,new int[]{0,0});
                lm.get(father).add(child);
            }
            nm.get(child)[lev]+=num;
        }
        int ans = 0;
        for (String f:fa) {
            int x = dfs(f);
            if(x>M)ans++;
        }
        System.out.println(ans);
    }
    static int dfs(String root){
        Set<String> x = lm.get(root);
        int[] my = nm.get(root);
        int cost = 5*my[0]+2*my[1];
        for(String y:x){
            cost += dfs(y);
        }
        return cost;
    }
}

第三题 dijkstra,求完再加个索引一块排序。 不过奇怪的是,给的数据n = 1e4,矩阵都1e8了,java竟然只跑180ms,不知道时间是怎么算的

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int[][] g = new int[n][n];
        int[] cap = new int[n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                g[i][j] = scan.nextInt();
            }
        }
        for (int i = 0; i < n; i++) {
            cap[i] = scan.nextInt();
        }
        int s = scan.nextInt();
        int ms = scan.nextInt();
        int[][] res = dijkstra(g,s);
        Arrays.sort(res,(a,b)->a[0]==b[0]?a[1]-b[1]:a[0]-b[0]);
        StringBuilder ans = new StringBuilder();
        int sum = 0;
        for (int i = 1; i < n; i++) {
            sum+=cap[res[i][1]];
            ans.append(res[i][1]);
            ans.append(' ');
            if(sum>=ms)break;
        }
        ans.deleteCharAt(ans.length()-1);
        System.out.println(ans);

    }

    static int[][] dijkstra(int[][]g,int s){
        int n = g.length;
        int[] dist = new int[n];
        Arrays.fill(dist,Integer.MAX_VALUE);
        dist[s] = 0;
        boolean[] vis = new boolean[n];
        for (int i = 0; i < n; i++) {
            int t  = -1;
            for (int j = 0; j < n; j++) {
                if(!vis[j]&&(t==-1||(dist[t]>dist[j]))){
                    t = j;
                }
            }
            vis[t] = true;
            for (int j = 0; j < n; j++) {
                if(g[t][j]<0) continue;
                if(dist[t]+g[t][j]<dist[j]){
                    dist[j] = dist[t]+g[t][j];
                }
            }
        }

        int[][]res = new int[n][2];
        for (int i = 0; i < n; i++) {
            res[i][0] = dist[i];
            res[i][1] = i;
        }

        return res;

    }

}

全部评论
膜带佬
2
送花
回复
分享
发布于 04-17 21:48 北京
恭喜
点赞
送花
回复
分享
发布于 04-18 19:06 湖南
网易互娱
校招火热招聘中
官网直投
并查集怎么做?如果不是一颗树的根结点就已经是风险云服务了,这种情况下也得加一吧。我感觉得并查集前加个拓扑排序保证并查集merge顺序才行把。
点赞
送花
回复
分享
发布于 04-21 09:31 陕西
山大学弟吗
点赞
送花
回复
分享
发布于 04-22 19:48 广东
第三题我本来拍了个bellman-ford上去想骗点分,结果直接过了,就是数据太弱了。当然一方面它输入是邻接矩阵,所以数据量不可能真的到给出的范围。
点赞
送花
回复
分享
发布于 04-23 13:04 上海
佬。找到实习了吗
点赞
送花
回复
分享
发布于 04-23 16:18 湖北
题目是啥呀 佬
点赞
送花
回复
分享
发布于 04-23 18:44 江苏

相关推荐

&nbsp;&nbsp;&nbsp;&nbsp;美团2月底实习开的最早,也像大家一样陆续投递。整个过程很辛苦,累得扣,因为本人假期没有咋学,hc一下子在三月开了不少,边准备八股边准备笔面。三月到四月中旬给笔面:美团&nbsp;腾讯&nbsp;饿了么&nbsp;阿里国际&nbsp;北森&nbsp;为旌&nbsp;360&nbsp;TCL&nbsp;拼多多&nbsp;小红书&nbsp;阿里灵犀互娱&nbsp;….拿到的有美团(到店)&nbsp;为旌(算法)&nbsp;TCL(制造)&nbsp;阿里灵犀(游测)&nbsp;阿里国际(约了二面&nbsp;但是我这边最近才确定意向,后边肯定不参加了)以下就我个人的简单看法吧!其实宏观来看,纯软件服务的开发红利期过了,但是互联网是靠提供服务和功能来盈利,这个是万变不离其宗,也接触过很多初创公司,或者行业大牛创业的,自己曾经也试过创业,在他们看来技术不是第一位的(%30),商业模式也就是如何盈利,谁为你买单(%60)是第一位,当然我并不认为是完全正确的,但如果从商人的角度,他们对了!我们只是个打工人,从老板的角度审视自己就真没必要push。因此,光靠纯软件提供的服务,市场被划分的很明确了,饱和了吧,还有一层意思是这个市场目前更多是需要维持,再往前一步肯定不是靠纯软件,你可以是软件的表现形式,但你的核心就是纯软那估计走得很艰难,说白了就是你和当前发展的方向和趋势有点出入。只需要维持就很有趣了,每年疯狂的毕业生都在卷,我也其中一个呀,现状呢?!大家有所感受吧,很难!而且也要看运气。问题大家可能都比我看的还清楚,但我想说能换赛道就换吧,我实验室很好的哥们,人家大胆迈出了第一步,做强化学习,做AI方向,他在这方面有基础,至少得到的机会更多。新能源&nbsp;大模型&nbsp;芯片&nbsp;AI&nbsp;这些赛道,把握住吃三年左右的苦入门这个行业,第五年看清行业,这些行业红利期将持续十年左右甚至更多,赚钱就看那一会,别总想着一辈子都在挣钱,现在大家说的铁饭碗我觉得其实并不对了,没有什么真的铁饭碗,我家人有好多是体制内的,退休后都不建议我们继续….我很有幸因为去之前做过相关的东西,然后得到了*通的车载芯片的dsp算法工程师实习&nbsp;至少我认为这是换赛道的入门机会,最好就是从实习开始….&nbsp;对了,可能大家觉得我蠢也好,在面试的时候,技术面试官最后问了我一个问题,不是技术问题,他说你认为什么是后端开发?我当时其实很死板,因为在我的认知中,做软件后台功能服务开发的业务就是后端开发…. 也想听听大家对目前软开发展趋势的看法,我也提升认知。
点赞 评论 收藏
转发
10 50 评论
分享
牛客网
牛客企业服务