var uniquePaths = function(m, n) {
let dp = new Array(m)
dp[0] = new Array(n)
dp[0].fill(1)
for(let i = 1; i < m; i++){
dp[i] = new Array(n)
for(let j = 0; j < n; j++){
if(j === 0) dp[i][0] = 1
else dp[i][j] = dp[i-1][j] + dp[i][j-1]
}
}
return dp[m-1][n-1]
};
var uniquePathsWithObstacles = function(obstacleGrid) {
let m = obstacleGrid.length
let n = obstacleGrid[0].length
let dp = new Array(m)
for(let i = 0; i < m; i++){
dp[i] = new Array(n)
if(i === 0) dp[0][0] = !obstacleGrid[0][0]
else{
if(obstacleGrid[i][0] === 1) dp[i][0] = 0
else dp[i][0] = dp[i-1][0]
}
}
for(let i = 1; i < n; i++){
if(obstacleGrid[0][i] === 1){
dp[0][i] = 0
}else{
dp[0][i] = dp[0][i-1]
}
}
console.log(dp)
for(let i = 1; i < m; i++){
for(let j = 1; j < n; j++){
if(obstacleGrid[i][j] === 1){
dp[i][j] = 0
}else{
dp[i][j] = dp[i-1][j] + dp[i][j-1]
}
}
}
return dp[m-1][n-1]
};
var numTrees = function(n) {
let dp = new Array(n+1)
dp[0] = 1
dp[1] = 1
for(let i = 2; i <= n; i++){
dp[i] = 0
for(let j = 0; j <= i - 1; j++){
dp[i] += dp[j]*dp[i-j-1]
}
}
return dp[n]
};
var integerBreak = function(n) {
let dp = new Array(n)
dp[2] = 1
for(let i = 3 ; i <= n; i++){
dp[i] = Math.max(1*(i-1), 1*dp[i-1])
for(let j = 2; j <= i -2; j++){
dp[i] = Math.max(dp[i], Math.max(j*(i-j), j*dp[i-j]))
}
}
return dp[n]
};
var canPartition = function(nums) {
let sum = 0
for(let i = 0; i < nums.length; i++){
sum += nums[i]
}
if(sum%2 !== 0) return false
let n = sum/2
let dp = new Array(n+1).fill(0)
dp[0] = 0
for(let i = 0; i < nums.length; i++){
for(let j = n; j >= nums[i]; j--){
dp[j] = Math.max(dp[j], dp[j - nums[i]] + nums[i])
}
}
if(dp[n] === n) return true
return false
};
var lastStoneWeightII = function(stones) {
let sum = 0
for(let i = 0; i < stones.length; i++){
sum += stones[i]
}
let a = Math.floor(sum/2)
let dp = new Array(a+1).fill(0)
for(let i = 0; i < stones.length; i++){
for(let j = a; j >= stones[i]; j--){
dp[j] = Math.max(dp[j], dp[j - stones[i]] + stones[i])
}
}
return (sum - dp[a]) - dp[a]
};
var findTargetSumWays = function(nums, target) {
let sum = 0
for(let i = 0; i < nums.length; i++){
sum += nums[i]
}
if((sum + target) % 2 !== 0 ) return false
if( sum < target) return false
let left = (sum + target) / 2
if(left < 0) return false
let dp = new Array(left + 1).fill(0)
dp[0] = 1
for(let i = 0; i < nums.length; i++){
for(let j = left; j >= nums[i]; j--){
dp[j] += dp[j - nums[i]]
}
}
return dp[left]
};
var findMaxForm = function(strs, m, n) {
let zeroArr = new Array(strs.length).fill(0)
let oneArr = new Array(strs.length).fill(0)
for(let i = 0; i < strs.length; i++){
let str = strs[i].split('')
str.forEach((char) => {
if(char == 0) zeroArr[i] ++
else if(char == 1) oneArr[i] ++
})
}
let dp = new Array()
for(let i = 0; i <= m; i++){
dp.push(new Array(n+1).fill(0))
}
for(let k = 0; k < strs.length; k++){
for(let i = m; i >= zeroArr[k]; i--){
for(let j = n; j >= oneArr[k]; j--){
dp[i][j] = Math.max(dp[i][j], dp[i-zeroArr[k]][j-oneArr[k]]+1)
}
}
}
return dp[m][n]
};
var change = function(amount, coins) {
let dp = new Array(amount + 1).fill(0)
dp[0] = 1
for(let i = 0; i < coins.length; i++){
for(let j = coins[i]; j <= amount; j++){
dp[j] += dp[j - coins[i]]
}
}
return dp[amount]
};
var combinationSum4 = function(nums, target) {
let dp = new Array(target + 1).fill(0)
dp[0] = 1
for(let i = 1; i <= target; i++){
for(let j = 0; j < nums.length; j++){
if(i >= nums[j]){
dp[i] += dp[i - nums[j]]
}
}
}
return dp[target]
};
var climbStairs = function(n) {
let dp = new Array(n+1).fill(0)
dp[0] = 1
for(let j = 1; j <= n ;j++){
for(let i = 1; i <= 2; i++){
if(j >= i){
dp[j] += dp[j - i]
}
}
}
return dp[n]
};
var coinChange = function(coins, amount) {
let dp = new Array(amount + 1).fill(Infinity)
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], dp[j - coins[i]] + 1)
}
}
return dp[amount] === Infinity ? -1 : dp[amount]
};
var numSquares = function(n) {
let dp = new Array(n + 1).fill(Infinity)
dp[0] = 0
let num = Math.sqrt(n)
if(num % 1 === 0) return 1
num = Math.floor(num)
for(let i = 1; i <= num; i++){
for(let j = i*i; j <= n; j++){
dp[j] = Math.min(dp[j], dp[j - i*i] + 1)
}
}
return dp[n]
};
var wordBreak = function(s, wordDict) {
let n = s.length
let dp = new Array( n + 1).fill(false)
dp[0] = true
let set = new Set(wordDict)
for(let i = 0; i <= n; i++){
for(let j = 0; j < i; j++){
let str = s.substr(j, i - j)
if(set.has(str) && dp[j]){
dp[i] = true
}
}
}
console.log(dp)
return dp[n]
};
var rob = function(nums) {
if(nums.length === 1) return nums[0]
let dp = new Array(nums.length)
dp[0] = nums[0]
dp[1] = Math.max(nums[0], nums[1])
for(let i = 2; i < nums.length; i++){
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i])
}
return dp[nums.length - 1]
};
var rob = function(nums) {
if(nums.length === 1) return nums[0]
function fn(start, end){
if(start === end) return nums[end]
let dp = new Array(nums.length - 1)
dp[0] = nums[start]
dp[1] = Math.max(nums[start], nums[start+1])
for(let i = 2; i <= dp.length - 1; i++){
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i + start])
}
console.log('dp:',dp)
return dp[dp.length - 1]
}
return Math.max(fn(0, nums.length - 2), fn(1, nums.length - 1))
};
var rob = function(root) {
function backtracking(node){
if(node === null) return [0, 0]
let left = backtracking(node.left)
let right = backtracking(node.right)
let val1 = Math.max(left[0], left[1]) + Math.max(right[0], right[1])
let val2 = node.val + left[0] + right[0]
return [val1,val2]
}
let dp = backtracking(root)
return Math.max(dp[0], dp[1])
};
贪心
var maxProfit = function(prices) {
let min = prices[0]
let max = 0
for(let i = 1; i < prices.length; i++){
max = Math.max(max, prices[i] - min)
min = Math.min(min, prices[i])
}
return max
};
动态规划
var maxProfit = function(prices) {
let dp = new Array(prices.length)
for(let i = 0; i < prices.length; i++){
dp[i] = [0,0]
}
dp[0][0] = -prices[0]
dp[0][1] = 0
for(let i = 1; i < prices.length; i++){
dp[i][0] = Math.max(dp[i-1][0], -prices[i])
dp[i][1] = Math.max(dp[i-1][1], dp[i-1][0] + prices[i])
}
return dp[dp.length - 1][1]
};
贪心
var maxProfit = function(prices) {
if(prices.length <= 1) return 0
let count = 0
for(let i = 1; i < prices.length; i++){
if(prices[i] - prices[i-1] > 0)
count += prices[i] - prices[i-1]
}
return count
};
动态规划
var maxProfit = function(prices) {
let dp = new Array(prices.length)
for(let i = 0; i < prices.length; i++){
dp[i] = [0,0]
}
dp[0][0] = -prices[0]
dp[0][1] = 0
for(let i = 1; i < prices.length; i++){
dp[i][0] = Math.max(dp[i-1][0], dp[i-1][1] - prices[i])
dp[i][1] = Math.max(dp[i-1][1], dp[i-1][0] + prices[i])
}
return dp[dp.length - 1][1]
};
var maxProfit = function(prices) {
let dp = new Array(prices.length)
for(let i = 0; i < prices.length; i++){
dp[i] = [0,0,0,0,0]
}
dp[0][1] = dp[0][3] = -prices[0]
for(let i = 1; i < prices.length; i++){
dp[i][0] = dp[i-1][0]
dp[i][1] = Math.max(dp[i-1][1], dp[i-1][0] - prices[i])
dp[i][2] = Math.max(dp[i-1][2], dp[i-1][1] + prices[i])
dp[i][3] = Math.max(dp[i-1][3], dp[i-1][2] - prices[i])
dp[i][4] = Math.max(dp[i-1][4], dp[i-1][3] + prices[i])
}
return dp[dp.length - 1][4]
};
var maxProfit = function(k, prices) {
if(k === 0) return 0
if(prices.length === 0) return 0
let dp = new Array(prices.length)
for(let i = 0; i < prices.length; i++){
dp[i] = new Array(2*k+1)
}
dp[0][0] = 0
for(let j = 1; j < 2*k + 1; j++){
if(j % 2 === 0){
dp[0][j] = 0
}else dp[0][j] = -prices[0]
}
for(let i = 1; i < prices.length; i++){
for(let j = 0; j < 2*k - 1; j += 2){
if(j === 0) dp[i][0] = dp[i-1][0]
dp[i][j+1] = Math.max(dp[i-1][j+1], dp[i-1][j] - prices[i])
dp[i][j+2] = Math.max(dp[i-1][j+2], dp[i-1][j+1] + prices[i])
}
}
return dp[prices.length-1][2*k]
};
var maxProfit = function(prices) {
if(prices.length === 1) return 0
let dp = new Array(prices.length)
for(let i = 0; i < prices.length; i++){
dp[i] = [0,0,0,0]
}
dp[0][0] = -prices[0]
for(let i = 1; i < prices.length; i++){
dp[i][0] = Math.max(dp[i-1][0],dp[i-1][1]-prices[i],dp[i-1][3]-prices[i])
dp[i][1] = Math.max(dp[i-1][1], dp[i-1][3])
dp[i][2] = dp[i-1][0] + prices[i]
dp[i][3] = dp[i-1][2]
}
return Math.max(dp[dp.length-1][1],dp[dp.length-1][2],dp[dp.length-1][3])
};
贪心
var maxProfit = function(prices, fee) {
let result = 0
let min = prices[0]
for(let i = 1; i < prices.length; i++){
if(prices[i] < min){
min = prices[i]
}
if(prices[i] >= min && prices[i] <= min + fee){
continue
}
if(prices[i] > min + fee){
result += prices[i] - min - fee
min = prices[i] - fee
}
}
return result
};
动态规划
var maxProfit = function(prices, fee) {
if(prices.length === 1) return 0
let dp = new Array(prices.length)
for(let i = 0; i < prices.length; i++){
dp[i] = [0, 0]
}
dp[0][0] = -prices[0]
for(let i = 1; i < prices.length; i++){
dp[i][0] = Math.max(dp[i-1][0], dp[i-1][1] - prices[i])
dp[i][1] = Math.max(dp[i-1][1], dp[i-1][0] + prices[i] - fee)
}
return dp[dp.length-1][1]
};
var lengthOfLIS = function(nums) {
let dp = new Array(nums.length).fill(1)
let result = 1
for(let i = 1; i < nums.length; i++){
for(let j = 0; j < i; j++){
if(nums[i] > nums[j])
dp[i] = Math.max(dp[i],dp[j] + 1)
}
result = Math.max(result,dp[i])
}
return result
};
var findLengthOfLCIS = function(nums) {
let dp = new Array(nums.length).fill(1)
let result = 1
for(let i = 1; i < nums.length; i++){
if(nums[i] > nums[i-1]){
dp[i] = dp[i-1] + 1
}
result = Math.max(result, dp[i])
}
return result
};
var findLength = function(nums1, nums2) {
let dp = new Array(nums1.length+1)
for(let i = 0; i <= nums1.length; i++){
dp[i] = new Array(nums2.length + 1).fill(0)
}
let ans = 0
for(let i = 1; i <= nums1.length; i++){
for(let j = 1; j <= nums2.length; j++){
if(nums1[i-1] == nums2[j-1]){
dp[i][j] = dp[i-1][j-1] + 1
}
ans = Math.max(ans, dp[i][j])
}
}
return ans
};
var longestCommonSubsequence = function(text1, text2) {
let dp = new Array(text1.length + 1)
for(let i = 0; i <= text1.length; i++){
dp[i] = new Array(text2.length + 1).fill(0)
}
let ans = 0
for(let i = 1; i <= text1.length; i++){
for(let j = 1; j <= text2.length; j++){
if(text1[i-1] === text2[j-1]){
dp[i][j] = dp[i-1][j-1] + 1
}else{
dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1])
}
ans = Math.max(ans, dp[i][j])
}
}
return ans
};
var maxUncrossedLines = function(nums1, nums2) {
let dp = new Array(nums1.length + 1)
for(let i = 0; i <= nums1.length; i++){
dp[i] = new Array(nums2.length+1).fill(0)
}
let ans = 0
for(let i = 1; i <= nums1.length; i++){
for(let j = 1; j <= nums2.length; j++){
if(nums1[i-1] == nums2[j-1]){
dp[i][j] = dp[i-1][j-1] + 1
}else{
dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1])
}
ans = Math.max(ans, dp[i][j])
}
}
return ans
};
贪心
var maxSubArray = function(nums) {
if(nums.length === 1) return nums[0]
let count = 0
let max = -10000
for(let i = 0; i < nums.length; i++){
count += nums[i]
max = Math.max(count, max)
if(count < 0){
count = 0
}
}
return max
};
动态规划
var maxSubArray = function(nums) {
let dp = new Array(nums.length)
dp[0] = nums[0]
let ans = nums[0]
for(let i = 1; i < nums.length; i++){
dp[i] = Math.max(dp[i-1] + nums[i], nums[i])
ans = Math.max(ans, dp[i])
}
return ans
};
var isSubsequence = function(s, t) {
let dp = new Array(s.length+1)
for(let i = 0; i <= s.length; i++){
dp[i] = new Array(t.length+1).fill(0)
}
for(let i = 1; i <= s.length; i++){
for(let j = 1; j <= t.length; j++){
if(s[i-1] == t[j-1]){
dp[i][j] = dp[i-1][j-1] + 1
}else{
dp[i][j] = dp[i][j-1]
}
}
}
return dp[s.length][t.length] == s.length
};
var numDistinct = function(s, t) {
let dp = new Array(s.length+1)
for(let i = 0; i <= s.length; i++){
dp[i] = new Array(t.length + 1).fill(0)
dp[i][0] = 1
}
for(let i = 1; i <= s.length; i++){
for(let j = 1; j <= t.length; j++){
if(s[i-1] == t[j-1]){
dp[i][j] = dp[i-1][j-1] + dp[i-1][j]
}else{
dp[i][j] = dp[i-1][j]
}
}
}
return dp[s.length][t.length]
};
var minDistance = function(word1, word2) {
let dp = new Array(word1.length + 1)
for(let i = 0; i <= word1.length; i++){
dp[i] = new Array(word2.length+1)
dp[i][0] = i
}
for(let j = 0; j <= word2.length; j++){
dp[0][j] = j
}
for(let i = 1; i <= word1.length; i++){
for(let j = 1; j <= word2.length; j++){
if(word1[i-1] == word2[j-1]){
dp[i][j] = dp[i-1][j-1]
}else{
dp[i][j] = Math.min(dp[i-1][j] + 1, dp[i][j-1] + 1, dp[i-1][j-1] + 2)
}
}
}
return dp[word1.length][word2.length]
};
var minDistance = function(word1, word2) {
let dp = new Array(word1.length+1)
for(let i = 0; i <= word1.length; i++){
dp[i] = new Array(word2.length+1)
dp[i][0] = i
}
for(let j = 0; j <= word2.length; j++){
dp[0][j] = j
}
for(let i = 1; i <= word1.length; i++){
for(let j = 1; j <= word2.length; j++){
if(word1[i-1] == word2[j-1]){
dp[i][j] = dp[i-1][j-1]
}else{
dp[i][j] = Math.min(dp[i-1][j] + 1, dp[i][j-1] + 1, dp[i-1][j-1] + 1)
}
}
}
return dp[word1.length][word2.length]
};
var countSubstrings = function(s) {
let dp = new Array(s.length)
for(let i = 0; i < s.length; i++){
dp[i] = new Array(s.length).fill(false)
}
let ans = 0
for(let i = s.length - 1 ; i >= 0; i --){
for( let j = i; j < s.length; j ++){
if(s[i] == s[j]){
if( (j - i) <= 1){
dp[i][j] = true
}else{
dp[i][j] = dp[i+1][j-1]
}
}
if(dp[i][j]) ans ++
}
}
return ans
};
var longestPalindromeSubseq = function(s) {
let dp = new Array(s.length)
for(let i = 0; i < s.length; i++){
dp[i] = new Array(s.length).fill(0)
dp[i][i] = 1
}
for(let i = s.length - 1; i >=0 ; i--){
for(let j = i+1; j < s.length; j++){
if(s[i] == s[j]){
dp[i][j] = dp[i+1][j-1] + 2
}else{
dp[i][j] = Math.max(dp[i][j-1], dp[i+1][j])
}
}
}
return dp[0][s.length-1]
};
|