hi, let Avik connect you.


** You can give your thought about this content and request modifications if needed from Ask For Modifications page. Thank You.

** You can check all the posts on the Posts page.

** More about me in the About Me section.

Minimum Health to Beat Game


  • Approach:

    1. Calculate the sum of all the damages.

    2. At the same time, find out the maximum element lesser than armor.

    3. Subtract the maximum_element from the damage sum and add 1.

    4. Return the result from step 3.


  • Time and Space Complexity:

      • Time Complexity: O(N)

      • Space Complexity: O(1)


Code [C++]

long long minimumHealth(vector<int>& A, int armor) {


long long sum = 0, maxVal = 0;

int n = A.size();

for(int i=0; i<n; i++){

sum += A[i];

if(A[i] <= armor){

maxVal = max(maxVal, (long long)A[i]);

}

}

return (sum - maxVal + 1);

}