Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Sachin Dev S
Python Solution

Sachin Dev S

Jul 28, 2024

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

1

0

Comments
Comments

On this page