[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 ...
随机推荐
- CF438 The Child and Sequence
题意: 给定一个长度为n的非负整数序列a,你需要支持以下操作:1)给定l,r,输出a[l] + a[l+1] + ... + a[r] 2)给定l,r,x, 将a[l].a[l+1]......a[r ...
- 图形管线之旅 Part6
原文:<A trip through the Graphics Pipeline 2011> 翻译:往昔之剑 转载请注明出处 欢迎回来.这次我们去看看三角形的光栅化.但在光栅化三角 ...
- 磁盘镜像分析工具Autopsy
磁盘镜像分析工具Autopsy Autopsy是Kali Linux预安装的一款磁盘镜像分析工具.该工具可以对磁盘镜像的卷和文件系统进行分析,支持Unix和Windows系统.Autopsy是一个 ...
- 我的OI生涯 第六章
开学了,但是我们并没有像一个正常的高二学生一样坐在教室里接受调研考试的洗礼. 暑假作业这种东西早已被甩在一旁,可以想象回去补文化课时该有多么狼狈. 大王给我们制定了周密的计划,每周两次测试,加上蔡老师 ...
- 【贪心大水题】BZOJ3410-[Usaco2009 Dec]Selfish Grazing 自私的食草者
[题目大意] 给出n个区间,问最多选取多少个区间使得它们互相不重叠. [思路] 水题quq改善心情用.按照右端点大小排序,每次更新上一次的右端点,如果当前左端点大于上次右端点可取. #include& ...
- bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 -- 凸包
1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 Time Limit: 3 Sec Memory Limit: 64 MB Description 为了防止 ...
- 62.COUNT(递归算法)--数的划分变式题型
文件名:count.cpp 输入输出文件:count.in.count.out 时空:64M,2s 我们已经知道这样一个定理:任意一个正整数能够分解成最多4个数字的平方和.现在给你一些数字,要你求出它 ...
- Codeforces Round #262 (Div. 2) E. Roland and Rose 暴力
E. Roland and Rose Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/pro ...
- SqlServer项目经验:介质集有2个介质簇,但只提供了1个。必须提供所有成员
在对数据库备份与还原的过程中,我遇到一个问题“介质集有2个介质簇,但只提供了1个.必须提供所有成员”,下面详细的介绍一下遇到问题的经过与问题解决的方法! 一.备份与还原遇到的问题描述与解决方法: 前两 ...
- Oracle密码忘记了解决办法
Oracle密码忘记了怎么办?有时候我们可能忘记了一个用户的密码,但是又需要以这个用户做一些操作,又不能去修改掉这个用户的密码,这个时候,就可以利用一些小窍门,来完成操作.采用如下方法可以修改密码: ...