DSA问题求助:补全寻找指定年龄最后一位人员的Java代码
求助:DSA问题解法思路
我是一名DSA问题求解新手,目前卡在这个问题的解法思路上,恳请大家提供帮助或相关参考。
问题描述
N个人按年龄降序排成队列,找出年龄为给定值K的最后一个人的名字。若不存在该年龄的人员,返回"Not found"。
输入格式
- 第一行包含两个整数N、K——分别为人数和指定年龄。
- 第二行包含N个整数——人员的年龄列表。
- 第三行包含N个字符串——人员的姓名列表。
输出格式
输出年龄为K的最后一个人的姓名,若不存在则输出"Not found"。
样例输入
6 30 80 30 30 7 3 3 Sam Will Roy Lane Pam Jim
样例输出
Roy
解释
年龄为30的人员依次是Will、Roy,其中最后一位是"Roy"。
约束条件
1 <= N <= 10^5 1 <= K, Age[i] <= 10^5 1 <= |Person's name| <= 10
我的代码如下
import java.util.*; class FindLastPersonOfAgeK{ public static void main(String args[]){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); List<Integer> ages = new ArrayList<Integer>(); List<String> people = new ArrayList<String>(); for (int i = 0; i < n; i++) { ages.add(sc.nextInt()); } for (int i = 0; i < n; i++) { people.add(sc.next()); } String ans = findLastPersonOfAgeK(ages, people, k); System.out.println(ans); sc.close(); } static String findLastPersonOfAgeK(List<Integer> ages, List<String> people, int k){ } }
内容的提问来源于stack exchange,提问作者shrimaan0205
相关产品推荐
相关产品推荐

