903. Valid Permutations for DI Sequence
We are given S, a length n string of characters from the set {'D', 'I'}. (These letters stand for "decreasing" and "increasing".)
A valid permutation is a permutation P[0], P[1], ..., P[n] of integers {0, 1, ..., n}, such that for all i:
- If
S[i] == 'D', thenP[i] > P[i+1], and; - If
S[i] == 'I', thenP[i] < P[i+1].
How many valid permutations are there? Since the answer may be large, return your answer modulo 10^9 + 7.
Example 1:
Input: "DID"
Output: 5
Explanation:
The 5 valid permutations of (0, 1, 2, 3) are:
(1, 0, 3, 2)
(2, 0, 3, 1)
(2, 1, 3, 0)
(3, 0, 2, 1)
(3, 1, 2, 0)
Note:
1 <= S.length <= 200Sconsists only of characters from the set{'D', 'I'}.
Approach #1: DP.[C++]
class Solution {
public:
int numPermsDISequence(string S) {
int n = S.length(), mod = 1e9 + 7;
vector<vector<int>> dp(n+1, vector<int>(n+1));
for (int j = 0; j <= n; ++j) dp[0][j] = 1;
for (int i = 0; i < n; ++i) {
if (S[i] == 'I') {
for (int j = 0, cur = 0; j < n - i; ++j)
dp[i+1][j] = cur = (cur + dp[i][j]) % mod;
} else {
for (int j = n - i - 1, cur = 0; j >= 0; --j)
dp[i+1][j] = cur = (cur + dp[i][j+1]) % mod;
}
}
return dp[n][0];
}
};
Analysis:
I feel this code is right, but I can't express why.
https://leetcode.com/problems/valid-permutations-for-di-sequence/discuss/168278/C%2B%2BJavaPython-DP-Solution-O(N2)
903. Valid Permutations for DI Sequence的更多相关文章
- [LeetCode] 903. Valid Permutations for DI Sequence DI序列的有效排列
We are given S, a length n string of characters from the set {'D', 'I'}. (These letters stand for &q ...
- [Swift]LeetCode903. DI 序列的有效排列 | Valid Permutations for DI Sequence
We are given S, a length n string of characters from the set {'D', 'I'}. (These letters stand for &q ...
- 动态规划——Valid Permutations for DI Sequence
We are given S, a length n string of characters from the set {'D', 'I'}. (These letters stand for &q ...
- leetcode903 Valid Permutations for DI Sequence
思路: dp[i][j]表示到第i + 1个位置为止,并且以剩下的所有数字中第j + 1小的数字为结尾所有的合法序列数. 实现: class Solution { public: int numPer ...
- [Algo] 66. All Valid Permutations Of Parentheses I
Given N pairs of parentheses “()”, return a list with all the valid permutations. Assumptions N > ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- leetcode hard
# Title Solution Acceptance Difficulty Frequency 4 Median of Two Sorted Arrays 27.2% Hard ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- 【LeetCode】分治法 divide and conquer (共17题)
链接:https://leetcode.com/tag/divide-and-conquer/ [4]Median of Two Sorted Arrays [23]Merge k Sorted Li ...
随机推荐
- visjs使用小记-3.简单网络拓扑图的折叠与展开
项目需要对节点无限层级查看,大概捣鼓了下,以下demo代码可根据节点的层级顺序,通过节点双击简单实现节点的折叠与展开 <!doctype html> <html> <he ...
- Lenovo SplitScreen联想分屏软件只能在联想电脑运行,如何破解
1.正常安装软件,重启电脑. 2.打开安装目录 C:\Program Files\Lenovo\Lenovo SplitScreen\SplitScreen 找到 MachineChecker.dll ...
- 关于setTimeout()你所不知道的地方
前言:看了这篇文章,1.注意setTimeout引用的是全部变量还是局部变量了,当直接调用外部函数方法时,实际上函数内部的变量已经变成全 局.2.提醒我防止出错的,用匿名函数不容易出错.3.setTi ...
- 结对作业——四则运算 Part2. 封装与对接相关问题
结对作业——四则运算 Part2. 封装与对接相关问题 PB15061303 刘梓轩PB16061489 艾寅中 GITHUB 地址 戳这里 目录 Part 1. Core代码编写部分Part 2. ...
- FastDFS 分布式文件系统
1 学习目标 了解项目中使用FastDFS的原因和意义. 掌握FastDFS的架构组成部分,能说出tracker和storage的作用. 了解FastDFS+nginx上传和下载的执行流程 ...
- cdoj913-握手 【Havel定理】
http://acm.uestc.edu.cn/#/problem/show/913 握手 Time Limit: 2000/1000MS (Java/Others) Memory Limit ...
- 104. Maximum Depth of Binary Tree (Tree; DFS)
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- 113. Path Sum II (Tree; DFS)
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- Leetcode catalogue
1. Array & List 1.1Sort Array的变更操作,好好运用尾指针:88题的end,75题的blueHead 88. Merge Sorted Array (Array) 7 ...
- Python实现目录文件的全量和增量备份
目标: 1.传入3个参数:源文件路径,目标文件路径,md5文件 2.每周一实现全量备份,其余时间增量备份 1.通过传入的路径,获取该路径下面的所有目录和文件(递归) 方法一:使用os.listdir ...