We are given S, a length n string of characters from the set {'D', 'I'}. (These letters stand for "decreasing" and "increasing".)

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', then P[i] > P[i+1], and;
  • If S[i] == 'I', then P[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. 1 <= S.length <= 200
  2. S consists 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的更多相关文章

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

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

  3. 动态规划——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 ...

  4. leetcode903 Valid Permutations for DI Sequence

    思路: dp[i][j]表示到第i + 1个位置为止,并且以剩下的所有数字中第j + 1小的数字为结尾所有的合法序列数. 实现: class Solution { public: int numPer ...

  5. [Algo] 66. All Valid Permutations Of Parentheses I

    Given N pairs of parentheses “()”, return a list with all the valid permutations. Assumptions N > ...

  6. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  7. leetcode hard

    # Title Solution Acceptance Difficulty Frequency     4 Median of Two Sorted Arrays       27.2% Hard ...

  8. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  9. 【LeetCode】分治法 divide and conquer (共17题)

    链接:https://leetcode.com/tag/divide-and-conquer/ [4]Median of Two Sorted Arrays [23]Merge k Sorted Li ...

随机推荐

  1. JAVA用email.jar发送邮件

    1 jar包 email.jar包,网上下载 2 源代码 package zjr.amy.emil.test; import java.util.Date; import java.util.Prop ...

  2. EF CodeFirst简单实例

    运行环境:VS2012,添加的EntityFramework为6.0.2 版本不用太关心,只要知道原理就行了: 基本代码就这几行: namespace ConsoleApplication1 {    ...

  3. 2018/4/26 python文件处理方式

    目录 2018/4/26总结 1. 如果有一天群内问题总结: 问题:python2和python3的默认编码是什么? 2. python学习总结: python的处理文件方式 2018/4/26总结 ...

  4. NormalMap & CubeMap

    [NormalMap & CubeMap] 有时候我们需要CubeMap的环境反射也需要有凹凸信息,此时需要装将NormalMap与CubeMap结合. 因为要使用NormalMap,使用Pr ...

  5. 在SharePoint解决方案中使用JavaScript (1) – 引用.js文件

    本文是系列文章的第一篇. 在SharePoint解决方案中使用JavaScript (0) 作为在SharePoint应用程序中使用JavaScript的第一步,就是要知道如何将一个写好的.js文件, ...

  6. react-native init安装指定版本的react-native

    C:\Users\ZHONGZHENHUA\imooc_gp\index.js index.js /** @format */ import React,{ Component } from 'rea ...

  7. MonoBehaviour.print和Debug.Log是同样的作用

    MonoBehaviour.print("identical------------------------");

  8. IDEA 提示找不到 javax 等 tomcat 的相关包

    网上很多方法都告诉你,把 javax 的 libs 拷贝到项目下吧,简直简单粗暴.其实有更好的办法. 1.首先进入 Run 其中的 Run/Debug Configurations,在 Server ...

  9. SQL SERVER性能优化综述

    SQL SERVER性能优化综述 一个系统的性能的提高,不单单是试运行或者维护阶段的性能调优的任务,也不单单是开发阶段的事情,而是在整个软件生命周期都需要注意,进行有效工作才能达到的.所以我希望按照软 ...

  10. Java TreeMap的排序

    TreeMap 和 HashMap 用法大致相同,但实际需求中,我们需要把一些数据进行排序:以前在项目中,从数据库查询出来的数据放在List中,顺序都还是对的,但放在HashMap中,顺序就完全乱了. ...