当前位置:   article > 正文

代码随想录-算法训练营day53【动态规划14:最长公共子序列、不相交的线、最大子序和(动态规划)】

代码随想录-算法训练营day53【动态规划14:最长公共子序列、不相交的线、最大子序和(动态规划)】

代码随想录-035期-算法训练营【博客笔记汇总表】-CSDN博客

  1. 第九章 动态规划part14
  2. ● 1143.最长公共子序列
  3. ● 1035.不相交的线
  4. ● 53. 最大子序和 动态规划
  5. 详细布置
  6. 1143.最长公共子序列
  7. 体会一下本题和 718. 最长重复子数组 的区别
  8. 视频讲解:https://www.bilibili.com/video/BV1ye4y1L7CQ
  9. https://programmercarl.com/1143.%E6%9C%80%E9%95%BF%E5%85%AC%E5%85%B1%E5%AD%90%E5%BA%8F%E5%88%97.html
  10. 1035.不相交的线
  11. 其实本题和 1143.最长公共子序列 是一模一样的,大家尝试自己做一做。
  12. 视频讲解:https://www.bilibili.com/video/BV1h84y1x7MP
  13. https://programmercarl.com/1035.%E4%B8%8D%E7%9B%B8%E4%BA%A4%E7%9A%84%E7%BA%BF.html
  14. 53. 最大子序和
  15. 这道题我们用贪心做过,这次 再用dp来做一遍
  16. 视频讲解:https://www.bilibili.com/video/BV19V4y1F7b5
  17. https://programmercarl.com/0053.%E6%9C%80%E5%A4%A7%E5%AD%90%E5%BA%8F%E5%92%8C%EF%BC%88%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%EF%BC%89.html
  18. 往日任务
  19. ● day 1 任务以及具体安排:https://docs.qq.com/doc/DUG9UR2ZUc3BjRUdY
  20. ● day 2 任务以及具体安排:https://docs.qq.com/doc/DUGRwWXNOVEpyaVpG
  21. ● day 3 任务以及具体安排:https://docs.qq.com/doc/DUGdqYWNYeGhlaVR6
  22. ● day 4 任务以及具体安排:https://docs.qq.com/doc/DUFNjYUxYRHRVWklp
  23. ● day 5 周日休息
  24. ● day 6 任务以及具体安排:https://docs.qq.com/doc/DUEtFSGdreWRuR2p4
  25. ● day 7 任务以及具体安排:https://docs.qq.com/doc/DUElCb1NyTVpXa0Jj
  26. ● day 8 任务以及具体安排:https://docs.qq.com/doc/DUGdsY2JFaFhDRVZH
  27. ● day 9 任务以及具体安排:https://docs.qq.com/doc/DUHVXSnZNaXpVUHN4
  28. ● day 10 任务以及具体安排:https://docs.qq.com/doc/DUElqeHh3cndDbW1Q
  29. ●day 11 任务以及具体安排:https://docs.qq.com/doc/DUHh6UE5hUUZOZUd0
  30. ●day 12 周日休息
  31. ●day 13 任务以及具体安排:https://docs.qq.com/doc/DUHNpa3F4b2dMUWJ3
  32. ●day 14 任务以及具体安排:https://docs.qq.com/doc/DUHRtdXZZSWFkeGdE
  33. ●day 15 任务以及具体安排:https://docs.qq.com/doc/DUHN0ZVJuRmVYeWNv
  34. ●day 16 任务以及具体安排:https://docs.qq.com/doc/DUHBQRm1aSWR4T2NK
  35. ●day 17 任务以及具体安排:https://docs.qq.com/doc/DUFpXY3hBZkpabWFY
  36. ●day 18 任务以及具体安排:https://docs.qq.com/doc/DUFFiVHl3YVlReVlr
  37. ●day 19 周日休息
  38. ●day 20 任务以及具体安排:https://docs.qq.com/doc/DUGFRU2V6Z1F4alBH
  39. ●day 21 任务以及具体安排:https://docs.qq.com/doc/DUHl2SGNvZmxqZm1X
  40. ●day 22 任务以及具体安排:https://docs.qq.com/doc/DUHplVUp5YnN1bnBL
  41. ●day 23 任务以及具体安排:https://docs.qq.com/doc/DUFBUQmxpQU1pa29C
  42. ●day 24 任务以及具体安排:https://docs.qq.com/doc/DUEhsb0pUUm1WT2NP
  43. ●day 25 任务以及具体安排:https://docs.qq.com/doc/DUExTYXVzU1BiU2Zl
  44. ●day 26 休息
  45. ●day 27 任务以及具体安排:https://docs.qq.com/doc/DUElpbnNUR3hIbXlY
  46. ●day 28 任务以及具体安排:https://docs.qq.com/doc/DUG1yVHdlWEdNYlhZ
  47. ●day 29 任务以及具体安排:https://docs.qq.com/doc/DUHZYbWhwSHRCRmp3
  48. ●day 30 任务以及具体安排:https://docs.qq.com/doc/DUEdTVVhxbnJiY3BR
  49. ●day 31 任务以及具体安排:https://docs.qq.com/doc/DUG1PQ1ZZY2xXY1ly
  50. ●day 32 任务以及具体安排:https://docs.qq.com/doc/DUGFEdGFWeVhleFF1
  51. ●day 33 周日休息
  52. ●day 34 任务以及具体安排:https://docs.qq.com/doc/DUEh5WFVlQkp1U0p4
  53. ●day 35 任务以及具体安排:https://docs.qq.com/doc/DUFRWc3BGRHFXZ1pO
  54. ●day 36 任务以及具体安排:https://docs.qq.com/doc/DUERGbnhhRkFRVENZ
  55. ●day 37 任务以及具体安排:https://docs.qq.com/doc/DUFVRd3p5SHFMSExQ
  56. ●day 38 任务以及具体安排:https://docs.qq.com/doc/DUGNUdVpoT0VJR01l
  57. ●day 39 任务以及具体安排:https://docs.qq.com/doc/DUE55cVJ5WkNoREhS
  58. ●day 40 周日休息
  59. ●day 41 任务以及具体安排:https://docs.qq.com/doc/DUFhIUXRFYnVGUkFp
  60. ●day 42 任务以及具体安排:42 第八章 动态规划
  61. ●day 43 任务以及具体安排:43第八章 动态规划
  62. ●day 44 任务以及具体安排:44 第八章 动态规划
  63. ●day 45 任务以及具体安排:45 第八章 动态规划
  64. ●day 46 任务以及具体安排:46 第八章 动态规划
  65. ●day 47 周日休息
  66. ●day 48 任务以及具体安排:48 第八章 动态规划
  67. ●day 49 任务以及具体安排:49 第八章 动态规划
  68. ●day 50 任务以及具体安排:50 第八章 动态规划
  69. ●day 51 任务以及具体安排:51 第八章 动态规划
  70. ●day 52 任务以及具体安排:52 第八章 动态规划

目录

1143_最长公共子序列

1035_不相交的线

0053_最大子序和(动态规划)


1143_最长公共子序列

  1. package com.question.solve.leetcode.programmerCarl2._10_dynamicProgramming;
  2. public class _1143_最长公共子序列 {
  3. }
  4. class Solution1143 {//二维dp数组
  5. public int longestCommonSubsequence(String text1, String text2) {
  6. //char[] char1 = text1.toCharArray();
  7. //char[] char2 = text2.toCharArray();
  8. //可以在一开始的时候就先把text1、text2 转成char[],之后就不需要有这么多为了处理字符串的调整。
  9. //就可以和卡哥的code更一致
  10. int[][] dp = new int[text1.length() + 1][text2.length() + 1];//先对dp数组做初始化操作
  11. for (int i = 1; i <= text1.length(); i++) {
  12. char char1 = text1.charAt(i - 1);
  13. for (int j = 1; j <= text2.length(); j++) {
  14. char char2 = text2.charAt(j - 1);
  15. if (char1 == char2) {//开始列出状态转移方程
  16. dp[i][j] = dp[i - 1][j - 1] + 1;
  17. } else {
  18. dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
  19. }
  20. }
  21. }
  22. return dp[text1.length()][text2.length()];
  23. }
  24. }
  25. class Solution1143_2 {//一维dp数组
  26. public int longestCommonSubsequence(String text1, String text2) {
  27. int n1 = text1.length();
  28. int n2 = text2.length();
  29. //多从二维dp数组过程分析
  30. //关键在于 如果记录 dp[i - 1][j - 1]
  31. //因为 dp[i - 1][j - 1] <!=> dp[j - 1] <=> dp[i][j - 1]
  32. int[] dp = new int[n2 + 1];
  33. for (int i = 1; i <= n1; i++) {
  34. //这里pre相当于 dp[i - 1][j - 1]
  35. int pre = dp[0];
  36. for (int j = 1; j <= n2; j++) {
  37. //用于给pre赋值
  38. int cur = dp[j];
  39. if (text1.charAt(i - 1) == text2.charAt(j - 1)) {
  40. //这里pre相当于dp[i - 1][j - 1],千万不能用dp[j - 1] !!
  41. dp[j] = pre + 1;
  42. } else {
  43. //dp[j] 相当于 dp[i - 1][j]
  44. //dp[j - 1] 相当于 dp[i][j - 1]
  45. dp[j] = Math.max(dp[j], dp[j - 1]);
  46. }
  47. //更新dp[i - 1][j - 1],为下次使用做准备
  48. pre = cur;
  49. }
  50. }
  51. return dp[n2];
  52. }
  53. }

1035_不相交的线

  1. package com.question.solve.leetcode.programmerCarl2._10_dynamicProgramming;
  2. public class _1035_不相交的线 {
  3. }
  4. class Solution1035 {
  5. public int maxUncrossedLines(int[] nums1, int[] nums2) {
  6. int len1 = nums1.length;
  7. int len2 = nums2.length;
  8. int[][] dp = new int[len1 + 1][len2 + 1];
  9. for (int i = 1; i <= len1; i++) {
  10. for (int j = 1; j <= len2; j++) {
  11. if (nums1[i - 1] == nums2[j - 1]) {
  12. dp[i][j] = dp[i - 1][j - 1] + 1;
  13. } else {
  14. dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
  15. }
  16. }
  17. }
  18. return dp[len1][len2];
  19. }
  20. }

0053_最大子序和(动态规划)

  1. package com.question.solve.leetcode.programmerCarl2._10_dynamicProgramming;
  2. public class _0053_最大子序和 {
  3. }
  4. class Solution0053 {
  5. public int maxSubArray(int[] nums) {
  6. if (nums.length == 1) {
  7. return nums[0];
  8. }
  9. int sum = Integer.MIN_VALUE;
  10. int count = 0;
  11. for (int i = 0; i < nums.length; i++) {
  12. count += nums[i];
  13. sum = Math.max(sum, count);//取区间累计的最大值(相当于不断确定最大子序终止位置)
  14. if (count <= 0) {
  15. count = 0;//相当于重置最大子序起始位置,因为遇到负数一定是拉低总和
  16. }
  17. }
  18. return sum;
  19. }
  20. }
  21. class Solution0053_2 {
  22. /**
  23. * 1.dp[i]代表当前下标对应的最大值
  24. * 2.递推公式 dp[i] = max (dp[i-1]+nums[i],nums[i]) res = max(res,dp[i])
  25. * 3.初始化 都为 0
  26. * 4.遍历方向,从前往后
  27. * 5.举例推导结果。。。
  28. *
  29. * @param nums
  30. * @return
  31. */
  32. public static int maxSubArray(int[] nums) {
  33. if (nums.length == 0) {
  34. return 0;
  35. }
  36. int res = nums[0];
  37. int[] dp = new int[nums.length];
  38. dp[0] = nums[0];
  39. for (int i = 1; i < nums.length; i++) {
  40. dp[i] = Math.max(dp[i - 1] + nums[i], nums[i]);
  41. res = res > dp[i] ? res : dp[i];
  42. }
  43. return res;
  44. }
  45. }
  46. //因为dp[i]的递推公式只与前一个值有关,所以可以用一个变量代替dp数组,空间复杂度为O(1)
  47. class Solution0053_3 {
  48. public int maxSubArray(int[] nums) {
  49. int res = nums[0];
  50. int pre = nums[0];
  51. for (int i = 1; i < nums.length; i++) {
  52. pre = Math.max(pre + nums[i], nums[i]);
  53. res = Math.max(res, pre);
  54. }
  55. return res;
  56. }
  57. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/900844
推荐阅读
相关标签
  

闽ICP备14008679号