美团8.13笔试代码

package meituan.bishi.one;

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int t = sc.nextInt();
        int[] ans = new int[n];
        for (int i = 0;i < n;i++){
            ans[i] = sc.nextInt();
        }
        Arrays.sort(ans);
        int sum = 0;
        int time = 0;
        for (int i = 0;i < n;i++){
            if(time + t > ans[i]){
                sum++;
            }else{
                time += t;
            }
        }
        System.out.println(sum);

    }
}
package meituan.bishi.two;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[][] res = new int[n][m];
        int len = sc.nextInt();
        String sb = sc.next();
        int num = 0;
        for (int i = 0;i < n;i++){
            for (int j = 0;j < m;j++){
                res[i][j] = 1;
            }
        }
        int row = 0;
        int col = 0;
        res[0][0] = 0;
        num++;
        int cnt = 0;
        for (char c : sb.toCharArray()){
            if(c=='W'){
                row--;
                cnt++;
                if(res[row][col]==1){
                    res[row][col]=0;
                    num++;
                    if (num==n*m){
                        System.out.println("Yes");
                        System.out.println(cnt);
                    }
                }
            }
            if(c=='A'){
                col--;
                cnt++;
                if(res[row][col]==1){
                    res[row][col]=0;
                    num++;
                    if (num==n*m){
                        System.out.println("Yes");
                        System.out.println(cnt);
                    }
                }
            }
            if(c=='S'){
                row++;
                cnt++;
                if(res[row][col]==1){
                    res[row][col]=0;
                    num++;
                    if (num==n*m){
                        System.out.println("Yes");
                        System.out.println(cnt);
                    }
                }
            }
            if(c=='D'){
                col++;
                cnt++;
                if(res[row][col]==1){
                    res[row][col]=0;
                    num++;
                    if (num==n*m){
                        System.out.println("Yes");
                        System.out.println(cnt);
                    }
                }
            }
        }
        if (num<n*m){
            System.out.println("No");
            System.out.println(n*m-num);
        }
    }
}
package meituan.bishi.three;

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

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] dianshu = new int[n];
        for(int i = 0;i < n;i++){
            dianshu[i] = sc.nextInt();
        }
        Deque<Integer> deque = new LinkedList<>();
        for (int i = n - 1;i >= 0;i--){
            if(deque.isEmpty() || deque.size()==1){
                deque.addFirst(dianshu[i]);
            }else{
                deque.addFirst(dianshu[i]);
                for (int j = 0;j < 2;j++){
                    int temp = deque.removeLast();
                    deque.addFirst(temp);
                }
            }
        }
        for (Integer integer : deque){
            System.out.print(integer + " ");
        }
    }
}
package meituan.bishi.four;

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] res = new int[n];
        for(int i = 0;i < n;i++){
            res[i] = sc.nextInt();
        }
        Map<Integer, List<Integer>> map = new HashMap<>();
        for (int i = 0;i < n;i++){
            if (map.containsKey(res[i])){
                map.get(res[i]).add(i);
            }else{
                map.put(res[i],new ArrayList<>());
                map.get(res[i]).add(i);
            }
        }
        int sum = 0;
        for (int i = 0;i < n;i++){
            for (int j = i + 1;j < n;j++){
                if (map.containsKey(3*res[j]-res[i])){
                    for (Integer integer : map.get(3*res[j]-res[i])){
                        if (integer > j){
                            sum++;
                        }
                    }
                }
            }
        }
        System.out.println(sum);
    }

}

package meituan.bishi.five;


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] tree = new int[n+1];
        for (int i = 1;i < n+1;i++){
            tree[i] = sc.nextInt();
        }
        Main main = new Main();
        long ans = main.dfs(1,n,tree);
        System.out.println(ans);
    }

    long dfs(int root,int n,int[] tree){
        if (root > n){
            return 0;
        }
        return tree[root] + Math.max(dfs(2*root,n,tree),dfs(2*root+1,n,tree));
    }

}





#美团##美团笔试##秋招进度交流#
全部评论
A了4.8 第四题是交卷之后才发现自己在装map时候有部分value的列表值没add,真的蠢 唉
点赞 回复 分享
发布于 2022-08-13 18:33
楼主很厉害,祝早日上岸。——————小尾巴—————— 楼主要不来试试深信服科技捏?给自己多一个选择,提前批最后两天,本科22w+,硕士25w+ 【投递方式】 👉投递:PC端hr.sangfor.com / 移动端:关注公众号【深信服招聘】 👉内推码:NTANUxG 写我码可随时找我查看进度
点赞 回复 分享
发布于 2022-08-19 18:45 四川

相关推荐

头发暂时没有的KFC总裁:找廉价劳动力罢了
点赞 评论 收藏
分享
评论
6
12
分享

创作者周榜

更多
牛客网
牛客企业服务