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 = [3, 4, 2, 5], limit = 24
Output: 7
Explanation: [3], [4], [2], [5], [3,4], [4,2], [2,5].
[3,4,2] costs exactly 24, which is not less than 24.

Input:  prices = [2, 5], limit = 1
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…