Skip to the code
cf.
+25 XP

#133 Best Run of Distinct Products

medium

A product carousel shows k neighboring items from a feed at a time. The feed lists product prices in prices, and the same price means the same product. The marketing team wants a run of k items where every product is different and the total price is as high as possible.

Write bestDistinctRun(prices, k) that returns the largest total of k consecutive prices that are all different. If no such run exists, return 0.

Example 1

Input: prices = [1,5,4,2,9,9,9], k = 3
Output: 15

[5, 4, 2] totals 11 and [4, 2, 9] totals 15. Any run with two 9s has a repeat.

Example 2

Input: prices = [4,4,4], k = 3
Output: 0

The only run repeats 4.

Constraints

  • 1 <= k <= prices.length <= 100000
  • 1 <= prices[i] <= 10^5
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).