首页 > 其他分享 >[左神面试指南] 其他题目[下]篇

[左神面试指南] 其他题目[下]篇

时间:2023-11-26 21:12:44浏览次数:34  
标签:指南 int 左神 面试 num arr2 arr1 new public

CD79 一种消息接收并打印的结构设计

public class CD79_1
{
    public static class Node
    {
        public int num;
        public Node next;

        public Node(int num)
        {
            this.num = num;
        }
    }

    public static class MessageBox
    {
        private HashMap<Integer, Node> headMap;
        private HashMap<Integer, Node> tailMap;
        private int lastPrint;

        public MessageBox()
        {
            headMap = new HashMap<Integer, Node>();
            tailMap = new HashMap<Integer, Node>();
            lastPrint = 0;
        }

        public void receive(int num)
        {
            if (num < 1) return;
            Node cur = new Node(num);
            headMap.put(num, cur);
            tailMap.put(num, cur);
            if (tailMap.containsKey(num - 1))
            {
                tailMap.get(num - 1).next = cur;
                tailMap.remove(num - 1);
                headMap.remove(num);
            }
            if (headMap.containsKey(num + 1))
            {
                cur.next = headMap.get(num + 1);
                tailMap.remove(num);
                headMap.remove(num + 1);
            }
            if (headMap.containsKey(lastPrint + 1))
                print(num);
        }

        private void print(int num)
        {
            Node node = headMap.get(++lastPrint);
            headMap.remove(lastPrint);
            while (node != null)
            {
                System.out.println(node.num + " " + num);
                node = node.next;
                lastPrint++;
            }
            tailMap.remove(--lastPrint);
        }
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        MessageBox messageBox = new MessageBox();
        int n = in.nextInt();
        for (int i = 0; i < n; i++)
            messageBox.receive(in.nextInt());
    }
}

CD80 随时找到数据流的中位数

public class CD80_1
{
    public static PriorityQueue<Integer> minHeap = new PriorityQueue<>();
    public static PriorityQueue<Integer> maxHeap = new PriorityQueue<>((o1, o2) -> o2 - o1);
    public static int count = 0;

    public static void Insert(Integer num)
    {
        count++;
        if (count % 2 != 0)
        {
            minHeap.add(num);
            maxHeap.add(minHeap.poll());
        }
        else
        {
            maxHeap.add(num);
            minHeap.add(maxHeap.poll());
        }
    }

    public static Double GetMedian()
    {
        if (minHeap.size() == maxHeap.size())
            return (minHeap.peek() + maxHeap.peek()) / 2.0;
        else
            return maxHeap.peek() * 1.0;
    }
}

CD81 在两个长度相等的排序数组中找到上中位数❓

/*❓*/
public class CD81_1
{
    public static int solution(int[] arr1, int[] arr2)
    {
        if (arr1 == null || arr2 == null || arr1.length != arr2.length)
        {
            throw new RuntimeException("Your arr is invalid!");
        }
        int start1 = 0;
        int end1 = arr1.length - 1;
        int start2 = 0;
        int end2 = arr2.length - 1;
        int mid1 = 0;
        int mid2 = 0;
        int offset = 0;
        while (start1 < end1)
        {
            mid1 = (start1 + end1) / 2;
            mid2 = (start2 + end2) / 2;
            // 元素个数为奇数,则 offset 为 0;
            // 元素个数为偶数,则 offset 为 1
            offset = ((end1 - start1 + 1) & 1) ^ 1;
            if (arr1[mid1] > arr2[mid2])
            {
                end1 = mid1;
                start2 = mid2 + offset;
            }
            else if (arr1[mid1] < arr2[mid2])
            {
                start1 = mid1 + offset;
                end2 = mid2;
            }
            else
                return arr1[mid1];
        }
        return Math.min(arr1[start1], arr2[start2]);
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] arr1 = new int[n];
        int[] arr2 = new int[n];
        for (int i = 0; i < n; i++)
            arr1[i] = in.nextInt();
        for (int i = 0; i < n; i++)
            arr2[i] = in.nextInt();
        System.out.println(solution(arr1, arr2));
    }
}

CD82 在两个排序数组中找到第 k 小的数❓

/*❓*/
public class CD82_1
{
    public static int solution(int[] arr1, int[] arr2, int kth)
    {
        if (arr1 == null || arr2 == null)
            throw new RuntimeException("Your arr is invalid!");
        if (kth < 1 || kth > arr1.length + arr2.length)
            throw new RuntimeException("K is invalid!");
        int[] longs = arr1.length >= arr2.length ? arr1 : arr2;
        int[] shorts = arr1.length < arr2.length ? arr1 : arr2;
        int l = longs.length;
        int s = shorts.length;
        if (kth <= s)
            return getUpMedian(shorts, 0, kth - 1, longs, 0, kth - 1);
        if (kth > l)
        {
            if (shorts[kth - l - 1] >= longs[l - 1])
                return shorts[kth - l - 1];
            if (longs[kth - s - 1] >= shorts[s - 1])
                return longs[kth - s - 1];
            return getUpMedian(shorts, kth - l, s - 1, longs, kth - s, l - 1);
        }
        if (longs[kth - s - 1] >= shorts[s - 1])
            return longs[kth - s - 1];
        return getUpMedian(shorts, 0, s - 1, longs, kth - s, kth - 1);
    }

    public static int getUpMedian(int[] a1, int s1, int e1, int[] a2, int s2, int e2)
    {
        int mid1 = 0;
        int mid2 = 0;
        int offset = 0;
        while (s1 < e1)
        {
            mid1 = (s1 + e1) / 2;
            mid2 = (s2 + e2) / 2;
            offset = ((e1 - s1 + 1) & 1) ^ 1;
            if (a1[mid1] > a2[mid2])
            {
                e1 = mid1;
                s2 = mid2 + offset;
            }
            else if (a1[mid1] < a2[mid2])
            {
                s1 = mid1 + offset;
                e2 = mid2;
            }
            else
                return a1[mid1];
        }
        return Math.min(a1[s1], a2[s2]);
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), m = in.nextInt(), k = in.nextInt();
        int[] arr1 = new int[n];
        int[] arr2 = new int[m];
        for (int i = 0; i < n; i++)
            arr1[i] = in.nextInt();
        for (int i = 0; i < m; i++)
            arr2[i] = in.nextInt();
        System.out.println(solution(arr1, arr2, k));
    }
}

CD83 两个有序数组间相加和的 Top k 问题

public class CD83_1
{
    public static class Node implements Comparable<Node>
    {
        public int index1;
        public int index2;
        public int val;

        public Node(int index1, int index2, int val)
        {
            this.index1 = index1;
            this.index2 = index2;
            this.val = val;
        }

        @Override
        public int compareTo(Node o)
        {
            return o.val - this.val;
        }
    }

    public static int[] solution(int[] arr1, int[] arr2, int k)
    {
        PriorityQueue<Node> que = new PriorityQueue<>();
        HashSet<String> set = new HashSet<>();
        int res[] = new int[k];
        int len1 = arr1.length - 1, len2 = arr2.length - 1, index = -1;
        que.add(new Node(len1, len2, arr1[len1] + arr2[len2]));
        while (++index < k)
        {
            Node pollNode = que.poll();
            int curIdx1 = pollNode.index1, curIdx2 = pollNode.index2;
            res[index] = pollNode.val;
            if (!set.contains(curIdx1 + "_" + (curIdx2 - 1)))
            {
                set.add(curIdx1 + "_" + (curIdx2 - 1));
                que.add(new Node(curIdx1, curIdx2 - 1, arr1[curIdx1] + arr2[curIdx2 - 1]));
            }
            if (!set.contains((curIdx1 - 1) + "_" + curIdx2))
            {
                set.add((curIdx1 - 1) + "_" + curIdx2);
                que.add(new Node(curIdx1 - 1, curIdx2, arr1[curIdx1 - 1] + arr2[curIdx2]));
            }
        }
        return res;
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), k = in.nextInt();
        int[] arr1 = new int[n];
        int[] arr2 = new int[n];
        for (int i = 0; i < n; i++)
            arr1[i] = in.nextInt();
        for (int i = 0; i < n; i++)
            arr2[i] = in.nextInt();
        int[] res = solution(arr1, arr2, k);
        System.out.println(Arrays.stream(res).mapToObj(String::valueOf).collect(Collectors.joining(" ")));
    }
}

CD84 出现次数的 Top k 问题

public class CD84_1
{
    public static class Node implements Comparable<Node>
    {
        public String str;
        public int times;

        public Node(String s, int t)
        {
            str = s;
            times = t;
        }

        @Override
        public int compareTo(Node o)
        {
            if (this.times != o.times)
                return o.times - this.times;
            else
                return this.str.compareTo(o.str);
        }
    }

    public static void solution(String[] arr, int topK)
    {
        HashMap<String, Integer> map = new HashMap<>();
        PriorityQueue<Node> que = new PriorityQueue<>();
        for (String word : arr)
        {
            if (!map.containsKey(word))
                map.put(word, 1);
            else
                map.put(word, map.get(word) + 1);
        }
        for (String word : map.keySet())
            que.add(new Node(word, map.get(word)));
        while (topK-- > 0)
        {
            Node pollNode = que.poll();
            System.out.println(pollNode.str + " " + pollNode.times);
        }
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), k = in.nextInt();
        in.nextLine();
        String[] arr = new String[n];
        for (int i = 0; i < n; i++)
            arr[i] = in.nextLine();
        solution(arr, k);
    }
}

CD85 Manacher 算法✍

CD86 Manacher 算法(进阶)✍

CD87 KMP 算法✍

CD88 丢棋子问题✍

CD89 画匠问题✍

CD90 邮局选址问题✍

标签:指南,int,左神,面试,num,arr2,arr1,new,public
From: https://www.cnblogs.com/VividBinGo/p/17857960.html

相关文章

  • Java开发者的Python快速进修指南:面向对象--高级篇
    首先,让我来介绍一下今天的主题。今天我们将讨论封装、反射以及单例模式。除此之外,我们不再深入其他内容。关于封装功能,Python与Java大致相同,但写法略有不同,因为Python没有修饰符。而对于反射来说,我认为它比Java简单得多,不需要频繁地获取方法和属性,而是有专门的方法来实现。最后,我......
  • SQL JOIN 子句:合并多个表中相关行的完整指南
    SQLJOINJOIN子句用于基于它们之间的相关列合并来自两个或更多表的行。让我们看一下“Orders”表的一部分选择:OrderIDCustomerIDOrderDate1030821996-09-1810309371996-09-1910310771996-09-20然后,看一下“Customers”表的一部分选择:CustomerID......
  • Day10 数据类型扩展及面试题讲解
    publicclassDemo03{publicstaticvoidmain(String[]args){//整数扩展:进制二进制0b十进制八进制00十六进制0xinti=10;inti2=010;//八进制0inti3=0x10;//十六进制0x0~9A~F16System.out.println(i);......
  • 100元预算,轻松涨粉1000!腾讯运营面试秘籍大揭秘!
    大家好啊!小米在这里~很高兴又有机会和大家见面啦!最近小米参加了一场腾讯的运营面试,遇到了一个超有趣的问题:如果让你运营一个公众号,近期需要增加1000个关注,预算100元,怎么完成这个目标?说实话,小米当时差点就跟面试官热聊了起来,因为这个问题太过火辣刺激了!好了,不多扯了,让我们一起揭开这......
  • [左神面试指南] 其他题目[中]篇
    CD66并查集的实现publicclassCD66_1{publicstaticclassSolution{int[]f;publicSolution(intn){f=newint[n];Arrays.fill(f,-1);}privateintfind(inta){......
  • Android平台GB28181设备接入模块开发填坑指南
    技术背景为什么要开发Android平台GB28181设备接入模块?这个问题不再赘述,在做Android平台GB28181客户端的时候,媒体数据这块,我们已经有了很好的积累,因为在此之前,我们就开发了非常成熟的RTMP推送、轻量级RTSP服务、录像模块、针对音视频的对接处理单元。这让我们在做Android平台GB28181......
  • 了解 ESP32 FreeRTOS:初学者指南
    原文:https://www.cnblogs.com/intomcu/p/17297020.html了解ESP32FreeRTOS:初学者指南ESP32FreeRTOS是什么?如何使用FreeRTOS?哪些常用的函数?xTaskCreate()vTaskDelete()vTaskDelay()xTicksToDelay()xSemaphoreCreateBinary()xSemaphoreGive()xSemaphore:要释放的信......
  • 小天才手表系统指南
    ,:       小天才手表系统指南(仅限Z6及以下旧款小天才主流机型)强烈推荐使用PDF版本阅读  【声明】允许规范转载并说明来源,不可用于商业用途。 【重要提示】本教程配套其他up的讲解视频点击观看 【支持机型】降级和ROOT:Z2Z3Z5A/PRO/QZ6(不含巅峰......
  • Java开发者的Python快速进修指南:面向对象基础
    当我深入学习了面向对象编程之后,我首先感受到的是代码编写的自由度大幅提升。不同于Java中严格的结构和约束,Python在面向对象的实现中展现出更加灵活和自由的特性。它使用了一些独特的关键字,如self和cls,这些不仅增强了代码的可读性,还提供了对类和实例的明确引用。正如Java,Python也......
  • Java开发者的Python快速进修指南:面向对象进阶
    在上一期中,我们对Python中的对象声明进行了初步介绍。这一期,我们将深入探讨对象继承、组合以及多态这三个核心概念。不过,这里不打算赘述太多理论,因为我们都知道,Python与Java在这些方面的主要区别主要体现在语法上。例如,Python支持多重继承,这意味着一个类可以同时继承多个父类的属......