📜  计算在到达城市之前可以被摧毁的巨人的最大数量

📅  最后修改于: 2021-10-26 06:44:04             🧑  作者: Mango

给定两个由N 个整数组成的数组dist[]speed[] ,其中dist[i]是第i巨人与城市的初始距离, speed[i]是第i巨人的速度。每分钟都可以消灭一个巨人。因此,在任何时间t ,最多可以杀死t 个巨人。如果更多的巨人能够在时间t到达城市,那么游戏就结束了。任务是在输掉之前找到最大可以消除的巨人数量,或者如果所有的巨人在到达城市之前都可以消除,则为N。

例子:

方法:思想是用贪心的方法来解决问题。找到每个巨人进入城市的时间,并尝试以尽可能少的接近时间摧毁巨人。请按照以下步骤解决问题:

  • 初始化一个向量timezone[]来存储时间。
  • 使用变量i在范围[0, N] 上迭代并执行以下步骤:
    • dist[i]/speed[i]的值推入向量timezone[]。
  • 按升序对向量timezone[]进行排序。
  • 将变量curr_time初始化为0以存储当前时间,将killcount 初始化0以存储被杀死的巨人数量。
  • 使用变量i在范围[0, N] 上迭代并执行以下步骤:
    • 如果timezone[i]小于curr_time ,则中断循环。
    • 否则,将curr_timekillcount的计数增加1。
  • 执行完上述步骤后,返回killcount的值作为答案。

下面是上述方法的实现。

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count the maximum number
// of giants that can be destroyed
int eliminateMaximum(int dist[], int N,
                     int speed[])
{
    // Make a vector of time and
    // store the time corresponding
    // to its distance and speed
    vector timezone;
 
    for (int i = 0; i < N; i++) {
 
        timezone.push_back((double)dist[i]
                           / (double)speed[i]);
    }
 
    // Sort time in ascending order
    sort(timezone.begin(), timezone.end());
 
    // Stores the time at each instant
    int Curr_time = 0;
 
    // Stores count of giants killed
    int killcount = 0;
 
    for (auto i : timezone) {
        if (i <= Curr_time) {
 
            // Game is lost
            break;
        }
        else {
            Curr_time++;
            killcount++;
        }
    }
    return killcount;
}
 
// Driver Code
int main()
{
    // Given input
    int dist[] = { 1, 3, 4 };
    int N = sizeof(dist) / sizeof(dist[0]);
 
    int speed[] = { 1, 1, 1 };
    int M = sizeof(speed) / sizeof(speed[0]);
 
    // function call
    cout << eliminateMaximum(dist, N, speed);
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to count the maximum number
// of giants that can be destroyed
static int eliminateMaximum(int dist[], int N,
                     int speed[])
{
   
    // Make a vector of time and
    // store the time corresponding
    // to its distance and speed
    Vector timezone = new Vector();
 
    for (int i = 0; i < N; i++) {
 
        timezone.add((double)dist[i]
                           / (double)speed[i]);
    }
 
    // Sort time in ascending order
    Collections.sort(timezone);
 
    // Stores the time at each instant
    int Curr_time = 0;
 
    // Stores count of giants killed
    int killcount = 0;
 
    for (Double i : timezone) {
        if (i <= Curr_time) {
 
            // Game is lost
            break;
        }
        else {
            Curr_time++;
            killcount++;
        }
    }
    return killcount;
}
 
// Driver Code
public static void main(String[] args)
{
    // Given input
    int dist[] = { 1, 3, 4 };
    int N = dist.length;
 
    int speed[] = { 1, 1, 1 };
    int M = speed.length;
 
    // function call
    System.out.print(eliminateMaximum(dist, N, speed));
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python program for the above approach
 
# Function to count the maximum number
# of giants that can be destroyed
def eliminateMaximum(dist, N, speed):
 
    # Make a vector of time and
    # store the time corresponding
    # to its distance and speed
    timezone = []
 
    for i in range(N):
        timezone.append(dist[i] / speed[i])
 
 
    # Sort time in ascending order
    timezone.sort()
 
    # Stores the time at each instant
    Curr_time = 0
 
    # Stores count of giants killed
    killcount = 0
 
    for i in timezone:
        if (i <= Curr_time) :
            # Game is lost
            break
 
        else:
            Curr_time += 1
            killcount += 1
         
    return killcount
 
# Driver Code
 
# Given input
dist = [1, 3, 4]
N = len(dist)
 
speed = [1, 1, 1]
M = len(speed)
 
# function call
print(eliminateMaximum(dist, N, speed))
 
# This code is contributed by gfgking


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
// Function to count the maximum number
// of giants that can be destroyed
static int eliminateMaximum(int []dist, int N,
                     int []speed)
{
    // Make a vector of time and
    // store the time corresponding
    // to its distance and speed
    List timezone = new List();
 
    for (int i = 0; i < N; i++) {
 
        timezone.Add((double)dist[i]/speed[i]);
    }
 
    // Sort time in ascending order
    timezone.Sort();
 
    // Stores the time at each instant
    int Curr_time = 0;
 
    // Stores count of giants killed
    int killcount = 0;
 
    foreach(double i in timezone) {
        if (i <= Curr_time) {
 
            // Game is lost
            break;
        }
        else {
            Curr_time++;
            killcount++;
        }
    }
    return killcount;
}
 
// Driver Code
public static void Main()
{
    // Given input
    int []dist = { 1, 3, 4 };
    int N = dist.Length;
 
    int []speed = { 1, 1, 1 };
    int M = speed.Length;
 
    // function call
    Console.Write(eliminateMaximum(dist, N, speed));
}
}
 
// This code is contributed by ipg2016107.


Javascript


输出:
3

时间复杂度: O(N * log(N))
辅助空间: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程