Skip to content

Commit bf2c5b1

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 139ad98 commit bf2c5b1

File tree

7 files changed

+107
-52
lines changed

7 files changed

+107
-52
lines changed

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

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,13 @@
7070

7171
<!-- 这里可写通用的实现逻辑 -->
7272

73-
题目关键点在于两只蚂蚁相遇,然后分别调转方向的情况,实际上相当于两只蚂蚁继续往原来的方向移动。
73+
**方法一:脑筋急转弯**
74+
75+
题目关键点在于两只蚂蚁相遇,然后分别调转方向的情况,实际上相当于两只蚂蚁继续往原来的方向移动。因此,我们只需要求出所有蚂蚁中最远的那只蚂蚁的移动距离即可。
76+
77+
注意 $left$ 和 $right$ 数组的长度可能为 $0$。
78+
79+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为木板的长度。
7480

7581
<!-- tabs:start -->
7682

@@ -82,10 +88,10 @@
8288
class Solution:
8389
def getLastMoment(self, n: int, left: List[int], right: List[int]) -> int:
8490
ans = 0
85-
for t in left:
86-
ans = max(ans, t)
87-
for t in right:
88-
ans = max(ans, n - t)
91+
for x in left:
92+
ans = max(ans, x)
93+
for x in right:
94+
ans = max(ans, n - x)
8995
return ans
9096
```
9197

@@ -97,11 +103,11 @@ class Solution:
97103
class Solution {
98104
public int getLastMoment(int n, int[] left, int[] right) {
99105
int ans = 0;
100-
for (int t : left) {
101-
ans = Math.max(ans, t);
106+
for (int x : left) {
107+
ans = Math.max(ans, x);
102108
}
103-
for (int t : right) {
104-
ans = Math.max(ans, n - t);
109+
for (int x : right) {
110+
ans = Math.max(ans, n - x);
105111
}
106112
return ans;
107113
}
@@ -115,8 +121,12 @@ class Solution {
115121
public:
116122
int getLastMoment(int n, vector<int>& left, vector<int>& right) {
117123
int ans = 0;
118-
for (int t : left) ans = max(ans, t);
119-
for (int t : right) ans = max(ans, n - t);
124+
for (int& x : left) {
125+
ans = max(ans, x);
126+
}
127+
for (int& x : right) {
128+
ans = max(ans, n - x);
129+
}
120130
return ans;
121131
}
122132
};
@@ -125,15 +135,14 @@ public:
125135
### **Go**
126136
127137
```go
128-
func getLastMoment(n int, left []int, right []int) int {
129-
ans := 0
130-
for _, t := range left {
131-
ans = max(ans, t)
138+
func getLastMoment(n int, left []int, right []int) (ans int) {
139+
for _, x := range left {
140+
ans = max(ans, x)
132141
}
133-
for _, t := range right {
134-
ans = max(ans, n-t)
142+
for _, x := range right {
143+
ans = max(ans, n-x)
135144
}
136-
return ans
145+
return
137146
}
138147
139148
func max(a, b int) int {
@@ -144,6 +153,21 @@ func max(a, b int) int {
144153
}
145154
```
146155

156+
### **TypeScript**
157+
158+
```ts
159+
function getLastMoment(n: number, left: number[], right: number[]): number {
160+
let ans = 0;
161+
for (const x of left) {
162+
ans = Math.max(ans, x);
163+
}
164+
for (const x of right) {
165+
ans = Math.max(ans, n - x);
166+
}
167+
return ans;
168+
}
169+
```
170+
147171
### **...**
148172

149173
```

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

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ The last moment when an ant was on the plank is t = 4 seconds. After that, it fa
6565
class Solution:
6666
def getLastMoment(self, n: int, left: List[int], right: List[int]) -> int:
6767
ans = 0
68-
for t in left:
69-
ans = max(ans, t)
70-
for t in right:
71-
ans = max(ans, n - t)
68+
for x in left:
69+
ans = max(ans, x)
70+
for x in right:
71+
ans = max(ans, n - x)
7272
return ans
7373
```
7474

@@ -78,11 +78,11 @@ class Solution:
7878
class Solution {
7979
public int getLastMoment(int n, int[] left, int[] right) {
8080
int ans = 0;
81-
for (int t : left) {
82-
ans = Math.max(ans, t);
81+
for (int x : left) {
82+
ans = Math.max(ans, x);
8383
}
84-
for (int t : right) {
85-
ans = Math.max(ans, n - t);
84+
for (int x : right) {
85+
ans = Math.max(ans, n - x);
8686
}
8787
return ans;
8888
}
@@ -96,8 +96,12 @@ class Solution {
9696
public:
9797
int getLastMoment(int n, vector<int>& left, vector<int>& right) {
9898
int ans = 0;
99-
for (int t : left) ans = max(ans, t);
100-
for (int t : right) ans = max(ans, n - t);
99+
for (int& x : left) {
100+
ans = max(ans, x);
101+
}
102+
for (int& x : right) {
103+
ans = max(ans, n - x);
104+
}
101105
return ans;
102106
}
103107
};
@@ -106,15 +110,14 @@ public:
106110
### **Go**
107111
108112
```go
109-
func getLastMoment(n int, left []int, right []int) int {
110-
ans := 0
111-
for _, t := range left {
112-
ans = max(ans, t)
113+
func getLastMoment(n int, left []int, right []int) (ans int) {
114+
for _, x := range left {
115+
ans = max(ans, x)
113116
}
114-
for _, t := range right {
115-
ans = max(ans, n-t)
117+
for _, x := range right {
118+
ans = max(ans, n-x)
116119
}
117-
return ans
120+
return
118121
}
119122
120123
func max(a, b int) int {
@@ -125,6 +128,21 @@ func max(a, b int) int {
125128
}
126129
```
127130

131+
### **TypeScript**
132+
133+
```ts
134+
function getLastMoment(n: number, left: number[], right: number[]): number {
135+
let ans = 0;
136+
for (const x of left) {
137+
ans = Math.max(ans, x);
138+
}
139+
for (const x of right) {
140+
ans = Math.max(ans, n - x);
141+
}
142+
return ans;
143+
}
144+
```
145+
128146
### **...**
129147

130148
```

solution/1500-1599/1503.Last Moment Before All Ants Fall Out of a Plank/Solution.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ class Solution {
22
public:
33
int getLastMoment(int n, vector<int>& left, vector<int>& right) {
44
int ans = 0;
5-
for (int t : left) ans = max(ans, t);
6-
for (int t : right) ans = max(ans, n - t);
5+
for (int& x : left) {
6+
ans = max(ans, x);
7+
}
8+
for (int& x : right) {
9+
ans = max(ans, n - x);
10+
}
711
return ans;
812
}
913
};

solution/1500-1599/1503.Last Moment Before All Ants Fall Out of a Plank/Solution.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
func getLastMoment(n int, left []int, right []int) int {
2-
ans := 0
3-
for _, t := range left {
4-
ans = max(ans, t)
1+
func getLastMoment(n int, left []int, right []int) (ans int) {
2+
for _, x := range left {
3+
ans = max(ans, x)
54
}
6-
for _, t := range right {
7-
ans = max(ans, n-t)
5+
for _, x := range right {
6+
ans = max(ans, n-x)
87
}
9-
return ans
8+
return
109
}
1110

1211
func max(a, b int) int {

solution/1500-1599/1503.Last Moment Before All Ants Fall Out of a Plank/Solution.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Solution {
22
public int getLastMoment(int n, int[] left, int[] right) {
33
int ans = 0;
4-
for (int t : left) {
5-
ans = Math.max(ans, t);
4+
for (int x : left) {
5+
ans = Math.max(ans, x);
66
}
7-
for (int t : right) {
8-
ans = Math.max(ans, n - t);
7+
for (int x : right) {
8+
ans = Math.max(ans, n - x);
99
}
1010
return ans;
1111
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
class Solution:
22
def getLastMoment(self, n: int, left: List[int], right: List[int]) -> int:
33
ans = 0
4-
for t in left:
5-
ans = max(ans, t)
6-
for t in right:
7-
ans = max(ans, n - t)
4+
for x in left:
5+
ans = max(ans, x)
6+
for x in right:
7+
ans = max(ans, n - x)
88
return ans
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function getLastMoment(n: number, left: number[], right: number[]): number {
2+
let ans = 0;
3+
for (const x of left) {
4+
ans = Math.max(ans, x);
5+
}
6+
for (const x of right) {
7+
ans = Math.max(ans, n - x);
8+
}
9+
return ans;
10+
}

0 commit comments

Comments
 (0)