动态规划(Dynamic Programming)问题集锦

快来打我* 2022-05-29 22:45 222阅读 0赞

原文站点:https://senitco.github.io/2018/02/04/data-structure-dynamic-programming-1/

数据结构与算法中动态规划问题的总结归纳。

Word Break

题目描述:LeetCode
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words,
determine if s can be segmented into a space-separated sequence of one or more dictionary
words. You may assume the dictionary does not contain duplicate words.
For example, given s = “leetcode”, dict = [“leet”, “code”].
Return true because “leetcode” can be segmented as “leet code”.
分析:DP解法:定义一个数组dp[],dp[i]为true表示一个有效的单词或单词序列和字符串s的前i个字符匹配

  1. bool wordBreak(string s, vector<string>& wordDict)
  2. {
  3. unordered_set<string> wordSet;
  4. for(auto word : wordDict)
  5. wordSet.insert(word); //将所有元素移至hashset中,这样在一轮搜索过程中时间复杂度为O(1)
  6. vector<bool> dp(s.length() + 1, false);
  7. dp[0] = true;
  8. for(int i = 1; i <= s.length(); i++)
  9. {
  10. for(int j = i - 1; j >= 0; j--) //反向遍历可能会更快
  11. {
  12. if(dp[j]) //dp[j]为true且子串s.substr(j, i-j)在word集合中,则dp[i]为true
  13. {
  14. string str = s.substr(j, i - j);
  15. if(wordSet.find(str) != wordSet.end())
  16. {
  17. dp[i] = true;
  18. break;
  19. }
  20. }
  21. }
  22. }
  23. return dp[s.length()];
  24. }

Malloc Candy

题目描述:LeetCode
There are N children standing in a line. Each child is assigned a rating value.
You are giving candies to these children subjected to the following requirements:
Each child must have at least one candy.
Children with a higher rating get more candies than their neighbors.
What is the minimum candies you must give?
分析:分别前向、后向遍历一次,更新每个人应该分配的最小数量

  1. int mallocCandy(vector<int>& ratings)
  2. {
  3. int size = ratings.size();
  4. if(size < 2)
  5. return size;
  6. int result = 0;
  7. vector<int> nums(size, 1);
  8. for(int i = 1; i < size; i++)
  9. {
  10. if(ratings[i] > ratings[i - 1])
  11. nums[i] = nums[i - 1] + 1;
  12. }
  13. for(int i = size - 1; i > 0; i--)
  14. {
  15. if(ratings[i - 1] > ratings[i])
  16. nums[i - 1] = max(nums[i - 1], nums[i] + 1);
  17. result += nums[i]; //在第二次遍历(反向)时直接累加,避免再循环一次
  18. }
  19. result += nums[0];
  20. return result;
  21. }

Palindrome Partitioning II

题目描述:LeetCode
Given a string s, partition s such that every substring of the partition is a palindrome.
Return the minimum cuts needed for a palindrome partitioning of s.
For example, given s = “aab”,
Return 1 since the palindrome partitioning [“aa”,”b”] could be produced using 1 cut.

  1. //法一
  2. int minCut(string s)
  3. {
  4. int n = s.size();
  5. vector<int> cut(n + 1, 0);
  6. for(int i = 0; i < n + 1; i++)
  7. cut[i] = i - 1; //初始化dp值
  8. for(int i = 0; i < n; i++)
  9. {
  10. for(int j = 0; i - j >= 0 && i + j < n && s[i - j] == s[i + j]; j++)
  11. cut[i + j + 1] = min(cut[i + j + 1], cut[i - j] + 1); //奇数长度的回文序列
  12. for(int j = 1; i - j + 1 >= 0 && i + j < n && s[i - j + 1] == s[i + j]; j++)
  13. cut[i + j + 1] = min(cut[i + j + 1], cut[i - j + 1] + 1); //偶数长度的回文序列
  14. }
  15. return cut[n];
  16. }
  17. //法二:dp[i + 1]记录长度为i的序列的最小分割次数,isPal[i][j]表示s[i...j]是否为回文序列
  18. int minCut(string s)
  19. {
  20. int n = s.size();
  21. vector<int> dp(n + 1, 0);
  22. for(int i = 0; i < n + 1; i++)
  23. dp[i] = i - 1;
  24. vector<vector<bool>> isPal(n, vector<bool>(n, false));
  25. for(int right = 0; right < n; right++)
  26. {
  27. for(int left = 0; left <= right; left++)
  28. {
  29. if(s[left] == s[right] && (right - left < 2 || isPal[left + 1][right - 1]))
  30. {
  31. isPal[left][right] = true;
  32. if(left == 0)
  33. dp[right + 1] = 0;
  34. else
  35. dp[right + 1] = min(dp[right + 1], dp[left] + 1);
  36. }
  37. }
  38. }
  39. return dp[n];
  40. }

发表评论

表情:
评论列表 (有 0 条评论,222人围观)

还没有评论,来说两句吧...

相关阅读

    相关 动态规划Dynamic Programming

    首先以LeetCode上面的一个问题来开始动态规划的讲解: 题目描述:你是一个专业的强盗,计划抢劫沿街的房屋。每间房都藏有一定的现金,阻止你抢劫他们的唯一的制约因素就是相邻的

    相关 算法-动态规划 Dynamic Programming

    今天在leetcode刷题的时候,遇到动态规划类型的题目,想更加深入的了解下,就从网上搜了搜,发现博主的这篇文章讲解的很详细,因此分享下,希望给大家都带来帮助。 转载自:[h