• + 0 comments

    I think the best way to solve it is to use a BST. Sadly, there's no suitable built-in BST in C# which is my favorite coding language. So, I solved it in C++.

    int minimumLoss(std::vector<long> price) {
    	std::set<long> before;
    	long minLoss = LONG_MAX;
    	for (long p : price)
    	{
    		long nearestGreater = *before.upper_bound(p);
    		long loss = nearestGreater - p;
    		if (loss > 0 && loss < minLoss)
    			minLoss = loss;
    
    		before.insert(p);
    	}
    
    	return minLoss;
    }