@Alpher I agree that the bug is when there are more than one maxv.
But always choosing the one with the rightmost end among the same maxv would not work, because that is basically choosing the last one instead of the first one. I suspect it would still fail certain testcases.
E.g. Consider the same testcase but left-right flipped (as if looking from right to left).
For example, [1,2],[2,5],[3,4],[4,6], the solution is removing [2,5], because it takes so much space, and it's overlapping with both [3,4] and [4,6]. After removing [2,5], we need to update temend with the smaller end, which is 4. If we don't update, the temend(5) will still affect the rest of comparison. In other word, update temend is a representation of removing an interval.