var fib = function(n) {
if(n<=1) {
return n
}
let result = []
result[0]=0
result[1]=1
for(let i = 2;i<=n;i++) {
result[i] = result[i-1] +result[i-2]
}
return result[n]
};
零钱兑换
dp或者完全背包
var coinChange = function(coins, amount) {
if(amount<0) return -1
if(amount===0) return 0
let result = []
for(let idx = 1;idx<=amount;idx++) {
result[idx] = idx+1
}
const dp = (amount)=> {
if(amount<0) return -1
if(amount===0) return 0
if(result[amount]!==amount+1) {
return result[amount]
}
let res = amount+1
for(let coin of coins) {
if(dp(amount-coin)===-1) {
continue
}
res = Math.min(dp(amount-coin)+1,res)
}
result[amount]=res===amount+1?-1:res
return result[amount]
}
return dp(amount)
};
var coinChange = function(coins, amount) {
const dp = new Array(amount+1).fill(10001)
dp[0] = 0
for(let i = 0;i<coins.length;i++) {
for(let j = coins[i];j<=amount;j++) {
dp[j] = Math.min(dp[j-coins[i]]+1,dp[j])
}
}
return dp[amount]===10001?-1:dp[amount]
};
下降路径最小和
二维数组初始化,理清楚转移函数,准备初始值和存储table,注意边界,就可以写出答案了
var minFallingPathSum = function(matrix) {
// const ress = []
// for(let i = 0;i<matrix.length;i++) {
// ress[i]=[]
// for(let j = 0;j<matrix.length;j++) {
// ress[i][j] = 10001
// }
// }
const ress = Array.from(new Array(matrix.length),()=>new Array(matrix.length).fill(10001))
const dp = (i,j)=> {
if(i<0||j<0||i>=matrix[0].length||j>=matrix[0].length) {
return 10001
}
if(ress[i][j]!==10001) return ress[i][j]
if(i===0) {
ress[i][j] = matrix[i][j]
return matrix[i][j]
}
let res = Math.min(dp(i-1,j-1),dp(i-1,j),dp(i-1,j+1)) + matrix[i][j]
ress[i][j] = res
return res
}
let res = 10001
for(let i = 0;i<matrix[0].length;i++) {
res = Math.min(res,dp(matrix[0].length-1,i))
}
return res
};
不同路径
l两种写法,迭代和递归
var uniquePaths = function(m, n) {
if(m<=1&&n<=1) return 1
const res = Array.from(new Array(m),()=>new Array(n).fill(-1))
const dp = (i,j)=> {
if(i<0 ||j<0 ||i>=m ||j>=n) {
return 0
}
if((i===0 && j===1)||(i === 1 && j ===0) ) {
res[i][j]=1
return 1
}
if(res[i][j] !== -1 ) {
return res[i][j]
}
res[i][j] = dp(i-1,j)+dp(i,j-1)
return res[i][j]
}
return dp(m-1,n-1)
};
var uniquePaths = function(m, n) {
const dpMat = new Array(m);
for (let i = 0; i < dpMat.length; i++) {
dpMat[i] = new Array(n);
}
for (let i = 0; i < m; i++) {
dpMat[i][0] = 1;
}
for (let i = 0; i < n; i++) {
dpMat[0][i] = 1;
}
for (let i = 1; i < m; i++) {
for (let j = 1; j < n; j++) {
dpMat[i][j] = dpMat[i-1][j] + dpMat[i][j-1];
}
}
return dpMat[m-1][n-1];
};
粉刷房子
递归写法vs迭代写法
var minCost = function(costs) {
const num = [0,1,2]
const res = Array.from(new Array(costs.length),()=>new Array(3).fill(21))
const find = (i,j)=> {
if(i===costs.length-1) {
res[i][j] = costs[i][j]
return costs[i][j]
}
if(res[i][j]!==21) return res[i][j]
res[i][j] =costs[i][j] + Math.min(find(i+1,num.filter((idx)=>idx!==j)[0]),find(i+1,num.filter((idx)=>idx!==j)[1]))
return res[i][j]
}
return Math.min(find(0,0),find(0,1),find(0,2))
};
var minCost = function(costs) {
let n = costs.length, f = new Array(n).fill(0).map(()=>new Array(3).fill(0))
f[0][0] = costs[0][0] //f[i][0]表示第i个房子粉刷红色,f[i][1]代表第i个房子粉刷绿色,f[i][2]表示第i个房子粉刷蓝色
f[0][1] = costs[0][1]
f[0][2] = costs[0][2]
for(let i=1;i<n;i++){
f[i][0] = Math.min(f[i-1][1]+costs[i][0],f[i-1][2]+costs[i][0])
f[i][1] = Math.min(f[i-1][0]+costs[i][1],f[i-1][2]+costs[i][1])
f[i][2] = Math.min(f[i-1][0]+costs[i][2],f[i-1][1]+costs[i][2])
}
return Math.min(f[n-1][0],f[n-1][1],f[n-1][2])
};
把数字翻译成字符串
关键才是转移函数
var translateNum = function(num) {
const str = String(num);
const len = str.length;
const dfs = function(index){
if(index <= 0) return 1;
if((str.charAt(index)<"6"&&str.charAt(index-1)==="2")||str.charAt(index-1)==="1") return dfs(index-1)+dfs(index-2);
return dfs(index-1);
}
return dfs(len-1);
};