找出给定比赛中最后一辆车到达赛道尽头的时间
给定一条长度为N个单位的赛车跑道。每辆车以每秒 1 个单位的速度移动。给定两个数组left[]和right[]分别表示汽车向左和向右移动的位置。当沿两个相反方向行驶的两辆汽车在轨道上的某个点相遇时,它们会改变方向并继续以相反的方向继续行驶。返回最后一辆车到达轨道两端的时间。
注意:最初没有两辆车在同一个地方。
例子:
Input: N = 4, left = {4, 3}, right = {0, 1}
Output: 4
Explanation:
The car at index 0 is named A and going to the right.
The car at index 1 is named B and going to the right.
The car at index 3 is named C and going to the left.
The car at index 4 is named D and going to the left.
The last moment when a car was on the race track is t = 4 seconds.
See the image given –
Input: N = 7, left = {0, 1, 2, 3, 4, 5, 6, 7}, right = { }
Output: 7
Explanation: All cars are going to the left, the car at index 7 needs 7 seconds to reach the end of track.
Input: N = 10, left = {3, 5}, right = {1}
Output: 9
Explanation:
Say at t = 0, carA = 1 (->), carB = 3 (<-), carC = 5 (<-)
At t = 1: carA and carB collides at 2 and change directions.
So, carA = 2 (<-), carB = 2 (->), carC = 4 (<-)
At t = 2, carB and carC collides at 3 and change direction.
So, carA = 1 (<-), carB = 3 (<-), carC = 3 (->)
At t = 3, carA reaches 0.
So, carA = 0 (<-), carB = 2 (<-), carC = 4 (->)
At t = 4, only carB and carC are running on track in opposite directions.
So, carB = 1 (<-), carC = 5 (->)
At t = 5, carB reaches at 0.
So, carB = 0 (<-), carC = 5 (->)
carC alone running towards right end (index 9).
So, at t = 9 carC reaches at 9.
方法:可以根据以下观察解决问题。
由于发生碰撞的汽车立即改变方向并开始向相反方向移动,因此可以认为碰撞的汽车只是交换了它们的位置并随后向同一方向移动。因此,所需时间是任何汽车继续沿同一方向移动时从起始位置到达终点所需的最长时间。
下面是上述方法的实现。
C++
// Program for above approach.
#include
using namespace std;
// Function to get the maximum time
int getLastCarMoment(int N,
vector& left,
vector& right)
{
int ans = 0;
for (auto it : left)
ans = max(ans, it);
for (auto it : right)
ans = max(ans, N - it);
return ans;
}
// Driver code
int main()
{
int N = 10;
vector left{ 3, 5 }, right{ 1 };
// Function call
int res = getLastCarMoment(N, left, right);
cout << res;
return 0;
}
Java
// Java Program for above approach.
class GFG {
// Function to get the maximum time
static int getLastCarMoment(int N,
int[] left,
int[] right) {
int ans = 0;
for (int it : left)
ans = Math.max(ans, it);
for (int it : right)
ans = Math.max(ans, N - it);
return ans;
}
// Driver code
public static void main(String args[]) {
int N = 10;
int[] left = { 3, 5 };
int[] right = { 1 };
// Function call
int res = getLastCarMoment(N, left, right);
System.out.println(res);
}
}
// This code is contributed by Saurabh jaiswal
Python3
# Program for above approach.
# Function to get the maximum time
def getLastCarMoment(N,
left,
right):
ans = 0
for it in left:
ans = max(ans, it)
for it in right:
ans = max(ans, N - it)
return ans
# Driver code
if __name__ == "__main__":
N = 10
left = [3, 5]
right = [1]
# Function call
res = getLastCarMoment(N, left, right)
print(res)
# This code is contributed by ukasp.
C#
// Program for above approach.
using System;
class GFG
{
// Function to get the maximum time
static int getLastCarMoment(int N,
int []left,
int []right)
{
int ans = 0;
foreach (int it in left)
ans = Math.Max(ans, it);
foreach (int it in right)
ans = Math.Max(ans, N - it);
return ans;
}
// Driver code
public static void Main()
{
int N = 10;
int []left = { 3, 5 };
int []right = { 1 };
// Function call
int res = getLastCarMoment(N, left, right);
Console.Write(res);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
9
时间复杂度: O(M),其中 M 是两个数组的最大大小。
辅助空间: O(1)