LeetCode 1269. Number of Ways to Stay in the Same Place After Some Steps

原题链接在这里:https://leetcode.com/problems/number-of-ways-to-stay-in-the-same-place-after-some-steps/

题目:

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

题解:

For each step, it could go left, right or stay.

Let dp[i] denotes number of ways to get to i.

next[i] = dp[i - 1] + dp[i] + dp[i + 1].

We only care about len = math.min(steps / 2 + 1, arrLen) since if it is beyond steps / 2 + 1, then it could not come back to 0.

To handle i == 0 and i == dp.length - 1 with i- 1 and i + 1, initialize dp as len + 2. Then first and last would always be 0.

Time Complexity: O(steps * len). len = Math.min(steps / 2 + 1, arrLen).

Sapce: O(len).

AC Java:

 1 class Solution {
 2     public int numWays(int steps, int arrLen) {
 3         int mod = 1000000007;
 4         int len = Math.min(steps / 2 + 1,  arrLen);
 5
 6         long [] dp = new long[len + 2];
 7         dp[1] = 1;
 8         while(steps-- > 0){
 9             long [] next = new long[len + 2];
10             for(int i = 1; i < dp.length - 1; i++){
11                 next[i] = (dp[i - 1] + dp[i] + dp[i + 1]) % mod;
12             }
13
14             dp = next;
15         }
16
17         return (int)dp[1];
18     }
19 }

类似Knight Dialer.

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12395346.html

时间: 2024-10-07 23:10:12

LeetCode 1269. Number of Ways to Stay in the Same Place After Some Steps的相关文章

【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 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

[LintCode] 1835. Number of Ways to Stay in the Same Place After Some Steps I

You have a pointer at index 00 in an array of size arrLenarrLen. At each step, you can move 11 position to the left, 11 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). Giv

leetcode第一刷_Decode Ways

这道题还挺难的.递归的思路是好想,不过不出意料的超时了. dp嘛.想一下i-1的编码加上第i个编码会怎样,如果加上的这个编码不是0,那么这一位可以独立解码,那长为i的解码个数至少是长为i-1的解码个数.还有呢?如果i-1位是1,可以把i-1位和i位同时解码出来,还有呢?如果i-1位是2而i位是0-6中的数字,也可以同时解码这两位编码.满足这个条件的时候,当前长度的解码个数还要加上i-2时的解码个数. 完全可以用一个数组存放过去的结果,但是很明显,当前的结论只与前一个和前前一个结果有关系.只要用三

[leetcode]Valid Number @ Python

原题地址:http://oj.leetcode.com/problems/valid-number/ 题意:判断输入的字符串是否是合法的数. 解题思路:这题只能用确定有穷状态自动机(DFA)来写会比较优雅.本文参考了http://blog.csdn.net/kenden23/article/details/18696083里面的内容,在此致谢! 首先这个题有9种状态: 0初始无输入或者只有space的状态1输入了数字之后的状态2前面无数字,只输入了dot的状态3输入了符号状态4前面有数字和有do

【Leetcode】91. Decode Ways

Yesterday, Bro Luo told me: "Why don't you improve your English by writing your blogs in English?" I think it may be a good way and i did so today. Problem Description: A message containing letters from A - Z  is being encoded to numbers using t

leetCode: Single Number II [137]

[题目] Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? [题意] 给定一个整数以外,其中除了一个整数只出现一次以外,其他

[leetcode]_Palindrome Number

判断integer是否为回文串(负数全部不为回文串) 思路很直接,提取出integer中的每一位,一头一尾进行比较是否相同. 一次AC , 直接上代码: public boolean isPalindrome(int x) { if(x < 0) return false; else if(x >= 0 && x <= 9) return true; else{ int[] num = new int[20]; int i = 0 ; while(x > 0){ n

【LeetCode】091. Decode Ways

题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example,Given enco

LeetCode: Single Number Ⅱ

1 /** 2 * 3 */ 4 package solution; 5 6 import java.util.Arrays; 7 8 /** 9 * @author whh 10 * 11 * Given an array of integers, every element appears three times except 12 * for one. Find that single one. 13 * 14 * Note: Your algorithm should have a li