Connection Status:
Competition Arena > BuyingCheap
SRM 270 · 2005-11-03 · by misof · Sorting
Class Name: BuyingCheap
Return Type: int
Method Name: thirdBestPrice
Arg Types: (vector<int>)
Problem Statement

Problem Statement

Steve would like to buy a new car. He isn't wealthy, so he would prefer a reasonably cheap car. The only problem is that the quality of the cheapest cars is... let's say questionable.

Thus Steve decided to make a list of car prices and to buy a car with the third lowest price.

You will be given a int[] prices. The same price may occur multiple times in prices, but it should count only once in the ordering of available prices. See Example 1 for further clarification.

Write a function that returns the third lowest price in this list. If there are less than three different car prices in prices, your method should return -1.

Constraints

  • prices contains between 1 and 50 elements, inclusive.
  • Each element in prices will be between 1 and 1000, inclusive.
Examples
0)
{10, 40, 50, 20, 70,
 80, 30, 90, 60}
Returns: 30
1)
{10, 10, 10, 10, 20,
 20, 30, 30, 40, 40}
Returns: 30

The lowest price is 10, the second lowest is 20 and the third lowest is 30.

2)
{10}
Returns: -1
3)
{80, 90, 80, 90, 80}
Returns: -1
4)
{1000}
Returns: -1

Submissions are judged against all 65 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class BuyingCheap with a public method int thirdBestPrice(vector<int> prices) · 65 test cases · 2 s / 256 MB per case

Submitting as anonymous