随笔分类 -  DP问题系列整理

一系列常见DP问题
96. Unique Binary Search Trees(dp)
摘要:Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n? Example: Input: 3 Output: 5 Explanation: Given n = 3, the 阅读全文

posted @ 2021-01-10 14:56 wsw_seu 阅读(118) 评论(0) 推荐(0)

494. 目标和(动态规划,01背包)
摘要:给定一个非负整数数组,a1, a2, ..., an, 和一个目标数,S。现在你有两个符号 + 和 -。对于数组中的任意一个整数,你都可以从 + 或 -中选择一个符号添加在前面。 返回可以使最终数组和为目标数 S 的所有添加符号的方法数。 示例: 输入:nums: [1, 1, 1, 1, 1], 阅读全文

posted @ 2020-12-20 15:14 wsw_seu 阅读(106) 评论(0) 推荐(0)

279. Perfect Squares(dp)
摘要:Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. Example 1: Input: n = 12 O 阅读全文

posted @ 2020-11-21 22:18 wsw_seu 阅读(143) 评论(0) 推荐(0)

dp:322. Coin Change 自下而上的dp
摘要:You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you nee 阅读全文

posted @ 2020-11-11 21:56 wsw_seu 阅读(124) 评论(0) 推荐(0)

动态规划题解(转)
摘要:动态规划算法似乎是一种很高深莫测的算法,你会在一些面试或算法书籍的高级技巧部分看到相关内容,什么状态转移方程,重叠子问题,最优子结构等高大上的词汇也可能让你望而却步。 而且,当你去看用动态规划解决某个问题的代码时,你会觉得这样解决问题竟然如此巧妙,但却难以理解,你可能惊讶于人家是怎么想到这种解法的。 阅读全文

posted @ 2020-07-26 18:16 wsw_seu 阅读(334) 评论(0) 推荐(0)

dp背包 面试题 08.11. 硬币
摘要:https://leetcode-cn.com/problems/coin-lcci/ 硬币。给定数量不限的硬币,币值为25分、10分、5分和1分,编写代码计算n分有几种表示法。(结果可能会很大,你需要将结果模上1000000007) 示例1: 输入: n = 5 输出:2 解释: 有两种方式可以凑 阅读全文

posted @ 2020-04-29 20:46 wsw_seu 阅读(162) 评论(0) 推荐(0)

1、线性DP 213. 打家劫舍 II
摘要:https://leetcode-cn.com/problems/house-robber-ii/ //rob 0, not rob n-1 || not rob 0,not rob n-1 ==>rob(0,nums.length-2,nums) //not rob 0,rob n-1 || no 阅读全文

posted @ 2020-04-26 10:21 wsw_seu 阅读(137) 评论(0) 推荐(0)

4. 树形DP
摘要:337. 打家劫舍 III https://leetcode-cn.com/problems/house-robber-iii/ /** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *T 阅读全文

posted @ 2020-04-26 10:09 wsw_seu 阅读(100) 评论(0) 推荐(0)

1、线性DP 198. 打家劫舍
摘要:198. 打家劫舍 https://leetcode-cn.com/problems/house-robber/ //dp动态规划,dp[i] 状态表示0-i家的盗的得最大值。那么dp[i] = (dp[i-1],dp[i-2]+nums[i]) //第i家不偷,或者第i家偷,就这两种子问题。 fu 阅读全文

posted @ 2020-04-23 22:50 wsw_seu 阅读(138) 评论(0) 推荐(0)

1、线性DP 354. 俄罗斯套娃信封问题
摘要:354. 俄罗斯套娃信封问题 https://leetcode-cn.com/problems/russian-doll-envelopes/ 算法分析 首先我们从两种情况来讨论这个问题: w无重复值(即信封的宽度每个信封都不一样) w可以重复(即信封的宽度存在一样的,题目就是这种情况) 针对情况I 阅读全文

posted @ 2020-04-22 18:59 wsw_seu 阅读(234) 评论(0) 推荐(0)

1. 线性DP 887. 鸡蛋掉落 (DP+二分)
摘要:887. 鸡蛋掉落 (DP+二分) https://leetcode-cn.com/problems/super-egg-drop/ /*首先分析1个蛋,1个蛋的话,最坏情况需要N次,每次只能从0 1 2 。。。开始如果蛋的个数随便用,或者说2的k次方大于等于N楼层高度,则可以利用二分。如果蛋的个数 阅读全文

posted @ 2020-04-21 18:01 wsw_seu 阅读(244) 评论(0) 推荐(0)

1. 线性DP 152. 乘积最大子数组
摘要:152. 乘积最大子数组 https://leetcode-cn.com/problems/maximum-product-subarray/ func maxProduct(nums []int) int { preMax,preMin,curMax,curMin,res := nums[0],n 阅读全文

posted @ 2020-04-20 16:50 wsw_seu 阅读(142) 评论(0) 推荐(0)

1. 线性DP 53. 最大子序和.
摘要:53. 最大子序和. https://leetcode-cn.com/problems/maximum-subarray/ func maxSubArray(nums []int) int { dp := make([]int,len(nums)) dp[0] = nums[0] for i:=1; 阅读全文

posted @ 2020-04-20 16:17 wsw_seu 阅读(155) 评论(0) 推荐(0)

1. 线性DP 120. 三角形最小路径和
摘要:经典问题: 120. 三角形最小路径和 https://leetcode-cn.com/problems/triangle/ func minimumTotal(triangle [][]int) int { n := len(triangle) dp := make([][]int,n) for 阅读全文

posted @ 2020-04-19 23:18 wsw_seu 阅读(117) 评论(0) 推荐(0)

1. 线性DP 1143. 最长公共子序列
摘要:最经典双串: 1143. 最长公共子序列 (LCS) https://leetcode-cn.com/problems/longest-common-subsequence/submissions/ func longestCommonSubsequence(text1 string, text2 阅读全文

posted @ 2020-04-19 15:53 wsw_seu 阅读(155) 评论(0) 推荐(0)

1. 线性DP 300. 最长上升子序列 (LIS)
摘要:最经典单串: 300. 最长上升子序列 (LIS) https://leetcode-cn.com/problems/longest-increasing-subsequence/submissions/ //GO //经典DP 线性DP //dp[i] 那么 nums[i] 必然要大于 nums[ 阅读全文

posted @ 2020-04-19 15:04 wsw_seu 阅读(226) 评论(0) 推荐(0)

导航