Feed Review Opening Fees

Minimum total start cost for a constrained contiguous partition

HardSliding WindowDynamic ProgrammingMonotonic Deque

A social network must divide a chronological feed into review batches. The string authors identifies the author of each post: equal characters mean the same author.

Every post must belong to exactly one batch. Each batch must be a nonempty contiguous part of the feed, and batches must preserve the original order.

A batch is allowed only when:

  • It contains at least min_posts and at most max_posts posts.
  • It contains posts from at most author_limit distinct authors.

Opening a batch at index i costs opening_cost[i]. There is no other cost for that batch. Indices are zero-based.

Return the minimum total opening cost of a valid division of the entire feed. Return -1 if no valid division exists.

Examples

Example 1

Input: authors = "abacbc", opening_cost = [4,1,8,2,5,3], min_posts = 2, max_posts = 3, author_limit = 2
Output: 6

Divide the feed into `aba` and `cbc`. Both batches have three posts and two distinct authors. Comparing this division with all other allowed divisions shows that it has the lowest total opening cost.

Example 2

Input: authors = "abc", opening_cost = [2,1,3], min_posts = 2, max_posts = 3, author_limit = 1
Output: -1

Every batch needs at least two posts, but every pair of adjacent posts has two different authors. The one-author limit therefore makes a complete division impossible.

Example 3

Input: authors = "aaab", opening_cost = [5,0,0,0], min_posts = 1, max_posts = 4, author_limit = 2
Output: 5

The entire feed may be one batch. Additional batches cannot improve the cost because every opening cost is nonnegative.

Constraints

  • 1 <= authors.length <= 100000
  • authors contains only lowercase English letters
  • opening_cost.length == authors.length
  • 0 <= opening_cost[i] <= 1000000000
  • 1 <= min_posts <= max_posts <= authors.length
  • 1 <= author_limit <= 26

A valid division is not guaranteed to exist. Only the minimum total cost is returned, so ties between divisions do not require a tie-breaking rule. Use a numeric type that safely represents totals up to 10^14. An O(n) time and O(n) space solution is expected.

Hints

Show hint 1

For each exclusive right endpoint, valid batch starts form an interval. Maintain its distinct-author lower boundary with a sliding window, then intersect it with the length restrictions.

Show hint 2

Let dp[i] be the cheapest division of the first i posts. A candidate batch start i contributes dp[i] + opening_cost[i]. Candidates become eligible after min_posts posts and expire in increasing index order, allowing a monotonic deque.

Follow-up questions

What an interviewer might ask once you have a working solution.

  • How would you reconstruct one optimal list of batch boundaries?
  • How would the recurrence change if every batch also paid the sum of a separate per-post cost array?

Practice this with an AI interviewer

Explain your approach out loud, write Python or JavaScript, run it against hidden tests (including large inputs), and get a scored debrief.

Start this problem