【leetcode】1269. Number of Ways to Stay in the Same Place After Some Steps
题目如下:
You have a pointer at index
0in 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
stepsandarrLen, return the number of ways such that your pointer still at index0after exactlystepssteps.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 <= 5001 <= 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 ...
随机推荐
- 【AtCoder】CODE FESTIVAL 2016 qual B
CODE FESTIVAL 2016 qual B A - Signboard -- #include <bits/stdc++.h> #define fi first #define s ...
- 如何配置kindeditor的工具栏
kindeditor编辑器的工具栏主要是指编辑器输入框上方的那些可以操作的菜单,默认情况下编辑器是给予了所有的工具栏.针对不同的用户,不同的项目,不同的环境,可能就需要保留部分工具栏.那么我们应该如何 ...
- C# U盘扫描
无意中看到的一个例程,保留一份. 以供后用. using System; using System.Collections.Generic; using System.ComponentM ...
- Java手写简单Linkedlist一(包括增加,插入,查找,toString,remove功能)
@Java300 学习总结 一.自定义节点 LinkList底层为双向链表.特点为查询效率低,但增删效率高,线程不安全. 链表数据储存在节点,且每个节点有指向上个和下个节点的指针. 创建ggLinke ...
- PB做的托盘程序(最小化后在左下角显示图标)
见‘文件’资源
- Invalid default value for 'time'
原因:安装的MySQL5.7版本之后,date, datetime类型设置默认值"0000-00-00",出现异常:Invalid default value for 'time' ...
- @Html.ActionLink方法
Html.ActionLink:MVC提供的自动构造重写地址的方法,该方法有五个重载 1.Html.ActionLink("linkText","actionName&q ...
- c#学习笔记-string stringBuilder
string aTest = "abc";//分配固定的内存大小 aTest += "ddd"; //销毁原先的数据再来分配,消耗大 StringBuilder ...
- SIP中From ,Contact, Via 和 Record-Route/Route
转载:http://eadgar.blogbus.com/logs/374635.html 注意:以下内容适用于SIP消息中,在具体的应用环境中,例如IMS,每个消息头都有其他独特的意义,但不会和以下 ...
- 垃圾分类,javascript和python
首先,实现的步骤,首先在微信applet中设计一个简单的界面,开始映射到python服务器.有关具体界面,请参阅微信小程序设计指南.以下主要讨论后台服务器交互和处理点. 1.使用js将图像上传到pyt ...