1015. Best Good Window

A streaming dashboard keeps nums, the positive load readings of a server over time. A window is a contiguous block of readings, and its score is the smallest reading inside it multiplied by the number of readings in it. A window is called good when it contains the reading at position p, where p = k mod n (so any non-negative k is allowed).

Return the largest score among all good windows. The result always fits in a 32-bit signed integer. Aim for O(n) time and O(1) extra space.

Example 1

Input:
nums = [6,2,9,7,4,8,3], k = 3
Output:
16
Explanation:

Index 3 holds 7. The window from index 2 to 5 is 9, 7, 4, 8: its minimum is 4 and its width is 4, scoring 16, which beats every other window containing index 3.

Example 2

Input:
nums = [8,1,8,8], k = 6
Output:
16
Explanation:

The index is 6 mod 4 = 2. The window of the last two entries scores 8 x 2 = 16, better than any wider window that includes the 1.

Constraints

1 ≤ n = nums.length ≤ 105

1 ≤ nums[i] ≤ 104, 0 ≤ k ≤ 109

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 1,600 msC++ 400 msJava 800 msJavaScript 800 msTypeScript 800 ms

Expected complexity

Time
O(n)
Space
O(1)

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…