Given two arraysdist and speed consisting of N integers, where dist[i] is the initial distance of the ith giant from the city and speed[i] is the speed of the ith giant. One giant can be eliminated every minute. Therefore, at any time t, at most t giants can be killed. If more giants are able to reach the city in time t, then the game is over. The task is to find the maximum number of giants that can be eliminated before losing, or N if all of the giants can be eliminated before they reach the city.
Input: dist = [1, 3, 4], speed = [1, 1, 1] Output: 3 Explanation: At the start of minute 0, the distances of the giants are [1, 3, 4]. The first giant is eliminated. At the start of 1st minute, the distances of the giants are [X, 2, 3]. No giant is eliminated. At the start of 2nd minute, the distances of the giants are [X, 1, 2]. The second giant is eliminated. At the start of 3rd minute, the distances of the giants are [X, X, 1]. The third giant is eliminated. All 3 giants can be eliminated.
Approach: The idea is to use the greedy approach to solve the problem. Find the time for each giant to come into the city and try to destroy the giant with the least possible time of approaching. Follow the steps below to solve the problem:
Please Login to comment...