Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d56e81d

Browse files
committed
feat: add solutions to lc problem: No.1503
No.1503.Last Moment Before All Ants Fall Out of a Plank
1 parent ea0ec4b commit d56e81d

File tree

6 files changed

+158
-27
lines changed

6 files changed

+158
-27
lines changed

‎solution/1500-1599/1503.Last Moment Before All Ants Fall Out of a Plank/README.md‎

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,27 +76,82 @@
7676
<li><code>left</code> 和 <code>right</code> 中的所有值都是唯一的,并且每个值 <strong>只能出现在二者之一</strong> 中。</li>
7777
</ul>
7878

79-
8079
## 解法
8180

8281
<!-- 这里可写通用的实现逻辑 -->
8382

83+
题目关键点在于两只蚂蚁相遇,然后分别调转方向的情况,实际上相当于两只蚂蚁继续往原来的方向移动。
84+
8485
<!-- tabs:start -->
8586

8687
### **Python3**
8788

8889
<!-- 这里可写当前语言的特殊实现逻辑 -->
8990

9091
```python
91-
92+
class Solution:
93+
def getLastMoment(self, n: int, left: List[int], right: List[int]) -> int:
94+
ans = 0
95+
for t in left:
96+
ans = max(ans, t)
97+
for t in right:
98+
ans = max(ans, n - t)
99+
return ans
92100
```
93101

94102
### **Java**
95103

96104
<!-- 这里可写当前语言的特殊实现逻辑 -->
97105

98106
```java
107+
class Solution {
108+
public int getLastMoment(int n, int[] left, int[] right) {
109+
int ans = 0;
110+
for (int t : left) {
111+
ans = Math.max(ans, t);
112+
}
113+
for (int t : right) {
114+
ans = Math.max(ans, n - t);
115+
}
116+
return ans;
117+
}
118+
}
119+
```
120+
121+
### **C++**
122+
123+
```cpp
124+
class Solution {
125+
public:
126+
int getLastMoment(int n, vector<int>& left, vector<int>& right) {
127+
int ans = 0;
128+
for (int t : left) ans = max(ans, t);
129+
for (int t : right) ans = max(ans, n - t);
130+
return ans;
131+
}
132+
};
133+
```
99134
135+
### **Go**
136+
137+
```go
138+
func getLastMoment(n int, left []int, right []int) int {
139+
ans := 0
140+
for _, t := range left {
141+
ans = max(ans, t)
142+
}
143+
for _, t := range right {
144+
ans = max(ans, n-t)
145+
}
146+
return ans
147+
}
148+
149+
func max(a, b int) int {
150+
if a > b {
151+
return a
152+
}
153+
return b
154+
}
100155
```
101156

102157
### **...**

‎solution/1500-1599/1503.Last Moment Before All Ants Fall Out of a Plank/README_EN.md‎

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,12 @@
66

77
<p>We have a wooden&nbsp;plank of the length <code>n</code> <strong>units</strong>. Some ants are walking on the&nbsp;plank, each ant moves with speed <strong>1 unit per second</strong>. Some of the ants move to the <strong>left</strong>, the other move to the <strong>right</strong>.</p>
88

9-
10-
119
<p>When two ants moving in two <strong>different</strong> directions meet at some point, they change their directions and continue moving again. Assume changing directions doesn&#39;t take any additional time.</p>
1210

13-
14-
1511
<p>When an ant reaches <strong>one end</strong> of the plank at a time <code>t</code>, it falls out of the plank imediately.</p>
1612

17-
18-
1913
<p>Given an integer <code>n</code> and two integer arrays <code>left</code> and <code>right</code>, the positions of the ants moving to the left and the right.&nbsp;Return <em>the&nbsp;moment</em> when the last ant(s) fall out of the plank.</p>
2014

21-
22-
2315
<p>&nbsp;</p>
2416

2517
<p><strong>Example 1:</strong></p>
@@ -46,8 +38,6 @@ Note that the last moment when an ant was on the plank is t = 4 second, after th
4638

4739
</pre>
4840

49-
50-
5141
<p><strong>Example 2:</strong></p>
5242

5343
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1500-1599/1503.Last%20Moment%20Before%20All%20Ants%20Fall%20Out%20of%20a%20Plank/images/ants2.jpg" style="width: 639px; height: 101px;" />
@@ -62,8 +52,6 @@ Note that the last moment when an ant was on the plank is t = 4 second, after th
6252

6353
</pre>
6454

65-
66-
6755
<p><strong>Example 3:</strong></p>
6856

6957
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1500-1599/1503.Last%20Moment%20Before%20All%20Ants%20Fall%20Out%20of%20a%20Plank/images/ants3.jpg" style="width: 639px; height: 100px;" />
@@ -78,12 +66,8 @@ Note that the last moment when an ant was on the plank is t = 4 second, after th
7866

7967
</pre>
8068

81-
82-
8369
<p><strong>Example 4:</strong></p>
8470

85-
86-
8771
<pre>
8872

8973
<strong>Input:</strong> n = 9, left = [5], right = [4]
@@ -94,12 +78,8 @@ Note that the last moment when an ant was on the plank is t = 4 second, after th
9478

9579
</pre>
9680

97-
98-
9981
<p><strong>Example 5:</strong></p>
10082

101-
102-
10383
<pre>
10484

10585
<strong>Input:</strong> n = 6, left = [6], right = [0]
@@ -108,14 +88,10 @@ Note that the last moment when an ant was on the plank is t = 4 second, after th
10888

10989
</pre>
11090

111-
112-
11391
<p>&nbsp;</p>
11492

11593
<p><strong>Constraints:</strong></p>
11694

117-
118-
11995
<ul>
12096
<li><code>1 &lt;= n &lt;= 10^4</code></li>
12197
<li><code>0 &lt;= left.length &lt;= n + 1</code></li>
@@ -133,13 +109,67 @@ Note that the last moment when an ant was on the plank is t = 4 second, after th
133109
### **Python3**
134110

135111
```python
136-
112+
class Solution:
113+
def getLastMoment(self, n: int, left: List[int], right: List[int]) -> int:
114+
ans = 0
115+
for t in left:
116+
ans = max(ans, t)
117+
for t in right:
118+
ans = max(ans, n - t)
119+
return ans
137120
```
138121

139122
### **Java**
140123

141124
```java
125+
class Solution {
126+
public int getLastMoment(int n, int[] left, int[] right) {
127+
int ans = 0;
128+
for (int t : left) {
129+
ans = Math.max(ans, t);
130+
}
131+
for (int t : right) {
132+
ans = Math.max(ans, n - t);
133+
}
134+
return ans;
135+
}
136+
}
137+
```
138+
139+
### **C++**
140+
141+
```cpp
142+
class Solution {
143+
public:
144+
int getLastMoment(int n, vector<int>& left, vector<int>& right) {
145+
int ans = 0;
146+
for (int t : left) ans = max(ans, t);
147+
for (int t : right) ans = max(ans, n - t);
148+
return ans;
149+
}
150+
};
151+
```
142152
153+
### **Go**
154+
155+
```go
156+
func getLastMoment(n int, left []int, right []int) int {
157+
ans := 0
158+
for _, t := range left {
159+
ans = max(ans, t)
160+
}
161+
for _, t := range right {
162+
ans = max(ans, n-t)
163+
}
164+
return ans
165+
}
166+
167+
func max(a, b int) int {
168+
if a > b {
169+
return a
170+
}
171+
return b
172+
}
143173
```
144174

145175
### **...**
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public:
3+
int getLastMoment(int n, vector<int>& left, vector<int>& right) {
4+
int ans = 0;
5+
for (int t : left) ans = max(ans, t);
6+
for (int t : right) ans = max(ans, n - t);
7+
return ans;
8+
}
9+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public int getLastMoment(int n, int[] left, int[] right) {
3+
int ans = 0;
4+
for (int t : left) {
5+
ans = Math.max(ans, t);
6+
}
7+
for (int t : right) {
8+
ans = Math.max(ans, n - t);
9+
}
10+
return ans;
11+
}
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def getLastMoment(self, n: int, left: List[int], right: List[int]) -> int:
3+
ans = 0
4+
for t in left:
5+
ans = max(ans, t)
6+
for t in right:
7+
ans = max(ans, n - t)
8+
return ans
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func getLastMoment(n int, left []int, right []int) int {
2+
ans := 0
3+
for _, t := range left {
4+
ans = max(ans, t)
5+
}
6+
for _, t := range right {
7+
ans = max(ans, n-t)
8+
}
9+
return ans
10+
}
11+
12+
func max(a, b int) int {
13+
if a > b {
14+
return a
15+
}
16+
return b
17+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /