Code Room
CodingHardcod-g715
Subject Dp optimizationLevel Senior–Staff~40 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

You must build a tower from a subset (in the given order) of n blocks. dp[i] is the minimum cost to end a valid build at block i, defined by dp[i] = min over j < i of (dp[j] + a[j] * b[i]) with dp[0] = 0, where a is non-increasing and b is non-decreasing (so the convex-hull / Li-Chao optimization applies). Return dp[n-1]. Inputs: arrays a and b of equal length n, with a[0] used as the base. Constraints: 1 <= n <= 100000, values fit in normal ints. A naive O(n^2) is too slow at the top range; the lines a[j]*x have non-increasing slopes queried at non-decreasing x.

Implement
min_build_cost(a: list[int], b: list[int]) → int
Examples
in[[5,3,2],[1,2,4]]out20
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.