📜  在给定的初始位置和速度的帮助下,最大化每秒可以停止的飞机数量

📅  最后修改于: 2022-05-13 01:56:05.496000             🧑  作者: Mango

在给定的初始位置和速度的帮助下,最大化每秒可以停止的飞机数量

给定两个由N个整数组成的数组A[]B[] ,其中A[i]表示第i飞机的初始位置, B[i]是飞机着陆的速度,任务是打印数字可以通过每秒射击一架飞机来阻止飞机着陆的飞机。

例子:

方法:可以根据以下观察解决给定的问题:

  • 可以观察到,可以停止的飞机数量是每架飞机降落所需的不同时间的集合,因为同时只能摧毁一架飞机。
  • 飞机降落所需的时间是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)