📜  英雄震惊的总时间

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

英雄震惊的总时间

给定一个包含N个非递减顺序整数和一个整数D的数组攻击。数组攻击中的每个元素表示英雄在游戏中被攻击的时间,D表示英雄将被震惊并且无法反击的持续时间。任务是找出英雄会受到电击的总时间。

例子:

方法:使用以下公式计算英雄将处于休克状态的时间:

现在要解决此问题,请按照以下步骤操作:

  1. 创建一个变量totalTime来存储这个问题的答案。
  2. 创建一个变量, prev并将其初始化为攻击 [0]。该变量将存储每次攻击的前一次攻击结束的时间。
  3. 运行从i=0i的循环,并且在每次迭代中:
    • 加上attack[i] i的冲击时间,e。 (attack[i] + D) – max(prev, attack[i])totalTime
    • 将 prev 更改为上一次攻击结束的时间,因此prev=attack[i]+D
  4. 在循环结束后返回totalTime作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the total time for
// which hero will be under shock
int heroInShock(vector& attack, int D)
{
 
    int N = attack.size();
    int totalTime = 0;
 
    int prev = attack[0];
 
    for (int i = 0; i < N; i++) {
 
        // Adding time of attack in total time
        totalTime += (attack[i] + D)
                     - max(prev, attack[i]);
 
        // Changing prev to the time where
        // the previous attack ends
        prev = attack[i] + D;
    }
 
    return totalTime;
}
 
// Driver Code
int main()
{
    vector attack = { 1, 2, 6 };
    int D = 2;
    cout << heroInShock(attack, D);
    return 0;
}


Java
// Java program for the above approach
import java.util.ArrayList;
 
class GFG{
 
// Function to find the total time for
// which hero will be under shock
static int heroInShock(ArrayList attack, int D)
{
    int N = attack.size();
    int totalTime = 0;
    int prev = (int)attack.get(0);
 
    for(int i = 0; i < N; i++)
    {
         
        // Adding time of attack in total time
        totalTime += ((int)attack.get(i) + D) -
                      Math.max(prev, (int)attack.get(i));
 
        // Changing prev to the time where
        // the previous attack ends
        prev = (int)attack.get(i) + D;
    }
    return totalTime;
}
 
// Driver code
public static void main(String args[])
{
    ArrayList attack = new ArrayList();
    attack.add(1);
    attack.add(2);
    attack.add(6);
 
    int D = 2;
    System.out.println(heroInShock(attack, D));
}
}
 
// This code is contributed by gfking


Python3
# Python code for the above approach
 
# Function to find the total time for
# which hero will be under shock
def heroInShock(attack, D):
    N = len(attack)
    totalTime = 0
    prev = attack[0]
    for i in range(N):
 
        # Adding time of attack in total time
        totalTime += (attack[i] + D) - max(prev, attack[i])
 
        # Changing prev to the time where
        # the previous attack ends
        prev = attack[i] + D
 
    return totalTime
 
# Driver Code
attack = [1, 2, 6]
D = 2
print(heroInShock(attack, D))
 
# This code is contributed by Saurabh Jaiswal


C#
// C# code to implement above approach
using System;
using System.Collections;
class GFG {
 
  // Function to find the total time for
  // which hero will be under shock
  static int heroInShock(ArrayList attack, int D)
  {
 
    int N = attack.Count;
    int totalTime = 0;
 
    int prev = (int)attack[0];
 
    for (int i = 0; i < N; i++) {
 
      // Adding time of attack in total time
      totalTime += ((int)attack[i] + D)
        - Math.Max(prev, (int)attack[i]);
 
      // Changing prev to the time where
      // the previous attack ends
      prev = (int)attack[i] + D;
    }
 
    return totalTime;
  }
 
  // Driver code
  public static void Main()
  {
    ArrayList attack = new ArrayList();
    attack.Add(1);
    attack.Add(2);
    attack.Add(6);
 
    int D = 2;
    Console.Write(heroInShock(attack, D));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
5

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