来看看近期亚马逊新出的OA吧,很快就被我秒掉了~
Code Question
You are shopping on Amazon.com for some bags of rice. Each listing displays the number of grains of rice that the bag contains. You want to buy a perfect set of rice bags from the entire search results list, riceBags
. A perfect set of rice bags,perfect, is defined as :
The set contains at least two bags of rice.
When the rice bags in the set perfect are sorted in increasing order by grain count, it satisfies the condition perfect[i] != perfect[i+1]
for all i
from 1 to n-1
, where n
is the size of the set, and perfect[i]
is the number of rice grains in bag i
.
Find the largest possible set, perfect, and return an integer, the size of that set. If no such set is possible, then return -1. It is guaranteed that all elements in riceBags
are distinct.
Example:
Let the bags of rice available on Amazon have grain counts [3, 9, 4, 2, 16]. The following are the perfect sets:
- Set perfect = [3, 9]. The size of this set is 2.
- Set perfect = [4, 2]. The size of this set is 2.
- Set perfect = [4, 16]. The size of this set is 2.
- Set perfect = [4, 2, 16]. The size of this set is 3.
The size of the largest set is 3.
Function Description:
Complete the function maxSetSize in the editor below. maxSetSize has the following parameter:
int riceBags[n]: the list of bags of rice by rice grain count
Returns
int: the size of the largest set possible or -1 if there is none
Constraints:
1 ≤ n ≤ 2 * 10^5
2 ≤ riceBags[i] ≤ 10^6
我们还提供OA与VO的咨询与支持服务,如果有需要,请联系我们:
chen@csoahelp.com
We also provide consultation and support services for OA and VO. If needed, please feel free to contact us:
chen@csoahelp.com