首页 > 其他分享 >美团3.11笔试记录

美团3.11笔试记录

时间:2023-03-11 21:36:26浏览次数:37  
标签:Node scanner int 3.11 美团 笔试 queue new nodes

自我感觉良好,尤其是我很菜情况下,幸运的没有碰到比较离谱的题

第一题签到

第二题 有代价的走格子吃金币,二维dp,这题接近AC,为了赶时间没管

第三题 看流星,每个流行有起始时间和结束时间,找能看到的最多的流行和持续时间

第四题 坦克大战游戏模拟 死活不AC

第五题 森林递归遍历,难点在于构建

下面是部分题目代码

第三题

import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main
{
    static class Node
    {
        int start;
        int end;

        public Node(int start, int end)
        {
            this.start = start;
            this.end = end;
        }

        public int getStart()
        {
            return start;
        }

        public int getEnd()
        {
            return end;
        }
    }
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        scanner.nextLine();
        int[] start = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::valueOf).toArray();
        int[] end = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::valueOf).toArray();
        Node[] nodes = new Node[n];
        for (int i = 0; i < nodes.length; i++) {
            nodes[i] = new Node(start[i], end[i]);
        }
        Arrays.sort(nodes, Comparator.comparingInt(Node::getStart));
        PriorityQueue<Node> queue = new PriorityQueue<>(Comparator.comparing(Node::getEnd));
        int max = -1;
        int time = 0;
        int prevTime = 0;
        int nextTime = 0;
        int endTime = 0;
        int totalTime = 0;
        for (int i = 0; i < nodes.length; i++) {
            if (queue.isEmpty()) {
                queue.add(nodes[i]);
                time = nodes[i].start;
                continue;
            }

            queue.add(nodes[i]);
            time = nodes[i].start;
            while (!queue.isEmpty() && queue.peek().end < time) {
                Node poll = queue.poll();
                endTime = Math.max(endTime, poll.end);
            }
            if (queue.size() > max) {
                max = queue.size();
                prevTime = time;
            }
            else if (queue.size() == max) {
                totalTime += time - prevTime;
                prevTime = time;
            }
        }
        System.out.println(max);
        System.out.println(totalTime + 1);
    }
}

第五题


import java.util.*;
import java.util.stream.Collectors;

public class Main
{
    static class Node
    {
        LinkedList<Node> children;
        char color;

        public Node(int color)
        {

            this.color = (char)color;
            this.children = new LinkedList<>();
        }

    }
    static int ans;
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        scanner.nextLine();
        Node[] nodes = new Node[n];
        List<Node> collect = scanner.nextLine().chars().mapToObj(Node::new).collect(Collectors.toList());
        collect.toArray(nodes);
        int[] arr = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::valueOf).toArray();
        for (int i = 0; i < arr.length; i++) {
            int father = arr[i] - 1;
            int current = i + 1;
            nodes[father].children.add(nodes[current]);
        }
        dfs(nodes[0]);
        System.out.println(ans);
    }

    static class Counter
    {
        int red;
        int blue;

        void add(Counter counter)
        {
            this.red += counter.red;
            this.blue += counter.blue;
        }

        boolean isBalanced()
        {
            return this.red == this.blue;
        }
    }
    public static Counter dfs(Node node)
    {

        Counter counter = new Counter();
        for (Node child : node.children) {
            counter.add(dfs(child));
        }
        if (counter.isBalanced()) {
            ans++;
        }
        if(node.color == 'R') counter.red++;
        else if(node.color == 'B') counter.blue++;
        return counter;
    }
}

标签:Node,scanner,int,3.11,美团,笔试,queue,new,nodes
From: https://www.cnblogs.com/aminor/p/17207012.html

相关文章

  • 2023.3.11
    传递实参函数定义中可能包含多个形参,因此函数在调用时也就包含多个实参。向函数传递实参的方式很多,有位置实参、关键字实参,还可使用列表和字典。1.位置实参使用位置实参要......
  • 3.11号今日总结
    1.基本用法与事件处理:1)RadioButton(单选按钮)如题单选按钮,就是只能够选中一个,所以我们需要把RadioButton放到RadioGroup按钮组中,从而实现单选功能!先熟悉下如何使用Rad......
  • 2023.03.11.函数重载,引用等
    程序生成的过程:1.预处理:头文件的展开宏的替换预处理指令解析去掉注释2.编译:预处理后文件生成汇编文件.asm(汇编代码)词法解析,语法解析语义分析优化3.汇编:汇编文件进一......
  • 2023.03.11.命名空间
    c++命名空间为了区分不同库中相同名称的函数、类、变量等命名空间的定义使用关键字namespace,后跟命名空间的名称,如下所示:namespacenamespace_name{//代码声明}为......
  • 3.11学习总结
    今天学习了RecyclerViewbuild.gradle文件下//添加RecyclerView的依赖包implementation'androidx.recyclerview:recyclerview-selection:1.1.0'MainActivity.javapacka......
  • 一道某度的笔试算法题
    题目:给定一个长度为n,由非零整数成的数组,求连续子数组乘积为负数的个数example:55-33-1178ChatGPT的答案,基本正确,有个地方多+1了n=int(input())arr=list(......
  • 指针8道笔试题解析
    笔试题1:intmain(){inta[5]={1,2,3,4,5};int*ptr=(int*)(&a+1);printf("%d,%d",*(a+1),*(ptr-1));return0;}//程序的结果是什么?第一......
  • python 安装最新版3.11.2 for centos
    目录Python安装前的准备参考官方文档openssl下载地址openssl安装python的安装遇到的一些坑Python安装前的准备sudoyumupdate&&sudoyuminstall-yopenssl-deve......
  • 指针和数组笔试题解析
    在大多数情况下,数组名就是数组首元素的地址,但是有两种特殊情况:一、sizeof(数组名):当数组名单独放在sizeof内部,指的是整个数组二、&数组名:取的是整个数组的地址,但是结果和首......
  • 经典 SQL 数据库笔试题及答案整理!
    最近,有蛮多小伙伴在跳槽找工作,但对于年限稍短的软件测试工程师,难免会需要进行笔试,而在笔试中,基本都会碰到一道关于数据库的大题,今天这篇文章呢,就收录了下最近学员反馈上来......