FastPrepApply Bounded Health Deltas

Apply Bounded Health Deltas

ZipRecruiter logoZipRecruiter● EasyNEW GRADOA
Learn

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[]) → int

Examples

Example 1

initialHealth = 12deltas = [-4,-12,6,2]return = 8

The health values are 12 -> 8 -> 0 -> 6 -> 8. The second update is clamped at zero.

Example 2

initialHealth = 95deltas = [10,-3]return = 97

The first update would reach 105, so it is clamped to 100. The final update reduces it to 97.

Constraints

  • 0 <= initialHealth <= 100
  • 1 <= deltas.length <= 100000
  • -100000 <= deltas[i] <= 100000

More ZipRecruiter problems

See ZipRecruiter hiring insights
public int applyBoundedHealthDeltas(int initialHealth, int[] deltas) {
    // Write your code here.
}
initialHealth12
deltas[-4,-12,6,2]
expected8
Checking account…