
动态规划
cx_cs
这个作者很懒,什么都没留下…
展开
-
174. 地下城游戏
无原创 2022-08-20 09:03:05 · 757 阅读 · 0 评论 -
132. 分割回文串 II
无原创 2023-03-09 13:15:51 · 226 阅读 · 0 评论 -
120. 三角形最小路径和
无原创 2023-03-07 11:41:16 · 36 阅读 · 0 评论 -
115. 不同的子序列
无原创 2022-08-19 11:39:29 · 51 阅读 · 0 评论 -
87. 扰乱字符串
无原创 2022-08-19 10:47:43 · 62 阅读 · 0 评论 -
97. 交错字符串
无原创 2022-08-19 09:58:59 · 354 阅读 · 1 评论 -
63. 不同路径 II
无原创 2023-03-08 10:41:11 · 70 阅读 · 0 评论 -
44. 通配符匹配
要求:第10题的弱化版思路:https://leetcode-cn.com/problems/wildcard-matching/solution/tong-pei-fu-pi-pei-by-leetcode-solution/class Solution {public: bool isMatch(string s, string p) { int m = s.size(); int n = p.size(); vector<vector原创 2022-04-04 20:12:05 · 178 阅读 · 0 评论 -
312. 戳气球
要求:戳破i获得nums[i-1]*nums[i]*nums[i+1],求全部最大值思路:dpclass Solution {public: int maxCoins(vector<int>& nums) { int n = nums.size(); vector<vector<int>> dp(n + 2,vector<int>(n + 2,0)); //创建一个辅助数组,并在首尾各添加1原创 2022-03-23 11:56:22 · 101 阅读 · 0 评论 -
221. 最大正方形
要求:矩阵中1构成的最大正方形思路:dpclass Solution {public: int maximalSquare(vector<vector<char>>& matrix) { if (matrix.size() == 0 || matrix[0].size() == 0) { return 0; } int maxSide = 0; int rows = matr原创 2022-03-22 13:52:27 · 103 阅读 · 0 评论 -
152. 乘积最大子数组
要求:rt思路:遇到小于0的最大变最小,最小变最大class Solution {public: int maxProduct(vector<int>& nums) { int maxValue = INT_MIN, tmpMax = 1, tmpMin = 1; for (int i = 0; i < nums.size(); ++ i) { if (nums[i] < 0)原创 2022-03-22 13:45:50 · 288 阅读 · 0 评论 -
55. 跳跃游戏
要求:数组的数表示能走的距离,问能否到达最后思路:正常做法class Solution {public: bool canJump(vector<int>& nums) { int n=nums.size(); vector<bool> dp(n); dp[0]=true; for(int i=0;i<n-1;++i){ for(int j=1;j<=nums[原创 2022-03-19 12:27:27 · 4034 阅读 · 0 评论 -
32. 最长有效括号
要求:长度思路:法一:栈class Solution {public: int longestValidParentheses(string s) { stack<int> st; int ans = 0; for (int i = 0,start = 0; i < s.size(); i++) { if (s[i] == '(') st.push(i); //左括号入栈原创 2022-03-18 12:28:37 · 383 阅读 · 0 评论 -
面试题19. 正则表达式匹配
要求:*和.思路:https://leetcode-cn.com/problems/zheng-ze-biao-da-shi-pi-pei-lcof/solution/jian-zhi-offer-19-zheng-ze-biao-da-shi-pi-pei-dong/class Solution {public: bool isMatch(string s, string p) { int m = s.size() + 1, n = p.size() + 1;原创 2022-03-12 17:40:11 · 792 阅读 · 0 评论 -
剑指 Offer 62. 圆圈中最后剩下的数字
要求:n个数每次删除往前第m个思路:一点也不简单的题,但不能想复杂了,f(n)为最后删除的数,f(n)走一次丢给f(n-1)然后从m%n开始,为什么能丢给f(n-1),因为正常来说f(n-1)是0到n-2,fn删了一个后要加个偏移,也就是说,最终的结果存在一一映射的关系,所以能够递推,就这么简单。代码数组可以去掉class Solution {public: int lastRemaining(int n, int m) { vector<int> dp(n+1);原创 2022-03-11 23:01:39 · 57 阅读 · 0 评论 -
剑指 Offer 60. n个骰子的点数
要求:输出所有的和的可能的概率思路:共六的n次方种情况,n个骰子范围是n,6n设dp[n][x]为n个骰子掷出和x的概率,显然dp[n][x]=Σdp[n-1][x-i]x1/6,i从1到6class Solution {public: vector<double> dicesProbability(int n) { vector<vector<double>> dp(n+1,vector<double>(6*n+1));原创 2022-03-10 20:56:49 · 105 阅读 · 0 评论 -
剑指 Offer 49. 丑数
要求:只含因子2,3,5(1也算),求第n个思路:朴素的做法是小根堆,每出队一个乘以2,3,5,第n个出队的就是。但是占空间很大。动态规划。记dp[n],用三个指针,首先dp[n]肯定是某个数乘以2或3或5得到的,p2下标表示这个数还没有乘以2,可以乘2比较,p3p5同理class Solution {public: int nthUglyNumber(int n) { vector<int> dp(n+1); dp[1]=1; in原创 2022-03-08 13:53:43 · 652 阅读 · 0 评论 -
剑指 Offer 48. 最长不含重复字符的子字符串
要求:rt思路:动态规划不够直观,直接哈希加滑动窗口class Solution {public: int lengthOfLongestSubstring(string s) { map<int,int> m; int maxlen=0; for(int start=0,end=0;end<s.length();++end){ if(m.count(s[end])&&m[s[end]]&原创 2022-03-08 12:27:51 · 234 阅读 · 0 评论 -
剑指 Offer 47. 礼物的最大价值
要求:沿右或者下求最大和思路:显然不能一条路走到黑,要算所有棋盘位置动态规划,不用dp直接用原数组也可以class Solution {public: int maxValue(vector<vector<int>>& grid) { int m=grid.size(),n=grid[0].size(); vector<vector<int>> dp(m+1,vector<int>(n+1));原创 2022-03-08 10:45:14 · 274 阅读 · 0 评论 -
剑指 Offer 46. 把数字翻译成字符串
要求:0-a,25-z,多种可能思路:动态规划12有1 2,12122有1 2 2,12 2,1 221222有1 2 2 2,12 2 2,1 22 2,1 2 22,12 22很明显,补单个不增加,结合前一位才增加,增加的个数等于前一位尚未结合的个数,用single和dp两个数组,single更新规则是等于前一个的dp实际上就是dp[i]=dp[i-1]+dp[i-2](可以的话)还可以把空间简化(本题dp从右向左也一样,所以直接对num取余)class Solution {publi原创 2022-03-08 10:12:56 · 135 阅读 · 0 评论 -
剑指 Offer 14- II. 剪绳子 II
要求:剪成m段,最大乘积,要取模思路:不能取模,只能贪心。贪心:平均分剪成3段乘积最大,推导见力扣上的题解。根据贪心,能整除则全3,余1的话要把最后一个4拆成2*2,余2的话乘上即可。要快速幂取模,原理是n为偶数则a的n拆为a的平方取模的n/2次方,奇数再则乘aclass Solution {public: long fastmod(int p){ if(p==0)return 1; long res=3; long rest=1;原创 2022-03-03 22:38:42 · 64 阅读 · 0 评论 -
650. 只有两个键的键盘
要求:只有一个A,只能全选复制、粘贴两种操作,生成n个A最少次数思路:法一:动规,可以同时f[i] = min(f[i], f[j] + i / j);f[i] = min(f[i], f[i / j] + j);然后j取到根号i就行作者:LeetCode-Solution链接:https://leetcode-cn.com/problems/2-keys-keyboard/solution/zhi-you-liang-ge-jian-de-jian-pan-by-lee-ussa/来源:力扣(L原创 2021-11-11 20:21:22 · 61 阅读 · 0 评论 -
72. 编辑距离
要求:增删改使两串一样思路:dp[i][j] 代表 word1 中前 i 个字符,变换到 word2 中前 j 个字符,最短需要操作的次数,增,dp[i][j] = dp[i][j - 1] + 删,dp[i][j] = dp[i - 1][j] + 1,改,dp[i][j] = dp[i - 1][j - 1] + 1class Solution {public: int minDistance(string word1, string word2) { int len1=w原创 2021-11-11 10:44:46 · 55 阅读 · 0 评论 -
583. 两个字符串的删除操作
要求:保留最长公共子序列思路:class Solution {public: int minDistance(string word1, string word2) { int len1=word1.length(),len2=word2.length(); vector<vector<int>> dp(len1+1,vector<int>(len2+1)); for(int i=1;i<=len1;++原创 2021-11-09 16:32:42 · 63 阅读 · 0 评论 -
188. 买卖股票的最-佳时机 IV
要求:同上题思路:同上题class Solution {public: int maxProfit(int k, vector<int>& prices) { int n=prices.size(); if(n<=1)return 0; vector<vector<int>> buy(n,vector<int>(k+1)); vector<vector<in原创 2021-11-09 15:05:22 · 191 阅读 · 0 评论 -
123. 买卖股票的最-佳时机 III
要求:只能买卖两次思路:考虑第i天状态,buy[i][j]表示进行了j次买,sell[i][j]同理原创 2021-11-09 14:56:33 · 174 阅读 · 0 评论 -
714. 买卖股票的最-佳时机含手续费
要求:含手续费思路:法一:动规,题目意思是卖出时才扣手续费。还可以更简化为两种状态持股和不持股class Solution {public: int maxProfit(vector<int>& prices, int fee) { int n=prices.size(); vector<vector<int>> dp(n,vector<int>(4,0)); dp[0][0]=dp[0]原创 2021-11-06 23:25:40 · 78 阅读 · 0 评论 -
309. 最-佳买卖股票时机含冷冻期
要求:卖出有一天冷冻期,求最大收益思路:每天可能有四种状态:买、持有、卖、空仓,见注释class Solution {public: int maxProfit(vector<int>& prices) { int n=prices.size(); vector<vector<int>> dp(n,vector<int>(4,0)); dp[0][0]=dp[0][1]=-prices[0]原创 2021-11-06 21:32:10 · 73 阅读 · 0 评论 -
377. 组合总和 Ⅳ
要求:给target,问数组里组合思路:{1,3}{3,1}不算重复,物品放里面class Solution {public: int combinationSum4(vector<int>& nums, int target) { vector<int> dp(target+1); dp[0]=1; for(int j=1;j<=target;++j) for(auto& a原创 2021-11-06 20:10:52 · 81 阅读 · 0 评论 -
139. 单词拆分
要求:字符串是否能拆分为字典里单词思路:单词可重复,暗示这是一道完全背包,然后本题只求布尔值两种循环都行,但是单词要按顺序所以单词应放在里面class Solution {public: bool wordBreak(string s, vector<string>& wordDict) { vector<bool> dp(s.length()+1); dp[0]=true; for(int j=1;j<=s原创 2021-11-06 19:34:58 · 78 阅读 · 0 评论 -
518. 零钱兑换 II
要求:上一题是最少硬币个数,这一题是种数,要相加思路:法一:完全背包,dp[i][j],j为总额class Solution {public: int change(int amount, vector<int>& coins) { vector<int> dp(amount+1); dp[0]=1; for(auto& coin:coins) for(int j=coin;j&l原创 2021-11-06 19:12:18 · 64 阅读 · 0 评论 -
322. 零钱兑换
要求:组成amount最少硬币数思路:法一:硬币可以无限使用,为完全背包问题,需要注意和01背包区别在于要顺序遍历,为什么?https://blog.youkuaiyun.com/yandaoqiusheng/article/details/84929357class Solution {public: int coinChange(vector<int>& coins, int amount) { if(amount==0)return 0; ve原创 2021-11-06 15:59:34 · 76 阅读 · 0 评论 -
474. 一和零
要求:字符串数组找0和1不超过m和n,最大子集长度思路:多维01背包我草,dp[i][j][k]表示i个串构成不超过j个0和k个1的最大长度,递推应该要取dp[i-1][j]和dp[i-1][j-v]+1的max,j和k要嵌套循环class Solution {public: int findMaxForm(vector<string>& strs, int m, int n) { vector<vector<int>> dp(m+1原创 2021-11-06 11:38:18 · 78 阅读 · 0 评论 -
494. 目标和
要求:非负数组,给元素加负号求和等于target的种数思路:回溯是O(2^n)太慢了,和等于target可以考虑01背包。添加负号元素之和pn(这里是正的),则正号和为sum-pn,则sum-pn-pn=target,pn=(sum-target)/2,即求能不能使和为pn的01背包问题,每个数只能用一次,dp[i][j]表示i个数构成和为j的次数,递推时要相加class Solution {public: int findTargetSumWays(vector<int>&原创 2021-11-06 10:50:35 · 89 阅读 · 0 评论 -
416. 分割等和子集
要求:如题思路:先算出一半和,然后01背包, 跟平方和分割有点像,但是平方和那个没有数字限制,这个有,变成背包问题放n件物品每个物品只能用一次,价值等于sum/2,dp表示i件能否等于j,一个个放,不会重复,且不会漏class Solution {public: bool canPartition(vector<int>& nums) { int n=nums.size(); int sum=0; for(int i=0;i&原创 2021-11-04 13:30:07 · 81 阅读 · 0 评论 -
01背包问题
能装N件物品,体积最大W,求最大价值vdp[i][j] 表示前 i 件物品体积不超过 j 的情况下能达到的最大价值,注意是不超过不是等于,所以状态方程不放或者放,j大于等于当前物品重量w才能放dp[i][j]=max(dp[i-1][j],dp[i-1][j-w]+v[i])for (int i = 1; i <= N; i++) { int w = weights[i - 1], v = values[i - 1]; for (int j = 1; j <原创 2021-11-03 12:36:42 · 82 阅读 · 0 评论 -
1143. 最长公共子序列
要求:两个序列的最长公共子序列思路:二维动规class Solution {public: int longestCommonSubsequence(string text1, string text2) { int n1=text1.length(),n2=text2.length(); vector<vector<int>> dp(n1+1,vector<int>(n2+1)); for(int i=1;i原创 2021-11-03 11:29:15 · 81 阅读 · 0 评论 -
376. 摆动序列
要求:最长摆动子序列,增减增思路:法一:模仿最长递增子序列的独特的思路O(n2)class Solution {public: int wiggleMaxLength(vector<int>& nums) { int n=nums.size(); int dp[n]; int big[n]; int maxlen=1; for(int i=0;i<n;++i){原创 2021-11-02 23:06:29 · 70 阅读 · 0 评论 -
646. 最长数对链
要求:[[1,2], [2,3], [3,4]],最长的数对链是 [1,2] -> [3,4]思路:要先排序,然后跟最长递增子序列类似class Solution {public: static bool cmp(vector<int>& a,vector<int>& b){ return a[0]!=b[0]?a[0]<b[0]:a[1]<b[1]; } int findLongestChain(vec原创 2021-11-02 13:22:40 · 84 阅读 · 0 评论 -
300. 最长递增子序列
要求:最长严格递增子序列长度思路:动规,dp[i]应该表示以si结尾的还是到位置i的最长长度?注意到需要比较数字所以以si结尾直接比较si方便一些法一:动规class Solution {public: int lengthOfLIS(vector<int>& nums) { int n=nums.size(); int dp[n]; int maxlen=1; dp[0]=1; for(in原创 2021-11-02 12:24:31 · 78 阅读 · 0 评论