美团8.13 后端方向笔试题 全AC
这题也太简单了。看脉脉上也说美团没多少HC。凉
魔法外卖
简单的模拟
public class Main
{
public static void main(String args[])
{
Scanner cin = new Scanner(System.in);
String[] line = cin.nextLine().split(" ");
int n = Integer.parseInt(line[0]), t = Integer.parseInt(line[1]);
int[] delivery = Arrays.stream(cin.nextLine().split(" ")).mapToInt(Integer::valueOf).toArray();
int count = 0, time = 0;
Arrays.sort(delivery);
for (int i = 0; i < n; i++) {
if (time + t <= delivery[i]){
time += t;
} else {
count++;
}
}
System.out.printf("%d", count);
}
} 打扫房间
简单模拟
public class Main
{
public static void main(String args[])
{
Scanner cin = new Scanner(System.in);
String[] line = cin.nextLine().split(" ");
int n = Integer.parseInt(line[0]), m = Integer.parseInt(line[1]), k = Integer.parseInt(line[2]);
String orders = cin.nextLine();
int[][] room = new int[n][m];
room[0][0] = 1;
int count = 1, x = 0, y = 0;
for (int i = 0; i < orders.length(); i++) {
char ch = orders.charAt(i);
if (ch == 'W') x--;
else if (ch == 'A') y--;
else if (ch == 'S') x++;
else y++;
if (room[x][y] == 0) {
room[x][y] = 1;
count++;
}
if (count == n * m) {
System.out.println("Yes");
System.out.println(i + 1);
return;
}
}
System.out.println("No");
System.out.println(n * m - count);
}
} 扑克牌
用双向队列的 简单模拟
牌堆可以使用双向队列模拟。每轮从牌堆顶抽一张牌,再放入牌堆底。这个动作可以抽象为从队列头取出一个元素,再放入队尾。
我们使用origin数组模拟初始牌堆中每张牌的顺序和每张牌的牌点的关系。
public class Main
{
public static void main(String args[])
{
Scanner cin = new Scanner(System.in);
int n = Integer.parseInt(cin.nextLine());
int[] card = Arrays.stream(cin.nextLine().split(" ")).mapToInt(Integer::valueOf).toArray();
int[] origin = new int[n];
Deque<Integer> d = new ArrayDeque<>();
for (int i = 0; i < n; i++) d.offerLast(n - i - 1);
for (int i = 0; i < n; i++) {
d.offerFirst(d.pollLast());
d.offerFirst(d.pollLast());
origin[d.pollLast()] = card[i];
}
for (int i = 0; i < n; i++) {
System.out.printf("%d ", origin[i]);
}
}
} 三元组
LeetCode经典3数之和改编版。唯一的坑是最后的三元组很多,用int会溢出。盲猜测试数据是全0。
public class Main
{
public static void main(String args[])
{
Scanner cin = new Scanner(System.in);
int n = Integer.parseInt(cin.nextLine());
int[] nums = Arrays.stream(cin.nextLine().split(" ")).mapToInt(Integer::valueOf).toArray();
long count = 0;
Map<Integer, Integer> mp = new HashMap<>();
mp.put(nums[n - 1], 1);
for (int i = n - 2; i >= 1; i--) {
for (int j = i - 1; j >= 0; j--) {
count += mp.getOrDefault(3 * nums[i] - nums[j], 0);
}
mp.put(nums[i], mp.getOrDefault(nums[i], 0) + 1);
}
System.out.println(count);
}
} 树
LeetCode经典 三角形最小路径。 不用care是不是叶子。因为叶子的累计的值一定比非叶子大。
public class Main
{
public static void main(String args[])
{
Scanner cin = new Scanner(System.in);
int n = Integer.parseInt(cin.nextLine());
String[] _nums = cin.nextLine().split(" ");
int[] nums = new int[n + 1];
for (int i = 1; i <= n; i++) nums[i] = Integer.parseInt(_nums[i - 1]);
int ans = 0;
Queue<Integer> q = new ArrayDeque<>();
q.offer(1);
while (!q.isEmpty()) {
int t = q.poll();
ans = Math.max(ans, nums[t]);
if (2 * t <= n){
nums[2 * t] += nums[t];
q.offer(2 * t);
}
if (2 * t + 1 <= n) {
nums[2 * t + 1] += nums[t];
q.offer(2 * t + 1);
}
}
System.out.println(ans);
}
} #美团##美团笔试##美团笔试题#
查看2道真题和解析