美团后台开发笔试编程题代码

1.图的遍历

答案就是所有边的两边然后减去离1最远点到1的距离

// 图的遍历
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
typedef long long ll;
vector e[N];
int ans;
void dfs(int x, int fa, int w) {
    for(auto to : e[x]) {
        if (to == fa) {
            continue;
        }
        dfs(to, x, w+1);
    }
    ans = max(ans, w);
}
int main() {
    int n;
    while(~scanf("%d", &n)) {
        for(int i = 0; i < n; ++ i) {
            e[i].clear();
        }
        for(int i = 1; i < n; ++ i) {
            int x, y;
            scanf("%d%d", &x, &y);
            e[x].push_back(y);
            e[y].push_back(x);
        }
        ans = 0;
        dfs(1, -1, 0);
        // cout << ans << endl;
        printf("%d\n", (n-1)*2-ans);
    }
    return 0;
}

2.区间统计

直接扫描一下区间就可以了

// 区间统计
#include <bits/stdc++.h>
 using namespace std;
typedef long long ll;
const int N =1e5+5;
int cnt[N];
int a[N];
int main() {
    int n, t, k;
    while(~scanf("%d%d%d", &n, &k, &t)) {
        memset(cnt, 0, sizeof(cnt));
        ll res = 0;
        ll ans = 0;
        for(int i = 1; i <= n; ++ i) {
            scanf("%d", &a[i]);
            cnt[a[i]] ++;
            if(cnt[a[i]] == t) {
                ans ++;
            }
            if(i >= k) {
                if(ans > 0) res ++;
                cnt[a[i-k+1]] --;
                if(cnt[a[i-k+1]] == t-1) {
                    ans --;
                }
            }
        }
        printf("%lld\n", res);
    }
    return 0;
}
#Java##美团##笔试题目##题解#
全部评论
呜呜呜,题目没看清楚是N-1条边,还以为会有环路,哭了
点赞 回复
分享
发布于 2018-09-06 21:38
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, k, t;         n = sc.nextInt();         k = sc.nextInt();         t = sc.nextInt();         int[] arr = new int[n];         int[] cnt = new int[100005];         for (int i = 0; i < n; i++) {             arr[i] = sc.nextInt();         }         Arrays.fill(cnt, 0);         int count = 0;         //计算初始窗口中满足条件的数字个数         for (int i = 0; i < k - 1; i++) {             if (++cnt[arr[i]] == t) count++;         }         //向右滑动窗口,判断进入和离开窗口的数字是否满足条件         int ans = 0;         for (int i = k - 1; i < n; i++) {             if (++cnt[arr[i]] == t) count++;             if (count > 0) ans++;             if (--cnt[arr[i - k + 1]] == t - 1) count--;         }         System.out.println(ans);     } }
点赞 回复
分享
发布于 2018-09-07 13:07
滴滴
校招火热招聘中
官网直投
老哥 你就不能早发五分钟?
点赞 回复
分享
发布于 2018-09-06 21:22
大佬大佬
点赞 回复
分享
发布于 2018-09-06 21:23
可以加QQ,抱大腿吗
点赞 回复
分享
发布于 2018-09-06 21:24
有没有java的大佬……我应聘的客户端
点赞 回复
分享
发布于 2018-09-06 21:24
优秀优秀
点赞 回复
分享
发布于 2018-09-06 21:24
油皮,油皮
点赞 回复
分享
发布于 2018-09-06 21:27
大佬,第一题想了好久没有弄出来
点赞 回复
分享
发布于 2018-09-06 21:27
#include <bits/stdc++.h> #include <stdio.h> #define N 100005 using namespace std;  struct edge  {      int from;      int to;      edge(int i=0,int j=0)      {          from = i;to = j;      }  };  vector<edge>mp;  int visit[N];  int num[N];  int depth = 1;  int maxdepth = 1;  int cmp(edge l,edge r)  {      return l.from < r.from;  }  void dfs(int from)  {      edge tmp(from,0);      depth++;      maxdepth = max(maxdepth,depth);      auto pos = lower_bound(mp.begin(),mp.end(),tmp,cmp);      for(auto it=pos;it!=mp.end();it++)      {          if(it->from == from && !visit[it->to])          {              //printf("%d to %d\n",it->from,it->to);              visit[it->to] = 1;              num[depth] ++;              dfs(it->to);          }          if(it->from != from)             break;      }      depth--;  }  int main()  {      int n;      int ans = 0;      cin>>n;      for(int i=0;i<n-1;i++)      {          int a,b;          cin>>a>>b;          edge tmp(a,b);          mp.push_back(tmp);      }      sort(mp.begin(),mp.end(),cmp);      visit[1] = 1;      dfs(1);      for(int i=2;num[i];i++)      {          ans += (num[i] - 1) * 2 + 1;          //printf("num[%d] = %d\n",i,num[i]);      }      cout<<ans<<endl;      return 0;  } 第一题,dfs记录树的深度的数量存在num数组 ans += (num[i] - 1) * 2 + 1;
点赞 回复
分享
发布于 2018-09-06 21:28
🐮批,🐮批
点赞 回复
分享
发布于 2018-09-06 21:28
大佬算法咋练啊,也一直在刷题,还是不会做
点赞 回复
分享
发布于 2018-09-06 21:35
求java版的啊
点赞 回复
分享
发布于 2018-09-06 21:36
第一题不知道为啥错了。。。。很烦
点赞 回复
分享
发布于 2018-09-06 21:39
大佬大佬,我想加你qq
点赞 回复
分享
发布于 2018-09-06 21:39
最后5分钟想到了第一题的解题思路,然后直接交卷
点赞 回复
分享
发布于 2018-09-06 21:41
第一题 你这个对吗?
点赞 回复
分享
发布于 2018-09-06 21:41
大佬都在哪里刷题
点赞 回复
分享
发布于 2018-09-06 21:46
第一题,思路一样,但我求最远距离的dfs提示内存超了= =,只过了9%
点赞 回复
分享
发布于 2018-09-06 21:58
老哥求带啊  加个qq或者微信
点赞 回复
分享
发布于 2018-09-06 23:01

相关推荐

点赞 51 评论
分享
牛客网
牛客企业服务