Build Array Where You Can Find The Maximum Exactly K Comparisons - Complete Solution Guide
Build Array Where You Can Find The Maximum Exactly K Comparisons is LeetCode problem 1420, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given three integers n , m and k . Consider the following algorithm to find the maximum element of an array of positive integers: You should build the array arr which has the following properties: arr has exactly n integers. 1 <= arr[i] <= m where (0 <= i < n) . After applying the mentioned algorithm to arr , the value search_cost is equal to k . Return the number of ways to build the array arr under the mentioned conditions. As the answer may grow large, the answer must be computed modu
Detailed Explanation
The problem asks us to find the number of arrays of length `n` where each element is between 1 and `m` (inclusive), such that a specific algorithm to find the maximum element of the array makes exactly `k` comparisons (search cost). The provided algorithm iterates through the array, updating the maximum and incrementing a counter (`search_cost`) each time a new maximum is found. We need to count how many such arrays exist, modulo 10^9 + 7.
Solution Approach
The solution uses dynamic programming. `dp[i][c][val]` represents the number of arrays of length `i` with a maximum value of `val` and search cost equal to `c`. We build up the `dp` table from `i = 1` to `n`. A key optimization is using prefix sums to efficiently calculate the number of ways to extend the array when the maximum value increases. The prefix sum array `prefix[c][val]` stores the sum of `dp[c][1]`, `dp[c][2]`, ..., `dp[c][val]`. This allows to compute the number of transitions with `search_cost` incremented efficiently.
Step-by-Step Algorithm
- Step 1: Initialize a DP table `dp[k+1][m+1]` and prefix sum table `prefix[k+1][m+1]`. `dp[c][val]` will store the number of arrays such that the search cost is `c` and the maximum value in the array is `val`.
- Step 2: Base case: For `i = 1` (arrays of length 1), `dp[1][val] = 1` for all `val` from 1 to `m`. Also, initialize the prefix sum: `prefix[1][val] = prefix[1][val-1] + dp[1][val]`.
- Step 3: Iterate from `i = 2` to `n` (array lengths). For each length `i`, iterate `c` from 1 to `k` (search cost) and `val` from 1 to `m` (maximum value).
- Step 4: Calculate `dp[c][val]`. It consists of two terms: 1) The number of arrays of length `i-1` with maximum `val` and cost `c`, extended by `val`, which doesn't change the search cost multiplied by the number of array elements not exceeding `val`, which is `dp[c][val]` * `val`. 2) The number of arrays of length `i-1` with max < `val` and cost `c-1`. The contribution of this is `prefix[c - 1][val - 1]`.
- Step 5: Calculate `prefix[c][val] = prefix[c][val-1] + dp[c][val]`.
- Step 6: After iterating through all array lengths, return `prefix[k][m]`, which represents the total number of arrays of length `n`, with elements between 1 and `m`, and a search cost equal to `k`.
Key Insights
- Insight 1: Dynamic programming is suitable for this problem because the number of arrays with specific properties can be built incrementally based on smaller subarrays.
- Insight 2: The `search_cost` (k) and maximum value encountered so far are crucial state variables for defining subproblems.
- Insight 3: Prefix sums can optimize the calculation of the number of ways to build arrays when the new maximum is introduced.
Complexity Analysis
Time Complexity: O(n*m*k)
Space Complexity: O(m*k)
Topics
This problem involves: Dynamic Programming, Prefix Sum.
Companies
Asked at: Amdocs, Dunzo.