Skip to the code
cf.
+25 XP

#138 Scale Chains Below a Limit

medium

In a design tool, nested groups each scale their contents by a whole-number factor, listed in order in factors. A run of neighboring groups multiplies its factors together, and the editor warns when that product reaches limit or more.

Write countSafeChains(factors, limit) that returns how many non-empty runs of neighboring groups have a product strictly less than limit.

Example 1

Input: factors = [10,5,2,6], limit = 100
Output: 8

The runs [10], [5], [2], [6], [10, 5], [5, 2], [2, 6], [5, 2, 6] all stay under 100.

Example 2

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

No product is below 0.

Constraints

  • 0 <= factors.length <= 30000
  • 1 <= factors[i] <= 1000
  • 0 <= limit <= 10^6
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).