题目如下:

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. 机器猫css

    <html> <head>  <title>机器猫</title>  <style>   div{    width: 30px;    h ...

  2. Promise.all的使用

    我有一个需求如下: 有一个学生号的数组,根据这个数组,取出相关学生的所有信息.获取学生信息的接口用setTime模拟,假设每次请求需要耗时2s,代码如下 // 获取学生信息接口 function ge ...

  3. VSFTP添加用户

    VSFTPD的安装网上有很多教程这里就不多说了,这里主要是针对做主机空间服务的朋友在安装好vsftpd后如何为用户增加ftp账号 先来看一看我们一般在*inux系统下面如何增加用户的 #adduser ...

  4. Java并发与多线程教程(3)

    Java中的锁 锁像synchronized同步块一样,是一种线程同步机制,但比Java中的synchronized同步块更复杂.因为锁(以及其它更高级的线程同步机制)是由synchronized同步 ...

  5. C# Redis分布式锁的应用 - 叶子栈 - SegmentFault 思否

    原文:C# Redis分布式锁的应用 - 叶子栈 - SegmentFault 思否 叶子 1 微信扫一扫 新浪微博 Twitter Facebook C# Redis分布式锁的应用 c#redis分 ...

  6. ant-design的Table组件自定义空状态

    Table,是antd中一个我们经常使用的组件,在官方文档中给出了非常详细的实例以及API, 但在我们在使用过程中可能需要根据项目的要求来定制空状态时的展示. 什么是空状态呢? 在antd的官方文档中 ...

  7. css优先级及其对应的权重

    1.选择器的优先级 !important>内联选择器(style)>id选择器>类选择器 | 属性选择器 | 伪类选择器 > 元素选择器>通配符(*) 2.选择器的权重( ...

  8. python中括号知识点

    Python语言中括号分为几个类型,常见的三个圆括号是圆括号().中间圆括号[]和大括号.它的函数也不同,代表不同的Python基本内置数据类型. python括号 python()中的括号:表示tu ...

  9. JavaScript特点有哪些

    JavaScript特点有哪些 JavaScript 文字脚本语言是一种动态的.弱类型的.基于原型的语言,具有内置的支持类型.它的解释器被称为javascript引擎,是浏览器的一部分,广泛用于客户端 ...

  10. oracel数据泵导出导入

    Oracle11g 使用数据泵导入/导出数据 expdp/impdp 目标:使用oracle数据泵,将A电脑上的数据库databaseA导出后,再导入到B电脑上的数据库databaseB中. A电脑上 ...