[Algorithm -- Dynamic programming] How Many Ways to Decode This Message?
For example we have
'a' -> 1
'b' -> 2
..
'z' -> 26
By given "12", we can decode the string to give result "ab" or 'L', 2 ways to decode, your function should return 2 as an answer.
Now asking by given "1246", what should be the return number;
The thinking process is somehow like this:
by given "1" -> we got 'a'
by given "" -> we got ""
by given "12345" -> 'a' + decode('2345') or 'L' + decode('345'), therefore number of ways to decode "12345"is the same of decode(2345)+decode(345).
Somehow we can see that this is a recursion task, therefore we can use Dynamice Programming + memo way to solve the problem.
const data = "";
function num_ways(data) {
// k : count from last to beginning
function helper(data, k, memo) {
if (k === ) {
// if k equals 0, mean only one single digital number left
// means there must be one char
return ;
}
if (data === "") {
// if data equals empty, then return 1
return ;
}
if (memo[k] != null) {
return memo[k];
}
const start = data.length - k;
if (data[start] === "") {
// if sth start as 0, then no char
return ;
}
let result = helper(data, k - , memo);
if (k >= && parseInt(data.slice(start, start + ), ) <= ) {
result += helper(data, k - , memo);
}
memo[k] = result;
return result;
}
let memo = [];
return helper(data, data.length, memo);
}
const res = num_ways(data);
console.log(res); //
[Algorithm -- Dynamic programming] How Many Ways to Decode This Message?的更多相关文章
- [Algorithm -- Dynamic Programming] Recursive Staircase Problem
For example there is a staricase N = 3 | ---| |---| | |---| | ---| ...
- Algorithm: dynamic programming
1. Longest Increasing Subsequence (LIS) problem unsorted array, calculate out the maximum length of ...
- [Algorithm] Dynamic programming: Find Sets Of Numbers That Add Up To 16
For a given array, we try to find set of pair which sums up as the given target number. For example, ...
- leetcode@ [91] Decode Ways (Dynamic Programming)
https://leetcode.com/problems/decode-ways/ A message containing letters from A-Z is being encoded to ...
- 以计算斐波那契数列为例说说动态规划算法(Dynamic Programming Algorithm Overlapping subproblems Optimal substructure Memoization Tabulation)
动态规划(Dynamic Programming)是求解决策过程(decision process)最优化的数学方法.它的名字和动态没有关系,是Richard Bellman为了唬人而取的. 动态规划 ...
- Dynamic Programming: From novice to advanced
作者:Dumitru 出处:http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=dynProg An impo ...
- [Optimization] Dynamic programming
“就是迭代,被众人说得这么玄乎" “之所以归为优化,是因为动态规划本质是一个systemetic bruce force" “因为systemetic,所以比穷举好了许多,就认为是 ...
- About Dynamic Programming
Main Point: Dynamic Programming = Divide + Remember + Guess 1. Divide the key is to find the subprob ...
- Dynamic Programming
We began our study of algorithmic techniques with greedy algorithms, which in some sense form the mo ...
随机推荐
- 微信小程序开发之路之组件化
类似于页面,自定义组件拥有自己的 wxml 模版和 wxss 样式. 官方链接 组件化,反过来理解,写重复的页面,方法,写第二遍就烦了,抽取出来就是组件化,可以理解为公用的方法 对于通用的数据,最先想 ...
- linux——(5)文件与文件系统的压缩与打包
概念一:常见的压缩文件拓展名 .z compress程序压缩的文件. .gz gzip程序压缩的文件. .bz2 bzip2程序压缩的文件. .tar tar程序打包的数据,并没有压缩过. .tar. ...
- centos7 更改时区
Linux 系统(我特指发行版, 没说内核) 下大部分软件的风格就是不会仔细去考虑向后 的兼容性, 比如你上个版本能用这种程序配置, 没准到了下一个版本, 该程序已经不见了. 比如 sysvinit ...
- vmware12安装centos7系统详解
1.首先需要准备的工具有vmware12和contos7的系统. vmvare12下载地址: http://pan.baidu.com/s/1i5vH50D contos7我自己使用的为1511版本. ...
- HDU 3339 In Action【最短路+01背包】
题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=3339] In Action Time Limit: 2000/1000 MS (Java/Other ...
- 【BZOJ 3812】 3812: 主旋律 (容斥原理**)
3812: 主旋律 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 235 Solved: 196 Description 响应主旋律的号召,大家决定 ...
- 「ZJOI2018」历史
「ZJOI2018」历史 前置知识 \(\text{LCT}\) 维护子树信息,考虑辅助树上一个节点的子树信息只是其代表的这一段链的信息,设 \(S(u)\) 为节点 \(u\) 的子树信息,那么在辅 ...
- [BZOJ4444][SCOI2015]国旗计划(倍增)
链上是经典贪心问题,将线段全按左端点排序后把点全撒在线段右端点上.这里放到环上,倍长即可. 题目保证不存在区间包含情况,于是有一种暴力做法,先将战士的管辖区间按左端点从小到大排序,对于询问x,从x战士 ...
- linux常见命令集合(下)
1. tar zcvf backup-$(date "+%Y-%m-%d").tar.gz demo01dir 常用命令集合 echo helloworld date “+%y-% ...
- Codeforces Round #346 (Div. 2) C. Tanya and Toys 贪心
C. Tanya and Toys 题目连接: http://www.codeforces.com/contest/659/problem/C Description In Berland recen ...