Back to course home
0% completed
Vote For New Content
Why is the solution posted so confusing - I believe mine is simpler
adi berkowitz
Jun 5, 2024
I have my own solution that I think is way less complex and similar efficiency.
We have outer loop which is O(N) and then we do an inner loop to get subarrays and we unpack arrays as we add them making the overall time complexity O(N^3)
Space O(N^3)
class Solution: def findSubarrays(self, arr, target): result = [] end = len(arr) # TODO: Write your code here # Go through the entire array for i, num in enumerate(arr): # Pointer starts after current index (for continuous array) pointer = i+1 product = num product_arr = [num] # Technically it is possible to have a current number that is more than target # But 0 as the next number - this would mean individual target is not but joined it is if product < target: result.append(product_arr) while pointer < end and arr[pointer]*product < target: # If we are still less than target, append a new array to result and update product! product *= arr[pointer] product_arr=[arr[pointer], *product_arr] result.append(product_arr) pointer += 1 return result
0
0
Comments
Comments