When I tried to upsolve this problem 706B - Interesting drink I faced an undefined time limit!!
In my first approach, I solved it using a multiset and upperbound 294508423 I got time limit on test 11
then, tried to solve with vector and sorting, which got accepted 294508512
Can anyone help me understand what the issue was?
auto price = prices.upper_bound(x); int canBuy = distance(prices.begin(), price);
The calculation of
canBuy
takes $$$O(n)$$$ time in the worst case instead of $$$O(\log n)$$$ as you suppose, wheren = prices.size()
, since the time complexity ofdistance(prices.begin(), price)
is $$$O(n)$$$ in the worst case, because these iterators are not random access.Time complexity of
std::distance( InputIt first, InputIt last )
is constant only ifInputIt
is the random access iterator, else it is linear.thx bro <3