{"id":6364,"date":"2020-02-23T00:59:42","date_gmt":"2020-02-23T08:59:42","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6364"},"modified":"2020-02-23T01:03:22","modified_gmt":"2020-02-23T09:03:22","slug":"leetcode-1359-count-all-valid-pickup-and-delivery-options","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1359-count-all-valid-pickup-and-delivery-options\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1359. Count All Valid Pickup and Delivery Options"},"content":{"rendered":"\n<p>Given&nbsp;<code>n<\/code>&nbsp;orders, each order consist in pickup and delivery services.&nbsp;<\/p>\n\n\n\n<p>Count all valid pickup\/delivery possible sequences such that delivery(i) is always after of&nbsp;pickup(i).&nbsp;<\/p>\n\n\n\n<p>Since the answer&nbsp;may be too large,&nbsp;return it modulo&nbsp;10^9 + 7.<\/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 = 1\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> Unique order (P1, D1), Delivery 1 always is after of Pickup 1.\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 = 2\n<strong>Output:<\/strong> 6\n<strong>Explanation:<\/strong> All possible orders: \n(P1,P2,D1,D2), (P1,P2,D2,D1), (P1,D1,P2,D2), (P2,P1,D1,D2), (P2,P1,D2,D1) and (P2,D2,P1,D1).\nThis is an invalid order (P1,D2,P2,D1) because Pickup 2 is after of Delivery 2.\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 = 3\n<strong>Output:<\/strong> 90\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;= 500<\/code><\/li><\/ul>\n\n\n\n<p>Solution: Combination<\/p>\n\n\n\n<p>Let dp[i] denote the number of valid sequence of i nodes.<br><br>For i-1 nodes, the sequence length is 2(i-1).<br>For the i-th nodes,<br>If we put Pi at index = 0, then we can put Di at 1, 2, &#8230;, 2i &#8211; 2 =&gt; 2i-1 options.<br>If we put Pi at index = 1, then we can put Di at 2,3,&#8230;, 2i &#8211; 2 =&gt; 2i &#8211; 2 options.<br>&#8230;<br>If we put Pi at index = 2i-1, then we can put Di at 2i &#8211; 1=&gt; 1 option.<br>There are total (2i &#8211; 1 + 1) \/ 2 * (2i &#8211; 1) = i * (2*i &#8211; 1) options<\/p>\n\n\n\n<p>dp[i] = dp[i &#8211; 1] * i * (2*i &#8211; 1)<\/p>\n\n\n\n<p>or<\/p>\n\n\n\n<p>dp[i] = 2n! \/ 2^n<\/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\nclass Solution {\npublic:\n  int countOrders(int n) {\n    constexpr int kMod = 1e9 + 7;\n    long ans = 1;\n    for (int i = 2; i <= n; ++i)\n      ans = ans * i * (2 * i - 1) % kMod;      \n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given&nbsp;n&nbsp;orders, each order consist in pickup and delivery services.&nbsp; Count all valid pickup\/delivery possible sequences such that delivery(i) is always after of&nbsp;pickup(i).&nbsp; Since the answer&nbsp;may&#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":[122,18,217,121],"class_list":["post-6364","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-combination","tag-dp","tag-hard","tag-permutation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6364","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=6364"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6364\/revisions"}],"predecessor-version":[{"id":6366,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6364\/revisions\/6366"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}