代码拉取完成,页面将自动刷新
/*
* bytedance.com Inc.
* Copyright (c) 2012-2022 All Rights Reserved.
*/
import java.util.*;
/**
* @author 陈奂宇
* @version 1: Test.java, v 0.1 2022-08-31 20:11 陈奂宇 Exp $$
*/
public class Test {
public static void main(String[] args) {
// 获取输入
Scanner sc = new Scanner(System.in);
// 苹果数量
int apples = sc.nextInt();
// 人数
int people = sc.nextInt();
// 苹果高度
int[] appleNums = new int[apples];
for (int i = 0; i < apples; i++) {
appleNums[i] = sc.nextInt();
}
// 人高度
int[] peopleNums = new int[people];
for (int i = 0; i < people; i++) {
peopleNums[i] = sc.nextInt();
}
// 苹果排序
Arrays.sort(appleNums);
// 哈希表:key是苹果高度,value是数组下标
HashMap<Integer, Integer> map = new HashMap<>();
// 有序链表
ArrayList<Integer> list = new ArrayList<>();
// 链表
for (int i = 0; i < appleNums.length; i++) {
map.put(appleNums[i],i);
list.add(appleNums[i]);
}
// 开始计算
for (int temp : peopleNums) {
// 剪枝:链表已经小于0了
if (list.isEmpty()) {
break;
}
// 剪枝:如果大于最大值
if (temp >= list.get(list.size()-1)) {
list.remove(list.size()-1);
continue;
}
// 剪枝:如果小于最小值
else if (temp < list.get(0)) {
continue;
}
// 剪枝:如果有匹配的值
else if (map.containsKey(temp)) {
Integer index = map.get(temp);
map.remove(temp);
list.remove(index);
continue;
}
// 寻找最接近但不大于自己的值
int index = list.size()-1;
while (index >= 0 && list.get(index) > temp) {
if (list.get(index) == temp) {
list.remove(index);
break;
}
index--;
}
if (index >= 0) {
list.remove(index+1);
}
}
// 返回结果
System.out.println(list.size());
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。