在给定的初始位置和速度的帮助下,最大化每秒可以停止的飞机数量
给定两个由N个整数组成的数组A[]和B[] ,其中A[i]表示第i个飞机的初始位置, B[i]是飞机着陆的速度,任务是打印数字可以通过每秒射击一架飞机来阻止飞机着陆的飞机。
例子:
Input: A[] = {1, 3, 5, 4, 8}, B[] = {1, 2, 2, 1, 2}
Output: 4
Explanation:
- At second 1, shoot the plane at index 0, the positions of the planes are now A[] = {_, 1, 3, 3, 6}.
- At second 2, shoot the plane at index 1, the positions of the planes are now A[] = {_, _, 1, 2, 4}.
- At second 3, shoot the plane at index 2, the positions of the planes are now A[] = {_, _, _, 1, 2}.
- At second 4, shoot the plane at index 4 or 5, the positions of the planes are now A[] = {_, _, _, _, _}.
Therefore, a total of 4 planes can be stopped from landing.
Input: A[] = {2, 8, 2, 3, 1}, B[] = {1, 4, 2, 2, 2}
Output: 2
方法:可以根据以下观察解决给定的问题:
- 可以观察到,可以停止的飞机数量是每架飞机降落所需的不同时间的集合,因为同时只能摧毁一架飞机。
- 飞机降落所需的时间是A[i] / B[i]可以使用以下公式计算:时间 = 距离 / 速度
请按照以下步骤解决问题:
- 初始化一个哈希集,比如存储飞机着陆所需时间的S。
- 使用变量i遍历范围[0, N]并执行以下任务:
- 初始化一个变量,如果A[i]%B[i]的值大于0 ,则将t设为1 。否则,将变量t更新为0 。
- 将A[i]/B[i]的值添加到变量中,例如t 。
- 将t的值添加到 HashSet S中。
- 执行上述步骤后,返回 HashSet 的大小作为结果答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find maximum number
// of planes that can be stopped
// from landing
int maxPlanes(int A[], int B[], int N)
{
// Stores the times needed for
// landing for each plane
set St;
// Iterate over the arrays
for (int i = 0; i < N; i++) {
// Stores the time needed for
// landing of current plane
int t = (A[i] % B[i] > 0) ? 1 : 0;
// Update the value of t
t += (A[i] / B[i]) + t;
// Append the t in set St
St.insert(t);
}
// Return the answer
return St.size();
}
// Driver Code
int main() {
int A[] = { 1, 3, 5, 4, 8 };
int B[] = { 1, 2, 2, 1, 2 };
int N = sizeof(A)/sizeof(A[0]);
cout << maxPlanes(A, B, N);
return 0;
}
// This code is contributed by Dharanendra L V.
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function to find maximum number
// of planes that can be stopped
// from landing
static int maxPlanes(int[] A, int[] B)
{
// Stores the times needed for
// landing for each plane
Set St = new HashSet<>();
// Iterate over the arrays
for (int i = 0; i < A.length; i++) {
// Stores the time needed for
// landing of current plane
int t = (A[i] % B[i] > 0) ? 1 : 0;
// Update the value of t
t += (A[i] / B[i]) + t;
// Append the t in set St
St.add(t);
}
// Return the answer
return St.size();
}
// Driver Code
public static void main(String[] args)
{
int[] A = { 1, 3, 5, 4, 8 };
int[] B = { 1, 2, 2, 1, 2 };
System.out.println(
maxPlanes(A, B));
}
}
Python3
# Python program for the above approach
# Function to find maximum number
# of planes that can be stopped
# from landing
def maxPlanes(A, B, N):
# Stores the times needed for
# landing for each plane
St = set()
# Iterate over the arrays
for i in range(N):
# Stores the time needed for
# landing of current plane
t = 1 if (A[i] % B[i] > 0) else 0
# Update the value of t
t += (A[i] // B[i]) + t
# Append the t in set St
St.add(t)
# Return the answer
return len(St)
# Driver Code
A = [ 1, 3, 5, 4, 8 ]
B = [ 1, 2, 2, 1, 2 ]
N = len(A)
print(maxPlanes(A, B, N))
# This code is contributed by shivanisinghss2110
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG
{
// Function to find maximum number
// of planes that can be stopped
// from landing
static int maxPlanes(int[] A, int[] B)
{
// Stores the times needed for
// landing for each plane
HashSet St = new HashSet();
// Iterate over the arrays
for (int i = 0; i < A.Length; i++) {
// Stores the time needed for
// landing of current plane
int t = (A[i] % B[i] > 0) ? 1 : 0;
// Update the value of t
t += (A[i] / B[i]) + t;
// Append the t in set St
St.Add(t);
}
// Return the answer
return St.Count;
}
// Driver code
static public void Main (){
int[] A = { 1, 3, 5, 4, 8 };
int[] B = { 1, 2, 2, 1, 2 };
Console.WriteLine(
maxPlanes(A, B));
}
}
// This code is contributed by Potta Lokesh
Javascript
输出:
3
时间复杂度: O(N)
辅助空间: O(N)