{"id":6652,"date":"2020-04-20T23:18:20","date_gmt":"2020-04-21T06:18:20","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6652"},"modified":"2020-04-20T23:20:52","modified_gmt":"2020-04-21T06:20:52","slug":"leetcode-1420-build-array-where-you-can-find-the-maximum-exactly-k-comparisons","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1420-build-array-where-you-can-find-the-maximum-exactly-k-comparisons\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1420. Build Array Where You Can Find The Maximum Exactly K Comparisons"},"content":{"rendered":"\n<p>Given three integers&nbsp;<code>n<\/code>,&nbsp;<code>m<\/code>&nbsp;and&nbsp;<code>k<\/code>. Consider the following algorithm to find the maximum element of an array of positive integers:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/04\/02\/e.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>You should build the array arr which has the following properties:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>arr<\/code>&nbsp;has exactly&nbsp;<code>n<\/code>&nbsp;integers.<\/li><li><code>1 &lt;= arr[i] &lt;= m<\/code>&nbsp;where&nbsp;<code>(0 &lt;= i &lt; n)<\/code>.<\/li><li>After applying the mentioned algorithm to&nbsp;<code>arr<\/code>, the value&nbsp;<code>search_cost<\/code>&nbsp;is equal to&nbsp;<code>k<\/code>.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the number of ways<\/em>&nbsp;to build the array&nbsp;<code>arr<\/code>&nbsp;under the mentioned conditions.&nbsp;As the answer may grow large, the answer&nbsp;<strong>must be<\/strong>&nbsp;computed modulo&nbsp;<code>10^9 + 7<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 2, m = 3, k = 1\n<strong>Output:<\/strong> 6\n<strong>Explanation:<\/strong> The possible arrays are [1, 1], [2, 1], [2, 2], [3, 1], [3, 2] [3, 3]\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 5, m = 2, k = 3\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> There are no possible arrays that satisify the mentioned conditions.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 9, m = 1, k = 1\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> The only possible array is [1, 1, 1, 1, 1, 1, 1, 1, 1]\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 50, m = 100, k = 25\n<strong>Output:<\/strong> 34549172\n<strong>Explanation:<\/strong> Don't forget to compute the answer modulo 1000000007\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 37, m = 17, k = 7\n<strong>Output:<\/strong> 418930126\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n &lt;= 50<\/code><\/li><li><code>1 &lt;= m &lt;= 100<\/code><\/li><li><code>0 &lt;= k &lt;= n<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<p>dp[n][m][k] := ways to build given n,m,k.<\/p>\n\n\n\n<p>dp[n][m][k] = sum(dp[n-1][m][k] + dp[n-1][i-1][k-1] &#8211; dp[n-1][i-1][k]), 1 &lt;= i &lt;= m<br>dp[1][m][1] = m<br>dp[n][m][k] = 0 if k &lt; 1 or k > m or k > n<\/p>\n\n\n\n<p>Time complexity: O(n*m^2*k)<br>Space complexity: O(n*m*k)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nint mem[51][101][51] = {0};\nclass Solution {\npublic:\n  int numOfArrays(int n, int m, int k) {\n    const int kMod = 1e9 + 7;    \n    function<int(int, int, int)> dp = [&dp](int n, int m, int k) {\n      if (k < 1 || k > m || k > n) return 0;\n      if (n == 1 && k == 1) return m;\n      if (mem[n][m][k]) return mem[n][m][k];      \n      long ans = 0;\n      for (int i = 1; i <= m; ++i) {\n        ans += dp(n - 1, m, k);\n        ans += dp(n - 1, i - 1, k - 1);\n        ans -= dp(n - 1, i - 1, k);\n        ans = (ans + kMod) % kMod;\n      }\n      return mem[n][m][k] = ans;\n    };    \n    return dp(n, m, k);\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given three integers&nbsp;n,&nbsp;m&nbsp;and&nbsp;k. Consider the following algorithm to find the maximum element of an array of positive integers: You should build the array arr which&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[8,18,217],"class_list":["post-6652","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-counting","tag-dp","tag-hard","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6652","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6652"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6652\/revisions"}],"predecessor-version":[{"id":6655,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6652\/revisions\/6655"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}