Skip to content

Instantly share code, notes, and snippets.

@bhaveshmunot1
Created July 5, 2020 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhaveshmunot1/b134c22641eee76d8e8f585a26a17eb2 to your computer and use it in GitHub Desktop.
Save bhaveshmunot1/b134c22641eee76d8e8f585a26a17eb2 to your computer and use it in GitHub Desktop.
Leetcode #1503: Last Moment Before All Ants Fall Out of a Plank (https://www.InterviewRecipes.com/leetcode-1503)
class Solution {
public:
int getLastMoment(int n, vector<int>& left, vector<int>& right) {
// The one that is farthest from the left end, but desires to go in the left
// direction, will be the last one to go off of the plank from the left side.
int maxLeft = left.empty() ? 0 : *max_element(left.begin(), left.end());
// Similarly,
// The one that is farthest from the right end, but desires to go in the right
// direction, will be the last one to go off of the plank from the right side.
int minRight = right.empty() ? n : *min_element(right.begin(), right.end());
// The one among above two would be the last one to off of the plank among all.
return max(maxLeft, n - minRight);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment