Grokking the Coding Interview: Patterns for Coding Questions
Vote

0% completed

​

Solution: Longest Continuous Subarray

.....

.....

.....

Like the course? Get enrolled and start learning!
A

Anonymous

· 2 years ago

from collections import defaultdict from bisect import bisect_left, bisect_right, insort_left class Solution: @staticmethod def longestSubarray(nums, limit): sorted_window = [] left = maxLength = 0 for right, value in enumerate(nums): insort_left(sorted_window, value) # Shrink the window if the condition is violated while sorted_window[-1] - sorted_window[0] > limit: sorted_window.pop(bisect_left(sorted_window, nums[left])) left += 1 # Update the maximum length found maxLength = max(maxLength, right - left + 1) return maxLength

Reading Progress

0%


Vote for new content