题目描述:

自己的提交:

class Solution:
def numWays(self, steps: int, arrLen: int) -> int:
l = min(steps,arrLen)
dp = [0] * l
dp[0] = 1
MOD = 10 ** 9 + 7
for step in range(steps):
dp_ = dp[:]
for i in range(len(dp)):
if i == 0:
dp_[i] = (dp[i] + dp[i+1]) % MOD
elif i == len(dp) - 1:
dp_[i] = (dp[i] + dp[i-1]) % MOD
else:
dp_[i] = (dp[i-1] + dp[i] + dp[i+1]) % MOD
dp = dp_
return dp[0]

另:

class Solution:
def numWays(self, steps: int, arrLen: int) -> int:
l = min(steps,arrLen)
dp = [0] * l
dp[0] = 1
MOD = 10 ** 9 + 7
for step in range(steps):
dp_ = [0] * l
for i in range(len(dp)):
for j in [-1,0,1]:
if 0 <= i+j < l:
dp_[i] += dp[i+j]
dp_[i] %= MOD
dp = dp_ return dp[0]

leetcode-164周赛-1269-停在原地的方案数的更多相关文章

  1. 【js】Leetcode每日一题-停在原地的方案数

    [js]Leetcode每日一题-停在原地的方案数 [题目描述] 有一个长度为 arrLen 的数组,开始有一个指针在索引 0 处. 每一步操作中,你可以将指针向左或向右移动 1 步,或者停在原地(指 ...

  2. Leetcode 5274. 停在原地的方案数

    纪念第一次正式参加,听说这次题目很水,感觉确实不是很难,一般前两题都很简单,这次,到第三题的时候,都还可以做,emm...... 实际代码记录: #include <iostream> # ...

  3. Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II)

    Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II) 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n ...

  4. [LeetCode] 164. Maximum Gap 求最大间距

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

  5. LeetCode 164. Maximum Gap[翻译]

    164. Maximum Gap 164. 最大间隔 Given an unsorted array, find the maximum difference between the successi ...

  6. Java实现 LeetCode 164 最大间距

    164. 最大间距 给定一个无序的数组,找出数组在排序之后,相邻元素之间最大的差值. 如果数组元素个数小于 2,则返回 0. 示例 1: 输入: [3,6,9,1] 输出: 3 解释: 排序后的数组是 ...

  7. ✡ leetcode 164. Maximum Gap 寻找最大相邻数字差 --------- java

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

  8. Java for LeetCode 164 Maximum Gap

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

  9. leetcode[164] Maximum Gap

    梅西刚梅开二度,我也记一题. 在一个没排序的数组里,找出排序后的相邻数字的最大差值. 要求用线性时间和空间. 如果用nlgn的话,直接排序然后判断就可以了.so easy class Solution ...

随机推荐

  1. 数据库索引(BTree索引和Hash索引)

    索引 索引是为了方便查找我们所需要的数据. mysql支持的索引数据类型 B-Tree索引的特点 B-Tree索引以B+Tree(树)的结构存储数据. B-Tree索引能够加快数据的查询速度: B-T ...

  2. Quartz.Net 任务调度之传递参数(2)

    1.jobDetail //添加 //Key:Value jobDetail.JobDataMap.Add("张翼德", "张翼德"); jobDetail.J ...

  3. CSP-S2019退役记。。。

    模拟赛的时候题目就比较迷,感觉不像联赛难度的. 考完正式赛才觉得这TM算个P. Day1: 写密码的监考同学的蜜汁字体让我傻了. 0和O是一样的,9和q是一样的,1和l是一样的-- 又没有冷静下来发现 ...

  4. java web项目的https配置

    1.进入到jdk下的bin目录 keytool -v -genkey -alias tomcat -keyalg RSA -keystore d:/tomcat.keystore -validity ...

  5. SQL Server数据库备份&还原

    一.备份 1.登录数据库 2.找到要还原的数据库 右键-任务-备份-添加(路径只写一个,刚开始二个总是报错)-确定 二.还原数据库 这个之间报错了二次 1.报错1:备份集中的数据库与现有数据库“XXX ...

  6. Least Common Ancestors

    /* Least Common Ancestors * Au: Small_Ash */ #include <bits/stdc++.h> using namespace std; con ...

  7. postgresql的规则系统

    " class="wiz-editor-body wiz-readonly" contenteditable="false"> Postgres ...

  8. 前端工程师技能图谱skill-map

    # 前端工程师技能图谱 ## 浏览器 - IE6/7/8/9/10/11 (Trident) - Firefox (Gecko) - Chrome/Chromium (Blink) - Safari ...

  9. leetcode上一些常见的链表问题

    92-按规定区间反转链表 思路:可以考虑成一种把前后数字的结点断开重新组合的问题 /** * Definition for singly-linked list. * struct ListNode ...

  10. MySQL 时间戳与日期格式的相互转换(转)

    1.UNIX时间戳转换为日期用函数: FROM_UNIXTIME() select FROM_UNIXTIME(1156219870); 输出:2006-08-22 12:11:10 2.日期转换为U ...