979. Min Times Sum

Medium3.5 points estimatedArrayMonotonic StackPrefix Sum3 hints

A bakery records the daily sales nums of one item. For any non-empty stretch of consecutive days, its score is the smallest daily sale in the stretch multiplied by the total sales over the stretch.

Find the stretch with the largest score and return that largest score modulo 1000000007. Note that the maximum is taken over the true (exact) scores first; only the winning score is reduced with the modulo at the end. Scores are compared exactly, never after reduction.

Example 1

Input:
nums = [5,8,6,2,9]
Output:
95
Explanation:

The block [5,8,6] has weakest 5 and sum 19, giving 95, which beats every other block (for example [8,6] gives 84).

Example 2

Input:
nums = [10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000]
Output:
999999993
Explanation:

The whole row has weakest 10000 and sum 200000, so the best product is 2000000000, reported as 2000000000 mod 1000000007 = 999999993.

Example 3

Input:
nums = [1]
Output:
1
Explanation:

The only block is [1], giving 1 x 1 = 1.

Constraints

  • 1 ≤ nums.length ≤ 105
  • 1 ≤ nums[i] ≤ 104

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 600 msC++ 150 msJava 300 msJavaScript 300 msTypeScript 300 ms

Expected complexity

Time
O(n)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…