Skip to the code
cf.
+25 XP

#137 Longest Uptime With k Patches

medium

A status page logs each hour as 1 (the service was up) or 0 (it was down). An engineer can mark at most k down hours as patched, turning them into 1s.

Write longestUptime(hours, k) that returns the length of the longest run of neighboring hours that are all 1 after patching at most k of the 0s.

Example 1

Input: hours = [1,1,0,0,1,1,1,0,1], k = 1
Output: 5

Patch the 0 at index 7 to get 1, 1, 1, 1, 1 from index 4 to 8.

Example 2

Input: hours = [0,0,0], k = 0
Output: 0

No patches and no up hours.

Constraints

  • 0 <= hours.length <= 100000
  • hours[i] is 0 or 1.
  • 0 <= k <= hours.length
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).