Created
July 5, 2020 19:34
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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