题目如下:

You have a pointer at index 0 in an array of size arrLen. At each step, you can move 1 position to the left, 1 position to the right in the array or stay in the same place  (The pointer should not be placed outside the array at any time).

Given two integers steps and arrLen, return the number of ways such that your pointer still at index 0 after exactly steps steps.

Since the answer may be too large, return it modulo 10^9 + 7.

Example 1:

Input: steps = 3, arrLen = 2
Output: 4
Explanation: There are 4 differents ways to stay at index 0 after 3 steps.
Right, Left, Stay
Stay, Right, Left
Right, Stay, Left
Stay, Stay, Stay

Example 2:

Input: steps = 2, arrLen = 4
Output: 2
Explanation: There are 2 differents ways to stay at index 0 after 2 steps
Right, Left
Stay, Stay

Example 3:

Input: steps = 4, arrLen = 2
Output: 8

Constraints:

  • 1 <= steps <= 500
  • 1 <= arrLen <= 10^6

解题思路:记dp[i][j]为移动i次后恰好位于下标j的次数,要使得第i步移动到j,那么第i-1步所处的位置就只能是 [j-1,j,j+1],所以有dp[i][j] = dp[i-1][j-1] + dp[i-1][j] + dp[i-1][j+1] 。

代码如下:

class Solution(object):
def numWays(self, steps, arrLen):
"""
:type steps: int
:type arrLen: int
:rtype: int
"""
arrLen = min(arrLen,steps) dp = [[0] * (arrLen) for _ in range(steps+1)] dp[1][0] = 1
dp[1][1] = 1 for i in range(1,steps+1):
for j in range(len(dp[i])):
dp[i][j] += dp[i-1][j]
if j - 1 >= 0 and j - 1 < len(dp[i]):
dp[i][j] += dp[i-1][j-1]
if j + 1 < len(dp[i]):
dp[i][j] += dp[i-1][j+1] return dp[-1][0] % (10**9 + 7)

【leetcode】1269. Number of Ways to Stay in the Same Place After Some Steps的更多相关文章

  1. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  2. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  3. 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)

    [LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...

  4. 【LeetCode】Single Number I & II & III

    Single Number I : Given an array of integers, every element appears twice except for one. Find that ...

  5. LeetCode 5274. Number of Ways to Stay in the Same Place After Some Steps - Java - DP

    题目链接:5274. 停在原地的方案数 You have a pointer at index 0 in an array of size arrLen. At each step, you can ...

  6. 【LeetCode】476. Number Complement (java实现)

    原题链接 https://leetcode.com/problems/number-complement/ 原题 Given a positive integer, output its comple ...

  7. 【leetcode】1155. Number of Dice Rolls With Target Sum

    题目如下: You have d dice, and each die has f faces numbered 1, 2, ..., f. Return the number of possible ...

  8. 【LeetCode】191. Number of 1 Bits 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 右移32次 计算末尾的1的个数 转成二进制统计1的个 ...

  9. 【LeetCode】1128. Number of Equivalent Domino Pairs 等价多米诺骨牌对的数量(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计 代码 复杂度分析 日期 题目地址:http ...

随机推荐

  1. GrapeCity Documents (服务端文档API组件) V3.0 正式发布

    近日,葡萄城GrapeCity Documents(服务端文档API组件)V3.0 正式发布! 该版本针对 Excel 文档.PDF 文档和 Word 文档的 API 全面更新,加入了用于生成 Exc ...

  2. Ubuntu18突然卡死解决方法

    emmmm 1.Ctrl+Alt+F2/F3/F4/F5/F6     F2-6随便选一个都可以 2.进入tty终端后先输入用户名和密码(记得小键盘会自动

  3. Devexpress xaf BO中字段为RuleRequiredField必输字段时,文本标签默认添加*标记

    BO中字段为RuleRequiredField必输字段时,文本标签默认添加*标记.需要在模型编辑器中设置,如图. 官网地址:https://docs.devexpress.com/eXpressApp ...

  4. SQL Server循环插入

    一个SQL循环插入的代码,运行正常: BEGIN DECLARE @idx AS INT; DECLARE @NodeName nvarchar(255); DECLARE @OtherName nv ...

  5. VMware与主机联网设置

    1.编辑-虚拟机网络编辑器 2.虚拟机设置 3. 4.主机ping虚拟机

  6. Redis 消息队列 初体验

    队列之生产者.消费者模式 using System; using System.Threading; using NServiceKit.Redis; namespace ConsoleApplica ...

  7. AngularJS-01.AngularJS,Module,Controller,scope

    1.AngularJS 一个构建动态Web应用程序的结构化框架. 基于JavaScript的MVC框架.(  MVC ---- Model(模型).View(视图).Controller(控制器) ) ...

  8. StoneTab标签页CAD插件 3.2.2

    //////////////////////////////////////////////////////////////////////////////////////////////////// ...

  9. caffe prototxt分析

    测试用prototxt name: "CIFAR10_quick"layer { name: "data" type: "MemoryData&quo ...

  10. beego从入门到弃坑(一)

      最近由于要写课程设计的原因,我便开始一边学习beego,一边开始用它写一个小型的管理系统.但是只有你真正的去用的时候,才会发现这个框架巨坑,他是第一个让我写出了心里阴影的框架,也是第一个让我写着写 ...