← 返回 coinbase 的题目列表Prefix Scan and Top N Elements
类型:online_judge
Given a list of names and a prefix, write a program to match all names that start with the given prefix. In the second part, given a list of integers, find the top N largest elements and return them as a new list sorted in descending order.
Input
names: A list of strings, representing the names.
prefix: A string, representing the prefix.
nums: A list of integers.
N: An integer, representing the number of largest elements to find.
Output
A list results containing two elements:
matched_names: A list of names matching the prefix.
top_n: A list of the top N largest elements sorted in descending order.
Example
Input:
names = ["alice", "bob", "carol", "alex"]
prefix = "al"
nums = [5, 2, 9, 1, 10]
N = 2
Output:
results = [
["alice", "alex"],
[10, 9]
]
Constraints
The names list can contain up to 1000 elements.
The prefix length is up to 100.
The list of integers can contain up to 1000 elements.
N is at least 1 and at most the length of the integer list.
Example
Input
names = ["john", "jane", "jill", "jack"]
prefix = "ja"
nums = [4, 5, 6, 7]
N = 3