本文涉及知识点
C++动态规划
Leetcode1269. 停在原地的方案数
有一个长度为 arrLen 的数组,开始有一个指针在索引 0 处。
每一步操作中,你可以将指针向左或向右移动 1 步,或者停在原地(指针不能被移动到数组范围外)。
给你两个整数 steps 和 arrLen ,请你计算并返回:在恰好执行 steps 次操作以后,指针仍然指向索引 0 处的方案数。
由于答案可能会很大,请返回方案数 模 109 + 7 后的结果。
示例 1:
输入:steps = 3, arrLen = 2
输出:4
解释:3 步后,总共有 4 种不同的方法可以停在索引 0 处。
向右,向左,不动
不动,向右,向左
向右,不动,向左
不动,不动,不动
示例 2:
输入:steps = 2, arrLen = 4
输出:2
解释:2 步后,总共有 2 种不同的方法可以停在索引 0 处。
向右,向左
不动,不动
示例 3:
输入:steps = 4, arrLen = 2
输出:8
提示:
1 <= steps <= 500
1 <= arrLen <= 106
动态规划
每次最多右移1,所以最多移到steps。
动态规划的状态表示
dp[i][j] 表示移动了i次,最终位置在j的方案。空间复杂度:O(steps steps)。
动态规划的填表顺序
枚举前置状态。
i = 0 to step-1 j = 0 to step
动态规划的转移方程
如果停留在原地:
dp[i+1][j] += dp[i][j]
如果左移:
dp[j+1][j-1] += dp[i][j];
如果右移:
dp[i+1][j+1] += dp[i][j];
动态规划的初始化
dp[0][0] 为1,其它全为0。
动态规划的返回值
dp.back()[0]。
代码
核心代码
template<int MOD = 1000000007>
class C1097Int
{
public:
C1097Int(long long llData = 0) :m_iData(llData% MOD)
{
}
C1097Int operator+(const C1097Int& o)const
{
return C1097Int(((long long)m_iData + o.m_iData) % MOD);
}
C1097Int& operator+=(const C1097Int& o)
{
m_iData = ((long long)m_iData + o.m_iData) % MOD;
return *this;
}
C1097Int& operator-=(const C1097Int& o)
{
m_iData = (m_iData + MOD - o.m_iData) % MOD;
return *this;
}
C1097Int operator-(const C1097Int& o)
{
return C1097Int((m_iData + MOD - o.m_iData) % MOD);
}
C1097Int operator*(const C1097Int& o)const
{
return((long long)m_iData * o.m_iData) % MOD;
}
C1097Int& operator*=(const C1097Int& o)
{
m_iData = ((long long)m_iData * o.m_iData) % MOD;
return *this;
}
C1097Int operator/(const C1097Int& o)const
{
return *this * o.PowNegative1();
}
C1097Int& operator/=(const C1097Int& o)
{
*this /= o.PowNegative1();
return *this;
}
bool operator==(const C1097Int& o)const
{
return m_iData == o.m_iData;
}
bool operator<(const C1097Int& o)const
{
return m_iData < o.m_iData;
}
C1097Int pow(long long n)const
{
C1097Int iRet = 1, iCur = *this;
while (n)
{
if (n & 1)
{
iRet *= iCur;
}
iCur *= iCur;
n >>= 1;
}
return iRet;
}
C1097Int PowNegative1()const
{
return pow(MOD - 2);
}
int ToInt()const
{
return m_iData;
}
private:
int m_iData = 0;;
};
class Solution {
public:
int numWays(int steps, int arrLen) {
const int iMaxPos = min(steps, arrLen);
vector<vector<C1097Int<>>> dp(steps + 1, vector<C1097Int<>>(iMaxPos));
dp[0][0] = 1;
for (int i = 0; i < steps; i++) {
for (int j = 0; j < iMaxPos; j++) {
dp[i + 1][j] += dp[i][j];
if(j+1 < iMaxPos)dp[i+1][j+1] += dp[i][j];
if( j -1 >= 0 )dp[i + 1][j - 1] += dp[i][j];
}
}
return dp.back()[0].ToInt();
}
};
单元测试
int steps, arrLen;
TEST_METHOD(TestMethod1)
{
steps = 1, arrLen = 2;
auto res = Solution().numWays(steps, arrLen);
AssertEx(1, res);
}
TEST_METHOD(TestMethod2)
{
steps = 2, arrLen = 2;
auto res = Solution().numWays(steps, arrLen);
AssertEx(2, res);
}
TEST_METHOD(TestMethod11)
{
steps = 3, arrLen = 2;
auto res = Solution().numWays(steps, arrLen);
AssertEx(4, res);
}
TEST_METHOD(TestMethod12)
{
steps = 2, arrLen = 4;
auto res = Solution().numWays(steps, arrLen);
AssertEx(2, res);
}
TEST_METHOD(TestMethod13)
{
steps = 4, arrLen = 2;
auto res = Solution().numWays(steps, arrLen);
AssertEx(8, res);
}