【leetcode】1269. Number of Ways to Stay in the Same Place After Some Steps
题目如下:
You have a pointer at index
0
in an array of sizearrLen
. 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
andarrLen
, return the number of ways such that your pointer still at index0
after exactlysteps
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, StayExample 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, StayExample 3:
Input: steps = 4, arrLen = 2
Output: 8Constraints:
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的更多相关文章
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
- 【LeetCode】Single Number I & II & III
Single Number I : Given an array of integers, every element appears twice except for one. Find that ...
- 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 ...
- 【LeetCode】476. Number Complement (java实现)
原题链接 https://leetcode.com/problems/number-complement/ 原题 Given a positive integer, output its comple ...
- 【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 ...
- 【LeetCode】191. Number of 1 Bits 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 右移32次 计算末尾的1的个数 转成二进制统计1的个 ...
- 【LeetCode】1128. Number of Equivalent Domino Pairs 等价多米诺骨牌对的数量(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计 代码 复杂度分析 日期 题目地址:http ...
随机推荐
- Pandas 读取超过 65536 行的 Excel 文件
Excel 文件的格式曾经发生过一次变化,在 Excel 2007 以前,使用扩展名为 .xls 格式的文件,这种文件格式是一种特定的二进制格式,最多支持 65,536 行,256 列表格.从 Exc ...
- Shell脚本之流程控制(if、for、while)
if 判断 if语句的三种格式: (1)if (2)if else (3)if elif else 语法格式如下: #if 语法格式 if 条件 then 命令1... 命令2... fi #if e ...
- .net操作数据库
1. 自动生化曾的一些查询 可以自定义查询语句 2.自定义查询过程调用 USE [mydb] GO /****** Object: StoredProcedure [dbo].[procLogin] ...
- Java学习总结一 数据类型
@Java300 学习总结 一.Java 基本数据类型分类如下: 整型变量:byte.short.int.long 浮点型变量:float.double 字符型变量:char 布尔型变量:boolea ...
- Devepxress xaf Dashboard中DetailView控件使其可编辑
最开始用Devexpress xaf Dashboard做界面的时候,DetailView界面里面的控件都无法编辑,后来解决了这个问题,记录下来供大家参考. 解决方法:创建ViewController ...
- 后端排序,debug模式中map的顺序出错
js中map遍历的顺序是按照插入的顺序来执行的.如果map的来源是字符串转换的,那么就会按照字符串中key值的顺序进行遍历.千万不要被debug中显示的顺序误导,这里应该是为了方便查看对key进行了字 ...
- 数据结构和算法总结(三):A* 寻路算法
前言 复习下寻路相关的东西,而且A star寻路在游戏开发中应用挺多的,故记录下. 正文 迪杰斯特拉算法 说起A*得先谈谈Dijkstra算法,它是在BFS基础上的一种带权值的两点最短寻路贪心算法. ...
- redhad 7.0更换yum源
1. 卸载红帽yum源 rpm –e $(rpm –qa|grep yum) --nodeps 2.删除所有repo相关文件 rm –rf /etc/yum.conf rm –rf /etc/yum ...
- uni-app使用Canvas绘图
最近公司项目在用uni-app做小程序商城,其中商品和个人需要生成图片海报,经过摸索记录后将一些重点记录下来.这里有两种方式来生成 1.后台控制生成 2.前端用canvas合成图片 这里我们只讲使用c ...
- 对于div里面内容过大根据长度或者宽度进行适配,然后可以滚轮缩放的功能
在做3000的项目中,因为页面的svg很大,但是做的只是适配电脑,打开肯定是看不全的,要看全就必须进行滚动,可是客户提出了将页面放在电视机上面,用电视输入网址直接访问,这样问题就来了,电视上怎么进行滚 ...