marut
BAN USERWRONG SOLUTION (misunderstood the question)
You need to find maximal value among all minimums, and minimal value among all maximums. If maximal minimum is less than minimal maximum, than you can use a range of size one, it may be any number within that range.
void Calculate(const std::vector<std::vector<int> >& src, int& l, int& r){
int mn = src[0].front();
int mx = src[0].back();
for(int i = 1; i < src.size(); ++i){
mn = std::max(mn, src[i].front());
mx = std::min(mx, src[i].back());
}
if(mn <= mx) mx = mn;
l = mn;
r = mx;
}
UPD: Sorry, there was a copy-paste mistake. Fixed now.
UPD2: For {0,10,255} {2 , 11 , 257} {4,12,258} the answer will be any two equal numbers from the range [4, 255], because we need minimal range, in my implementation it's [4, 4].
I would divide the answer on two parts.
At first, we need to count how many 3-digit numbers can be made using 1, 2, 3, 4 or 5. It is obvious that on every digit we can use every number, so 3 digits, 5 numbers: 5*5*5 = 125 different 3-digit numbers. But those numbers can have even number repetitions, like 223 or 444.
So secondly, we need to count number of such 3-digit numbers with repetitions of 2 and 4. We can see, that for this case 22X it is possible to have 5 numbers, as well as for this case 2X2, and for this case X22. That way we have 3*5 combinations for '2' and 3*5 for '4'.
That way we have the answer: 5*5*5 - 3*5 - 3*5 = 125 - 15 - 15 = 95.
Since Next() and HasNext() methods are exactly the same for CPeekIterator as for CIterator, we just duplicate them without any changes.
The only difference in CPeekIterator is that Peek() needs to return next value without advancing the iterator. Solution is to store CIterator inside CPeekIterator and in case of Peek() create a temporary iterator. It will return next value, but advancing is not important, since it's temporary value.
- marut December 08, 2015