Created
December 2, 2018 02:40
-
-
Save Jojozzc/adca3760f8f64087a693d0362bfa4c8a to your computer and use it in GitHub Desktop.
Find all no-zero continuous interval, then in every interval, set all element 0 except the peek.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def interval_peek(arr): | |
''' | |
Find all no-zero continuous interval, then in every interval, set all element 0 except the peek. | |
:param arr: | |
:return: | |
''' | |
left = 0 | |
arr_len = len(arr) | |
res = arr[:] | |
while left < arr_len: | |
right = left + 1 | |
if res[left] != 0: | |
max_idx = left | |
while right < arr_len and res[right] != 0: | |
if res[right] > res[max_idx]: | |
max_idx = right | |
right += 1 | |
for i in range(left, max_idx): | |
res[i] = 0 | |
for i in range(max_idx + 1, right): | |
res[i] = 0 | |
left = right | |
return res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment