1 Star 0 Fork 0

陈同学。/笔记

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Test.java 2.34 KB
一键复制 编辑 原始数据 按行查看 历史
陈奂宇 提交于 2022-09-06 02:05 +08:00 . 1
/*
* 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());
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/chychenchenchen/note.git
git@gitee.com:chychenchenchen/note.git
chychenchenchen
note
笔记
master

搜索帮助