1503. Last Moment Before All Ants Fall Out of a Plank
У нас есть деревянная доска длиной n единиц. Некоторые муравьи ходят по доске, каждый муравей движется со скоростью 1 единица в секунду. Некоторые муравьи движутся влево, другие движутся вправо.
Когда два муравья, движущиеся в разных направлениях, встречаются в какой-то точке, они меняют свои направления и продолжают двигаться дальше. Предполагается, что изменение направлений не занимает дополнительного времени.
Когда муравей достигает одного из концов доски в момент времени t, он сразу же падает с доски.
given số nguyên n и два целых mảngа left и right, обозначающие позиции муравьев, движущихся влево и вправо соответственно. return момент, когда последний(е) муравей(и) падает(ют) с доски.
Ví dụ:
Input: n = 4, left = [4,3], right = [0,1]
Output: 4
Explanation: In the image above:
-The ant at index 0 is named A and going to the right.
-The ant at index 1 is named B and going to the right.
-The ant at index 3 is named C and going to the left.
-The ant at index 4 is named D and going to the left.
The last moment when an ant was on the plank is t = 4 seconds. After that, it falls immediately out of the plank. (i.e., We can say that at t = 4.0000000001, there are no ants on the plank).
C# lời giải
đã khớp/gốcpublic class Solution {
public int GetLastMoment(int n, int[] left, int[] right) {
int ans = 0;
foreach (int num in left) {
ans = Math.Max(ans, num);
}
foreach (int num in right) {
ans = Math.Max(ans, n - num);
}
return ans;
}
}
C++ lời giải
bản nháp tự động, xem lại trước khi gửi#include <bits/stdc++.h>
using namespace std;
// Auto-generated C++ draft from the C# solution. Review containers, LINQ and helper types before submit.
class Solution {
public:
public int GetLastMoment(int n, vector<int>& left, vector<int>& right) {
int ans = 0;
foreach (int num in left) {
ans = max(ans, num);
}
foreach (int num in right) {
ans = max(ans, n - num);
}
return ans;
}
}
Java lời giải
bản nháp tự động, xem lại trước khi gửiimport java.util.*;
import java.math.*;
// Auto-generated Java draft from the C# solution. Review API differences before LeetCode submit.
public class Solution {
public int GetLastMoment(int n, int[] left, int[] right) {
int ans = 0;
foreach (int num in left) {
ans = Math.max(ans, num);
}
foreach (int num in right) {
ans = Math.max(ans, n - num);
}
return ans;
}
}
JavaScript lời giải
đã khớp/gốcvar getLastMoment = function(n, left, right) {
let ans = 0
for (let num of left) {
ans = Math.max(ans, num)
}
for (let num of right) {
ans = Math.max(ans, n - num)
}
return ans
}
Python lời giải
đã khớp/gốcclass Solution:
def getLastMoment(self, n: int, left: List[int], right: List[int]) -> int:
ans = 0
for num in left:
ans = max(ans, num)
for num in right:
ans = max(ans, n - num)
return ans
Go lời giải
đã khớp/gốcfunc getLastMoment(n int, left []int, right []int) int {
ans := 0
for _, num := range left {
if num > ans {
ans = num
}
}
for _, num := range right {
if n - num > ans {
ans = n - num
}
}
return ans
}
Algorithm
Инициализируйте переменную ans значением 0.
Итерация по mảngу left и обновление ans значением num, если оно больше текущего значения ans.
Итерация по mảngу right и обновление ans значением n - num, если оно больше текущего значения ans. return значение ans.
😎
Vacancies for this task
việc làm đang hoạt động with overlapping task tags are đã hiển thị.