摸牌+闪电


记一次某大厂笔试

摸牌:

拿道题目就是一通模拟,很快啊,超时了

package letcodeLearn;

import java.util.Deque;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Pro1 {

    public static int eval(Deque<Integer> list){
        PriorityQueue<Integer> res = new PriorityQueue<>(list);
        int count = 0;
        while(list.size() != 0){
            if(list.peek() == res.peek()){
                list.pollFirst();
                res.poll();
            }else{
                int tmp = list.pollFirst();
                list.addLast(tmp);
            }
            count++;
        }
        return count;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        Deque<Integer> list = new LinkedList<>();
        while(n-- > 0){
            list.addLast(scanner.nextInt());
        }
        System.out.println(eval(list));
    }
}

通过16%,超时。

排序二叉树求闪电个数:

题目真长,差10秒提交。

package letcodeLearn;

import java.util.*;

public class Pro3 {

    public static Node root;
    public static int count;
    public static int length;

    public static void insertBST(int key){
        Node p = root;
        Node prev = null;
        while(p != null){
            prev = p;
            if(key < p.value){
                p = p.left;
            }else if(key > p.value){
                p = p.right;
            }else{
                return;
            }
        }
        if(root == null){
            root = new Node(key);
        }else if (key < prev.value){
            prev.left = new Node(key);
        }else{
            prev.right = new Node(key);
        }
    }

    public static void eval(Node root,int lor,int len){
        if(root == null){
            return;
        }
        if(len > length){
            length = len;
            count=1;
        }else if(len == length){
            count += 1;
        }
        if (lor == 1) {
            eval(root.right,0,len + 1);
        }else{
            eval(root.left,1,len + 1);
        }
    }
    public static void t(Node root){
        if(root == null) return;
        eval(root,0,1);
        eval(root,1,1);
        t(root.left);
        t(root.right);
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        List<Integer> list = new ArrayList<>();
        while(n-- > 0){
            list.add(scanner.nextInt());
        }
        for (int i = 0; i < list.size(); i++) {
            insertBST(list.get(i));
        }
        t(root);
        System.out.print(length+" "+count);
    }
}

class Node{
    public int value;
    public Node left;
    public Node right;

    Node(){

    }
    Node(int key){
        this.value = key;
        this.left = null;
        this.right = null;
    }
    Node(int key,Node left,Node right){
        this.value = key;
        this.left = left;
        this.right = right;
    }
}

本地测试通过,没提交上。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值