笔试题(2021.7.21华为)

这篇博客记录了三道华为面试题目,分别是链路可靠性问题,通过建立图和深度优先搜索求解;出租车数量问题,考虑环形路线和时间处理;以及生产调度问题,利用优先队列优化。每道题目都提供了详细的思路和解决方案,适合面试准备和算法学习。
摘要由CSDN通过智能技术生成

2021.7.21
今晚华为的面试题,帮同学做的,记录一下
说实话还挺难的,基本都算中等题,而且光看题就得看半天

链路可靠性

在这里插入图片描述

思路

建图,dfs
我这里是用的哈希表,加数组的形式,也差不多

import java.util.*;
public class Main{

    static class Node{
        int target;
        int weight;

        public Node(int t, int w){
            target = t;
            weight = w;
        }
    }
    static Map<Integer, ArrayList<Node>> map;
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        map = new HashMap<>();
        while(sc.hasNext()){
            int start = sc.nextInt();
            int target = sc.nextInt();
            int weight = sc.nextInt();
            Node node = new Node(target, weight);
            ArrayList<Node> list = map.getOrDefault(start, new ArrayList<>());
            list.add(node);
            map.put(start, list);
        }
        int res = 0;
        Main main = new Main();
        for(int key : map.keySet()){
            int len = main.dfs(key, 0);
            res = Math.max(res, len);
        }
        System.out.println(res);
    }

    public int dfs(int start, int len){
        if(!map.containsKey(start)) {
            return len;
        }
        int res = 0;
        ArrayList<Node> list = map.get(start);
        for(int i = 0; i < list.size(); i++){
            Node node = list.get(i);
            int t = node.target;
            int w = node.weight;
            res = Math.max(res, dfs(t,len + w));
        }
        return res;
    }
}

同时运行的出租车数量

在这里插入图片描述

思路

就是遍历时间,看这个时间有没有车经过
注意路是环形的,两地经过的时间要处理一下

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int K = sc.nextInt();
        int[][] person = new int[K][3];

        int res = 0;
        for(int i = 0; i < K; i++){
            person[i][0] = sc.nextInt();
            person[i][1] = sc.nextInt();
            person[i][2] = sc.nextInt();
        }

        for(int i = 0; i <= 1000; i++){
            int temp = 0;
            for(int j = 0; j < K; j++){
                int pos1 = person[j][1];
                int pos2 = person[j][2];
                int diff = Math.abs(pos1 - pos2);
                int num = Math.min(diff, N - diff);
                if(person[j][0] <= i && i < person[j][0] + num * 5)
                    temp++;
            }
            res = Math.max(res, temp);
        }
        System.out.println(res);

    }
}

生产调度

在这里插入图片描述

思路

第二三次周赛时候的题吧,忘记了,第三道,当时有bug没发现
思路就是两个优先队列,一个放工序,一个放设备(记录用完的时间)
然后遍历时间,根据当前是否有设备,是否到达了完工时间来处理逻辑

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int K = sc.nextInt();
        int[][] shebei = new int[K][2];

        PriorityQueue<int[]> pq = new PriorityQueue<>((a,b) -> (a[1] == b[1] ? b[0] - a[0] : a[1] - b[1]));

        for(int i = 0; i < K; i++){
            shebei[i][0] = sc.nextInt();
            shebei[i][1] = sc.nextInt();
            pq.offer(shebei[i]);
        }

        int res = 0;
        PriorityQueue<Integer> time = new PriorityQueue<>();
        int num = 0;
        //遍历时间
        for(int i = 0; i < 1000 * 1000; i++) {
            if(pq.isEmpty())
                break;
            //如果到点了,机器停止
            while(!time.isEmpty() && time.peek() == i){
                time.poll();
            }
            //如果机器没有空闲,剪枝
            if(time.size() == N){
                i = time.peek() - 1;
            }
            while(!pq.isEmpty() && time.size() < N){
                int[] temp = pq.poll();
                time.offer(i + temp[0]);
                res = Math.max(res, i + temp[0]);
            }
        }
        System.out.println(res);
    }
}

算是做了一次笔试题吧,三道基本也都能写出来…
啥也不说了,还是去看八股了

  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值