feat(chapter_dynamic_programming): Add js and ts codes for chapter 14.1 (#658)
* feat(chapter_dynamic_programming): Add js and ts codes for chapter 14.1 * style(chapter_dynamic_programming): format code * refactor(chapter_dynamic_programming): remove main definition and add typepull/593/head^2
parent
13bb76c283
commit
35e9c8c209
@ -0,0 +1,35 @@
|
||||
/**
|
||||
* File: climbing_stairs_backtrack.js
|
||||
* Created Time: 2023-07-26
|
||||
* Author: yuan0221 (yl1452491917@gmail.com)
|
||||
*/
|
||||
|
||||
/* 回溯 */
|
||||
function backtrack(choices, state, n, res) {
|
||||
// 当爬到第 n 阶时,方案数量加 1
|
||||
if (state === n) res.set(0, res.get(0) + 1);
|
||||
// 遍历所有选择
|
||||
for (choice of choices) {
|
||||
// 剪枝:不允许越过第 n 阶
|
||||
if (state + choice > n) break;
|
||||
// 尝试:做出选择,更新状态
|
||||
backtrack(choices, state + choice, n, res);
|
||||
// 回退
|
||||
}
|
||||
}
|
||||
|
||||
/* 爬楼梯:回溯 */
|
||||
function climbingStairsBacktrack(n) {
|
||||
const choices = [1, 2]; // 可选择向上爬 1 或 2 阶
|
||||
const state = 0; // 从第 0 阶开始爬
|
||||
const res = new Map();
|
||||
res.set(0, 0); // 使用 res[0] 记录方案数量
|
||||
backtrack(choices, state, n, res);
|
||||
return res.get(0);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
const n = 9;
|
||||
const res = climbingStairsBacktrack(n);
|
||||
console.log(`爬 ${n} 阶楼梯共有 ${res} 种方案`);
|
||||
|
@ -0,0 +1,24 @@
|
||||
/**
|
||||
* File: climbing_stairs_dfs.js
|
||||
* Created Time: 2023-07-26
|
||||
* Author: yuan0221 (yl1452491917@gmail.com)
|
||||
*/
|
||||
|
||||
/* 搜索 */
|
||||
function dfs(i) {
|
||||
// 已知 dp[1] 和 dp[2] ,返回之
|
||||
if (i == 1 || i == 2) return i;
|
||||
// dp[i] = dp[i-1] + dp[i-2]
|
||||
const count = dfs(i - 1) + dfs(i - 2);
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 爬楼梯:搜索 */
|
||||
function climbingStairsDFS(n) {
|
||||
return dfs(n);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
const n = 9;
|
||||
const res = climbingStairsDFS(n);
|
||||
console.log(`爬 ${n} 阶楼梯共有 ${res} 种方案`);
|
@ -0,0 +1,30 @@
|
||||
/**
|
||||
* File: climbing_stairs_dfs_mem.js
|
||||
* Created Time: 2023-07-26
|
||||
* Author: yuan0221 (yl1452491917@gmail.com)
|
||||
*/
|
||||
|
||||
/* 记忆化搜索 */
|
||||
function dfs(i, mem) {
|
||||
// 已知 dp[1] 和 dp[2] ,返回之
|
||||
if (i == 1 || i == 2) return i;
|
||||
// 若存在记录 dp[i] ,则直接返回之
|
||||
if (mem[i] != -1) return mem[i];
|
||||
// dp[i] = dp[i-1] + dp[i-2]
|
||||
const count = dfs(i - 1, mem) + dfs(i - 2, mem);
|
||||
// 记录 dp[i]
|
||||
mem[i] = count;
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 爬楼梯:记忆化搜索 */
|
||||
function climbingStairsDFSMem(n) {
|
||||
// mem[i] 记录爬到第 i 阶的方案总数,-1 代表无记录
|
||||
const mem = new Array(n + 1).fill(-1);
|
||||
return dfs(n, mem);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
const n = 9;
|
||||
const res = climbingStairsDFSMem(n);
|
||||
console.log(`爬 ${n} 阶楼梯共有 ${res} 种方案`);
|
@ -0,0 +1,41 @@
|
||||
/**
|
||||
* File: climbing_stairs_dp.js
|
||||
* Created Time: 2023-07-26
|
||||
* Author: yuan0221 (yl1452491917@gmail.com)
|
||||
*/
|
||||
|
||||
/* 爬楼梯:动态规划 */
|
||||
function climbingStairsDP(n) {
|
||||
if (n == 1 || n == 2) return n;
|
||||
// 初始化 dp 表,用于存储子问题的解
|
||||
const dp = new Array(n + 1).fill(-1);
|
||||
// 初始状态:预设最小子问题的解
|
||||
dp[1] = 1;
|
||||
dp[2] = 2;
|
||||
// 状态转移:从较小子问题逐步求解较大子问题
|
||||
for (let i = 3; i <= n; i++) {
|
||||
dp[i] = dp[i - 1] + dp[i - 2];
|
||||
}
|
||||
return dp[n];
|
||||
}
|
||||
|
||||
/* 爬楼梯:状态压缩后的动态规划 */
|
||||
function climbingStairsDPComp(n) {
|
||||
if (n == 1 || n == 2) return n;
|
||||
let a = 1,
|
||||
b = 2;
|
||||
for (let i = 3; i <= n; i++) {
|
||||
const tmp = b;
|
||||
b = a + b;
|
||||
a = tmp;
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
const n = 9;
|
||||
let res = climbingStairsDP(n);
|
||||
console.log(`爬 ${n} 阶楼梯共有 ${res} 种方案`);
|
||||
res = climbingStairsDPComp(n);
|
||||
console.log(`爬 ${n} 阶楼梯共有 ${res} 种方案`);
|
||||
|
@ -0,0 +1,41 @@
|
||||
/**
|
||||
* File: climbing_stairs_backtrack.ts
|
||||
* Created Time: 2023-07-26
|
||||
* Author: yuan0221 (yl1452491917@gmail.com)
|
||||
*/
|
||||
|
||||
/* 回溯 */
|
||||
function backtrack(
|
||||
choices: number[],
|
||||
state: number,
|
||||
n: number,
|
||||
res: Map<0, any>
|
||||
): void {
|
||||
// 当爬到第 n 阶时,方案数量加 1
|
||||
if (state === n) res.set(0, res.get(0) + 1);
|
||||
// 遍历所有选择
|
||||
for (let choice of choices) {
|
||||
// 剪枝:不允许越过第 n 阶
|
||||
if (state + choice > n) break;
|
||||
// 尝试:做出选择,更新状态
|
||||
backtrack(choices, state + choice, n, res);
|
||||
// 回退
|
||||
}
|
||||
}
|
||||
|
||||
/* 爬楼梯:回溯 */
|
||||
function climbingStairsBacktrack(n: number): number {
|
||||
const choices = [1, 2]; // 可选择向上爬 1 或 2 阶
|
||||
const state = 0; // 从第 0 阶开始爬
|
||||
const res = new Map();
|
||||
res.set(0, 0); // 使用 res[0] 记录方案数量
|
||||
backtrack(choices, state, n, res);
|
||||
return res.get(0);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
const n = 9;
|
||||
const res = climbingStairsBacktrack(n);
|
||||
console.log(`爬 ${n} 阶楼梯共有 ${res} 种方案`);
|
||||
|
||||
export {};
|
@ -0,0 +1,26 @@
|
||||
/**
|
||||
* File: climbing_stairs_dfs.ts
|
||||
* Created Time: 2023-07-26
|
||||
* Author: yuan0221 (yl1452491917@gmail.com)
|
||||
*/
|
||||
|
||||
/* 搜索 */
|
||||
function dfs(i: number): number {
|
||||
// 已知 dp[1] 和 dp[2] ,返回之
|
||||
if (i == 1 || i == 2) return i;
|
||||
// dp[i] = dp[i-1] + dp[i-2]
|
||||
const count = dfs(i - 1) + dfs(i - 2);
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 爬楼梯:搜索 */
|
||||
function climbingStairsDFS(n: number): number {
|
||||
return dfs(n);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
const n = 9;
|
||||
const res = climbingStairsDFS(n);
|
||||
console.log(`爬 ${n} 阶楼梯共有 ${res} 种方案`);
|
||||
|
||||
export {};
|
@ -0,0 +1,32 @@
|
||||
/**
|
||||
* File: climbing_stairs_dfs_mem.ts
|
||||
* Created Time: 2023-07-26
|
||||
* Author: yuan0221 (yl1452491917@gmail.com)
|
||||
*/
|
||||
|
||||
/* 记忆化搜索 */
|
||||
function dfs(i: number, mem: number[]): number {
|
||||
// 已知 dp[1] 和 dp[2] ,返回之
|
||||
if (i == 1 || i == 2) return i;
|
||||
// 若存在记录 dp[i] ,则直接返回之
|
||||
if (mem[i] != -1) return mem[i];
|
||||
// dp[i] = dp[i-1] + dp[i-2]
|
||||
const count = dfs(i - 1, mem) + dfs(i - 2, mem);
|
||||
// 记录 dp[i]
|
||||
mem[i] = count;
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 爬楼梯:记忆化搜索 */
|
||||
function climbingStairsDFSMem(n: number): number {
|
||||
// mem[i] 记录爬到第 i 阶的方案总数,-1 代表无记录
|
||||
const mem = new Array(n + 1).fill(-1);
|
||||
return dfs(n, mem);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
const n = 9;
|
||||
const res = climbingStairsDFSMem(n);
|
||||
console.log(`爬 ${n} 阶楼梯共有 ${res} 种方案`);
|
||||
|
||||
export {};
|
@ -0,0 +1,42 @@
|
||||
/**
|
||||
* File: climbing_stairs_dp.ts
|
||||
* Created Time: 2023-07-26
|
||||
* Author: yuan0221 (yl1452491917@gmail.com)
|
||||
*/
|
||||
|
||||
/* 爬楼梯:动态规划 */
|
||||
function climbingStairsDP(n: number): number {
|
||||
if (n == 1 || n == 2) return n;
|
||||
// 初始化 dp 表,用于存储子问题的解
|
||||
const dp = new Array(n + 1).fill(-1);
|
||||
// 初始状态:预设最小子问题的解
|
||||
dp[1] = 1;
|
||||
dp[2] = 2;
|
||||
// 状态转移:从较小子问题逐步求解较大子问题
|
||||
for (let i = 3; i <= n; i++) {
|
||||
dp[i] = dp[i - 1] + dp[i - 2];
|
||||
}
|
||||
return dp[n];
|
||||
}
|
||||
|
||||
/* 爬楼梯:状态压缩后的动态规划 */
|
||||
function climbingStairsDPComp(n: number): number {
|
||||
if (n == 1 || n == 2) return n;
|
||||
let a = 1,
|
||||
b = 2;
|
||||
for (let i = 3; i <= n; i++) {
|
||||
const tmp = b;
|
||||
b = a + b;
|
||||
a = tmp;
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
const n = 9;
|
||||
let res = climbingStairsDP(n);
|
||||
console.log(`爬 ${n} 阶楼梯共有 ${res} 种方案`);
|
||||
res = climbingStairsDPComp(n);
|
||||
console.log(`爬 ${n} 阶楼梯共有 ${res} 种方案`);
|
||||
|
||||
export {};
|
Loading…
Reference in new issue