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

0% completed

Vote For New Content
Mohammed Dh Abbas
This is a better O(N)^2 solution

Mohammed Dh Abbas

Jun 23, 2024

This is a better O(N)^2 solution . the official solution is not ideal and confusing .

Simply do a double loop as below

def findSubarrays(self, arr, target): result = [] for i in range(len(arr)): if arr[i] < target: result.append([arr[i]]) mpy = arr[i] path = [arr[i]] for j in range(i + 1, len(arr)): mpy *= arr[j] path.append(arr[j]) if mpy < target: result.append(path[:])

1

0

Comments
Comments
softest mike
softest mikea year ago

It's a cleaner solution but still O(N^2)