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
Problem: 2214. Minimum Health to Beat Game
Problem Link: leetcode.com/problems/minimum-health-to-beat-game/
Alternate Link: leetcode.ca/2022-04-16-2214-Minimum-Health-to-Beat-Game/
Companies Asked: Amazon
Approach:
Calculate the sum of all the damages.
At the same time, find out the maximum element lesser than armor.
Subtract the maximum_element from the damage sum and add 1.
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);
}