Apply Bounded Health Deltas
Problem statement
Start with initialHealth. Apply each integer in deltas from left to right.
After every delta, clamp health to the inclusive range from 0 through 100: values below zero become zero, and values above one hundred become one hundred.
Return the final health after every delta has been processed.
Function
applyBoundedHealthDeltas(initialHealth: int, deltas: int[]) → intExamples
Example 1
initialHealth = 12deltas = [-4,-12,6,2]return = 8The health values are 12 -> 8 -> 0 -> 6 -> 8. The second update is clamped at zero.
Example 2
initialHealth = 95deltas = [10,-3]return = 97The first update would reach 105, so it is clamped to 100. The final update reduces it to 97.
Constraints
0 <= initialHealth <= 1001 <= deltas.length <= 100000-100000 <= deltas[i] <= 100000