[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).

Given two integers stepssteps and arrLenarrLen, return the number of ways such that your pointer still at index 00 after exactly stepssteps steps.

Since the answer may be too large, return it modulo 10^9 + 710?9??+7.

Example

Example 1:

Input:
3
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
public class Solution {
    /**
     * @param steps: steps you can move
     * @param arrLen: the length of the array
     * @return: Number of Ways to Stay in the Same Place After Some Steps
     */
    public int numWays(int steps, int arrLen) {
        // write your code here
        return helper(steps, arrLen, 0);
    }

    private int helper(int steps, int arrLen, int curPos) {
        if (curPos < 0 || curPos >= arrLen) {
            return 0;
        }

        if (steps == 0) {
            if (curPos == 0) {
                return 1;
            }
            return 0;
        }

        int lStep = helper(steps - 1, arrLen, curPos - 1);
        int rStep = helper(steps - 1, arrLen, curPos + 1);
        int sStep = helper(steps - 1, arrLen, curPos);
        return lStep + rStep + sStep;
    }
}

原文地址:https://www.cnblogs.com/xuanlu/p/12445988.html

时间: 2024-11-06 14:36:19

[LintCode] 1835. Number of Ways to Stay in the Same Place After Some Steps I的相关文章

【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

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

Codeforces Round #266 (Div. 2)C. Number of Ways

传送门 Description You've got array a[1], a[2], ..., a[n], consisting of n integers. Count the number of ways to split all the elements of the array into three contiguous parts so that the sum of elements in each part is the same. More formally, you nee

[GeeksForGeeks] Count Number of ways to reach a given score in a game

Consider a game where a player can score 3 or 5 or 10 points in a move. Given a total score n, find the number of ways to reach the given score. Example: Input n = 20 Output: 4 There are the following 4 ways to reach 20. (10, 10) (5, 5, 10) (5, 5, 5,

[Coding Made Simple] Coin Changes Number of ways to get a total

Given coins of certain denominations and a total, how many ways these coins can be combined to get the total. Dynamic Programming solution State: T[i][j]: given the first i coins, the total number of ways these coins can be combined to get the total

Number of Ways

Description You've got array a[1], a[2], ..., a[n], consisting of n integers. Count the number of ways to split all the elements of the array into three contiguous parts so that the sum of elements in each part is the same. More formally, you need to

CF 466C Number of Ways(数学 / 思维 / DP)

题目链接:http://codeforces.com/problemset/problem/466/C 题目: You've got array a[1], a[2], ..., a[n], consisting of n integers. Count the number of ways to split all the elements of the array into three contiguous parts so that the sum of elements in each

[CodeForces 466C] Number of Ways

Given an array that has n integers, count the number of ways to split all elements of this array into 3 contiguous parts so that the sum of each part is the same. Each part must not be empty. Algorithm: O(N) runtime 1. If total sum % 3 != 0, return 0

Lintcode: Majority Number II 解题报告

Majority Number II 原题链接: http://lintcode.com/en/problem/majority-number-ii/# Given an array of integers, the majority number is the number that occurs more than 1/3 of the size of the array. Find it. Note There is only one majority number in the arra