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

0% completed

Vote For New Content
adi berkowitz
Much simpler solution with a classic moving left pointer up 1 and trying again

adi berkowitz

Dec 8, 2024

Much simpler python solution - Instead of backtracking from right to left in the middle, we simply move left over 1 and start again. It is the same exact thing.

class Solution: def findSubarrays(self, arr, target): result = [] end = len(arr) left = 0 product = 1 while left < end: product = 1 right = left temp_list = [] while right < end and arr[right] * product < target: temp_list.append(arr[right]) result.append(list(temp_list)) product *= arr[right] right += 1 left += 1 return result

0

0

Comments
Comments