Problem 337465 · medium · Level 03 Linear Management & Searching

Cheap Bundles

sliding window · variable-size window · counting subarrays

A shop displays items in a row with prices prices (each at least 1). A bundle is any set of consecutive items, and its cost is the product of the prices. Return how many bundles cost strictly less than limit.

Examples

Input:  prices = [10, 5, 2, 6], limit = 100
Output: 8
Explanation: [10], [5], [2], [6], [10,5], [5,2], [2,6], [5,2,6].

Input:  prices = [1, 2, 3], limit = 0
Output: 0

Constraints

  • 0 <= len(prices) <= 10**5
  • 1 <= prices[i] <= 1000, 0 <= limit <= 10**6
  • Target complexity: O(n) time; multiplying out every subarray (O(n²)) is too slow for the largest tests.

Goals

  • Count every subarray ending at the current right edge in O(1)
  • Maintain a running product and shrink when it gets too large
Starting Python…