Skip to content

Commit 257c531

Browse files
committed
Add solution #1503
1 parent edf768d commit 257c531

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,332 LeetCode solutions in JavaScript
1+
# 1,333 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1148,6 +1148,7 @@
11481148
1498|[Number of Subsequences That Satisfy the Given Sum Condition](./solutions/1498-number-of-subsequences-that-satisfy-the-given-sum-condition.js)|Medium|
11491149
1499|[Max Value of Equation](./solutions/1499-max-value-of-equation.js)|Hard|
11501150
1502|[Can Make Arithmetic Progression From Sequence](./solutions/1502-can-make-arithmetic-progression-from-sequence.js)|Easy|
1151+
1503|[Last Moment Before All Ants Fall Out of a Plank](./solutions/1503-last-moment-before-all-ants-fall-out-of-a-plank.js)|Medium|
11511152
1507|[Reformat Date](./solutions/1507-reformat-date.js)|Easy|
11521153
1512|[Number of Good Pairs](./solutions/1512-number-of-good-pairs.js)|Easy|
11531154
1519|[Number of Nodes in the Sub-Tree With the Same Label](./solutions/1519-number-of-nodes-in-the-sub-tree-with-the-same-label.js)|Medium|
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* 1503. Last Moment Before All Ants Fall Out of a Plank
3+
* https://leetcode.com/problems/last-moment-before-all-ants-fall-out-of-a-plank/
4+
* Difficulty: Medium
5+
*
6+
* We have a wooden plank of the length n units. Some ants are walking on the plank, each ant moves
7+
* with a speed of 1 unit per second. Some of the ants move to the left, the other move to the
8+
* right.
9+
*
10+
* When two ants moving in two different directions meet at some point, they change their directions
11+
* and continue moving again. Assume changing directions does not take any additional time.
12+
*
13+
* When an ant reaches one end of the plank at a time t, it falls out of the plank immediately.
14+
*
15+
* Given an integer n and two integer arrays left and right, the positions of the ants moving to the
16+
* left and the right, return the moment when the last ant(s) fall out of the plank.
17+
*/
18+
19+
/**
20+
* @param {number} n
21+
* @param {number[]} left
22+
* @param {number[]} right
23+
* @return {number}
24+
*/
25+
var getLastMoment = function(n, left, right) {
26+
let result = 0;
27+
28+
for (const pos of left) {
29+
result = Math.max(result, pos);
30+
}
31+
for (const pos of right) {
32+
result = Math.max(result, n - pos);
33+
}
34+
35+
return result;
36+
};

0 commit comments

Comments
 (0)