KatInterview

Worst Equity Drop, Best Trade, and Fewest Coins

Dynamic ProgrammingAuto-graded · 22 test cases
MediumAuto-gradedDynamic Programmingarraysdynamic-programminggreedy

Last updated

Return a series' steepest peak-to-trough drop with its indices, the best one-buy-one-sell gain, and the minimum coin count for a target.

You receive four arguments: a cumulative profit-and-loss series pnl, a list of daily share prices prices, a list of coin denominations coins, and a non-negative target amount. Produce three results and return them together in one dictionary whose keys are "drawdown", "profit", and "coins".

1. Steepest drop of the cumulative series ("drawdown")

A drop is anchored at an index p (a peak) and closed at a later-or-equal index t (a trough), with p <= t. Its size is pnl[t] - pnl[p], which is at most zero. Report the most negative value this expression takes over all valid p <= t.

The value under "drawdown" is a list [size, start, end] where:

  • size is that most negative value, or 0 when no pair yields a negative value;
  • start is p + 1, i.e. the index directly following the peak p behind the steepest drop;
  • end is t, the trough index.

Tie-breaking: when several pairs share the same steepest size, keep the pair whose trough t comes first — scan left to right and overwrite only on a strictly smaller size. Likewise, when raising the running peak, only accept a strictly greater value, so the earliest of several equal peaks stays the reference. If the steepest size is 0 (including when pnl is empty), return [0, -1, -1].

2. Best single trade ("profit")

Pick a buy day j and a sell day i with j < i; the gain is prices[i] - prices[j]. Return the largest such gain as a non-negative integer, and 0 when no profitable trade is possible (also when there are fewer than two days).

3. Fewest coins for the target ("coins")

Every denomination in coins may be reused without limit. Return the smallest number of coins whose values add up to exactly amount, or -1 when no combination does (also when coins holds no positive denomination). Denote zero or negative values in coins are skipped. When amount == 0 the answer is always 0.

Return exactly {"drawdown": [...], "profit": ..., "coins": ...}.

Constraints

  • 0 <= len(pnl) <= 10^5
  • 0 <= len(prices) <= 10^5
  • 0 <= len(coins) <= 10^3
  • -10^9 <= pnl[i] <= 10^9
  • 0 <= prices[i] <= 10^9
  • 0 <= amount <= 10^4
  • -10^9 <= coins[i] <= 10^9 (non-positive denominations are ignored)

Examples

Example 1

Input: analyze_series_and_amount([100, 120, 90, 95, 80], [7, 1, 5, 3, 6, 4], [1, 2, 5], 11)

Output: {'drawdown': [-40, 2, 4], 'profit': 5, 'coins': 3}

Example 2

Input: analyze_series_and_amount([5, 4, 3, 2, 1], [7, 6, 4, 3, 1], [2], 3)

Output: {'drawdown': [-4, 1, 4], 'profit': 0, 'coins': -1}

Example 3

Input: analyze_series_and_amount([10, 10, 12, 12], [1, 2, 3], [2, 4], 7)

Output: {'drawdown': [0, -1, -1], 'profit': 2, 'coins': -1}

Example 4

Input: analyze_series_and_amount([0, -5, -2, -10, -3, 4], [3, 8, 2, 5, 1, 7], [2, 4], 7)

Output: {'drawdown': [-10, 1, 3], 'profit': 6, 'coins': -1}

Hints

Hint 1

For the drop, comparing every pair is wasteful: carry the largest value seen so far (plus its position) and measure each new point against it.

Hint 2

The trade gain is the same pass with the extremes swapped — carry the smallest price seen so far rather than the largest.

Hint 3

For coins, greedy can be wrong (coins [1,3,4] making 6), so build a DP table where dp[x] is the fewest coins summing to x, filled in increasing x.