• 20220807 leetcodez周赛


    本次周赛只做出来两题。前路漫漫,还要继续加油呀!

    第三题总结:

     思路:当时的思路是dfs,结果超时了。

    正确的思路是用动态规划。dp[i] = true, 表示以从第0个到第i-1个数结尾的数组可以进行有效划分。

    boolean a = (nums[i-1] == nums[i-2] && dp[i-2])

    boolean b = ((nums[i-1] == nums[i-2] && nums[i-2] == nums[i-3]) || (nums[i-1] == nums[i-2]+1 && nums[i-3] == nums[i-2]-1)) && dp[i-3]

    dp[i] = a || b;

    还有一个注意点,注意不要排序!!!!

    代码:

    1. class Solution {
    2. public boolean validPartition(int[] nums) {
    3. // Arrays.sort(nums);
    4. boolean[] dp = new boolean[nums.length+1];
    5. dp[0] = true;
    6. dp[1] = false;
    7. for(int i = 2; i <= nums.length; i++) {
    8. if(i == 2) {
    9. if(nums[i-1] == nums[i-2]) {
    10. dp[i] = true;
    11. } else {
    12. dp[i] = false;
    13. }
    14. } else {
    15. boolean a = false,b = false;
    16. if(nums[i-1] == nums[i-2]) {
    17. a = true && dp[i-2];
    18. }
    19. if((nums[i-1] == nums[i-2] && nums[i-2] == nums[i-3]) || (nums[i-1] == nums[i-2]+1 && nums[i-3] == nums[i-2]-1)) {
    20. b = true && dp[i-3];
    21. }
    22. dp[i] = a || b;
    23. }
    24. // System.out.println(i+":"+dp[i]);
    25. }
    26. return dp[nums.length];
    27. }
    28. }

    当然,如果这个题变成有几种划分方法,可以用dfs。

    dfs代码也奉上:

    1. class Solution {
    2. public boolean validPartition(int[] nums) {
    3. return backTrack(0, nums);
    4. }
    5. public boolean backTrack(int index, int[] nums) {
    6. if(index == nums.length) {
    7. return true;
    8. }
    9. if(index == nums.length-1) {
    10. return false;
    11. }
    12. if(nums[index] == nums[index+1] && backTrack(index+2, nums)) {
    13. return true;
    14. }
    15. if(index == nums.length-2) {
    16. return false;
    17. }
    18. if((nums[index] == nums[index+1] && nums[index+1] == nums[index+2]) || (nums[index] == nums[index+1]-1 && nums[index+1] == nums[index+2]-1)) {
    19. if(backTrack(index+3, nums)) {
    20. return true;
    21. }
    22. }
    23. return false;
    24. }
    25. }

    第四题总结:

    力扣icon-default.png?t=M666https://leetcode.cn/problems/longest-ideal-subsequence/

     思路:从当前元素往前找,找到符合条件的s.charAt(j), 使得当前的dp[i] = Math.max(dp[i],dp[j]+1);

    双层for循环,但是这么做的话,时间复杂度(0(n*n))会超时。

    因此,可以用一个HashMap记录下字母的位置,直接使用HashMap寻找符合条件的字母的位置。注意,不需要用一个LinkedList记录字母出现的所有位置,只要记录下最新的就可以了。

    代码奉上:

    1. class Solution {
    2. public int longestIdealString(String s, int k) {
    3. int[] dp = new int[s.length()];
    4. HashMap map = new HashMap<>();
    5. int max = 1;
    6. map.put(s.charAt(0) - 'a', 0);
    7. dp[0] = 1;
    8. for(int i = 1; i < s.length(); i++) {
    9. dp[i] = 1;
    10. for(int j = -k ; j <= k; j++) {
    11. int num = map.getOrDefault(s.charAt(i)-'a'+j, -1);
    12. if(num!= -1) {
    13. dp[i] = Math.max(dp[i],dp[num]+1);
    14. }
    15. }
    16. max = Math.max(dp[i], max);
    17. map.put(s.charAt(i)-'a', i);
    18. }
    19. return max;
    20. }
    21. }

  • 相关阅读:
    C语言入门Day_20 函数的定义
    MySQL3:MySQL中一条更新SQL是如何执行的?
    简单线性回归(Simple Linear Regression)
    js实现页面元素的拖拽
    如果员工因为管理者而辞职,为什么不解雇管理者呢?
    爬虫逆向实战(32)-某号店登录(RSA、补环境、混淆)
    【每日一句】名人金句学英语(20221130)
    tomcat 的日志详解
    程序员如何“升级打怪”?我用了这几个“歪瓜”!
    基于 FPGA 实现 IIC(I2C) 协议控制 EEPROM 读写操作
  • 原文地址:https://blog.csdn.net/xuan971130/article/details/126212593