Skip to content

1503. Last Moment Before All Ants Fall Out of a Plank

  • Time: $O(n)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
 public:
  int getLastMoment(int n, vector<int>& left, vector<int>& right) {
    const int maxLeft =
        left.empty() ? 0 : *max_element(left.begin(), left.end());
    const int minRight =
        right.empty() ? n : *min_element(right.begin(), right.end());
    return max(maxLeft, n - minRight);
  }
};