Skip to content

Commit 6f5b88a

Browse files
committed
Daily LC
1 parent ff087a6 commit 6f5b88a

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Problem Link: https://leetcode.com/problems/last-moment-before-all-ants-fall-out-of-a-plank/description/
3+
4+
Problem Statement: We have a wooden plank of the length n units. Some ants are walking on the plank, each ant moves with a speed of 1 unit per second. Some of the ants move to the left, the other move to the right.
5+
6+
When two ants moving in two different directions meet at some point, they change their directions and continue moving again. Assume changing directions does not take any additional time.
7+
8+
When an ant reaches one end of the plank at a time t, it falls out of the plank immediately.
9+
10+
Given an integer n and two integer arrays left and right, the positions of the ants moving to the left and the right, return the moment when the last ant(s) fall out of the plank.
11+
12+
Solution Approach:
13+
If two ants bump into each other and change directions, it's the same as if these ants continue as nothing happens.
14+
15+
So, we can think about that plank as a two-way street. So, find the maximum units that any ant needs to travel.
16+
*/
17+
18+
/* ------------CODE---------------- */
19+
class Solution {
20+
public int getLastMoment(int n, int[] left, int[] right) {
21+
22+
int max = 0;
23+
for(int curr : left) {
24+
max = Math.max(curr, max);
25+
}
26+
for(int curr : right) {
27+
max = Math.max(n-curr, max);
28+
}
29+
return max;
30+
31+
}
32+
}
33+
34+
/*
35+
Time Complexity: O(n+m)
36+
Space Complexity: O(1)
37+
*/

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy