{"id":5844,"date":"2019-11-24T00:23:17","date_gmt":"2019-11-24T08:23:17","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5844"},"modified":"2019-11-24T00:23:31","modified_gmt":"2019-11-24T08:23:31","slug":"leetcode-1269-number-of-ways-to-stay-in-the-same-place-after-some-steps","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1269-number-of-ways-to-stay-in-the-same-place-after-some-steps\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1269. Number of Ways to Stay in the Same Place After Some Steps"},"content":{"rendered":"\n<p>You have a pointer at index&nbsp;<code>0<\/code>&nbsp;in an array of size&nbsp;<code>arrLen<\/code>. At each step, you can move 1 position to the left, 1 position to the right&nbsp;in the array or stay in the same place&nbsp; (The pointer should not be placed outside the array at any time).<\/p>\n\n\n\n<p>Given two integers&nbsp;<code>steps<\/code>&nbsp;and&nbsp;<code>arrLen<\/code>, return the number of&nbsp;ways such that your pointer still at index&nbsp;<code>0<\/code>&nbsp;after&nbsp;<strong>exactly&nbsp;<\/strong><code>steps<\/code>&nbsp;steps.<\/p>\n\n\n\n<p>Since the answer&nbsp;may be too large,&nbsp;return it&nbsp;<strong>modulo<\/strong>&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> steps = 3, arrLen = 2\n<strong>Output:<\/strong> 4\n<strong>Explanation: <\/strong>There are 4 differents ways to stay at index 0 after 3 steps.\nRight, Left, Stay\nStay, Right, Left\nRight, Stay, Left\nStay, Stay, Stay\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> steps = 2, arrLen = 4\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> There are 2 differents ways to stay at index 0 after 2 steps\nRight, Left\nStay, Stay\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> steps = 4, arrLen = 2\n<strong>Output:<\/strong> 8\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= steps &lt;= 500<\/code><\/li><li><code>1 &lt;= arrLen&nbsp;&lt;= 10^6<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<p>Since we can move at most steps, we can reduce the arrLen to min(arrLen, steps + 1).<\/p>\n\n\n\n<p>dp[i][j] = dp[i-1][j &#8211; 1] + dp[i-1][j] + dp[i-1][j+1] \/\/ sum of right, stay, left<\/p>\n\n\n\n<p>Time complexity: O(steps * steps)<br>Space complexity: O(steps)<\/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 numWays(int steps, int arrLen) {\n    const int kMod = 1e9 + 7;\n    arrLen = min(steps + 1, arrLen);\n    vector<int> dp(arrLen);\n    dp[0] = 1;\n    for (int i = 0; i < steps; ++i) {\n      vector<int> tmp(dp.size());      \n      for (int j = 0; j < arrLen; ++j) {\n        tmp[j] = dp[j];\n        if (j > 0) tmp[j] = (tmp[j] + dp[j - 1]) % kMod;\n        if (j < arrLen - 1) tmp[j] = (tmp[j] + dp[j + 1]) % kMod;\n      }\n      swap(dp, tmp);\n    }\n    return dp[0];\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You have a pointer at index&nbsp;0&nbsp;in an array of size&nbsp;arrLen. At each step, you can move 1 position to the left, 1 position to the&#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":[18,217],"class_list":["post-5844","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-hard","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5844","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=5844"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5844\/revisions"}],"predecessor-version":[{"id":5846,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5844\/revisions\/5846"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5844"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}