KatInterview

Triple Challenge: Price Drops, Bounded Windows, Twin Rows

Arrays & HashingAuto-graded · 17 test cases
MediumAuto-gradedArrays & Hashingbinary-searchgreedysliding-window

Last updated

Combine a threshold binary search with greedy arithmetic sums, a linear scan counting subarrays with exact min and max, and hashing of binary rows to count identical pairs.

Write one function that answers three independent questions and returns the three answers together as a list [revenue, window_count, twin_pairs].

Part 1: Selling with falling prices

stock is a list where stock[i] is the current unit price of product i. Every time you sell one unit of product i, you earn its current price, and the price of that product then drops by 1 (it never goes below 0). Different products are independent.

You must make exactly sales sales in total, choosing which product to sell each time. Maximize the total earnings and return them modulo 1,000,000,007.

If the products can't supply sales sales at positive prices, the remaining sales each earn 0.

Part 2: Subarrays with exact extremes

values is an integer list, and low <= high. Count the contiguous, non-empty subarrays whose smallest element is exactly low and whose largest element is exactly high. If low == high, the subarray must consist entirely of copies of that value.

Part 3: Identical rows after global column flips

grid is a binary matrix. A column flip toggles that column's bit in every row at once, so it never changes whether two rows are equal. Two rows are therefore equivalent exactly when they are identical.

Count the unordered pairs of rows (i, j) with i < j whose contents are identical.

Return value

A list of three integers [revenue, window_count, twin_pairs]:

  • revenue: the Part 1 answer modulo 1,000,000,007.
  • window_count: the exact Part 2 count (not reduced by a modulus).
  • twin_pairs: the exact Part 3 count.

Constraints

  • 1 <= len(stock) <= 2 * 10^5, 1 <= stock[i] <= 10^9
  • 1 <= sales <= 10^13
  • 1 <= len(values) <= 2 * 10^5, -10^9 <= values[i] <= 10^9, low <= high
  • 1 <= len(grid) <= 10^5, each row has the same length m with 1 <= m <= 30, and every entry is 0 or 1
  • Target complexity: roughly O(n log max(stock)) for Part 1, O(n) for Part 2, and O(n * m) for Part 3.

Examples

Example 1

Input: solve_combined_queries([2, 5], 4, [1, 3, 5, 2, 7], 1, 5, [[1, 0, 1], [0, 1, 0], [1, 0, 1]])

Output: [14, 2, 1]

Example 2

Input: solve_combined_queries([3, 3, 3], 5, [1, 1, 1], 1, 1, [[0, 0], [0, 0], [1, 1]])

Output: [13, 6, 1]

Example 3

Input: solve_combined_queries([10], 3, [-2, -1, 0, 1, 2], -1, 1, [[1], [0], [1], [1]])

Output: [27, 1, 3]

Hints

Hint 1

Part 1: always selling the most expensive unit is optimal. Since sales can be 10^13, don't simulate. Binary search a cutoff price so that everything priced above it is sold and the rest are sold at exactly the cutoff.

Hint 2

Part 1: for a cutoff p, the number of units priced at least p is the sum of max(0, x - p + 1). Use the arithmetic series formula for the earnings of each product, and take the modulus only at the end (Python integers don't overflow).

Hint 3

Part 2: scan left to right, tracking the last index of an out-of-range value and the last indices of low and high. For each right end, the valid starts are those after the bad index and at or before the smaller of the two last indices.

Hint 4

Part 3: turn each row into an integer bitmask and count how many earlier rows have the same mask using a dictionary, adding that count to the answer each time.