【题解】—— LeetCode一周小结33

🌟欢迎来到 我的博客 —— 探索技术的无限可能!


🌟博客的简介(文章目录)


【题解】—— 每日一道题目栏


上接:【题解】—— LeetCode一周小结32

12.实现一个魔法字典

题目链接:676. 实现一个魔法字典

设计一个使用单词列表进行初始化的数据结构,单词列表中的单词 互不相同 。 如果给出一个单词,请判定能否只将这个单词中一个字母换成另一个字母,使得所形成的新单词存在于你构建的字典中。

实现 MagicDictionary 类:

MagicDictionary() 初始化对象
void buildDict(String[] dictionary) 使用字符串数组 dictionary 设定该数据结构,dictionary 中的字符串互不相同
bool search(String searchWord) 给定一个字符串 searchWord ,判定能否只将字符串中 一个 字母换成另一个字母,使得所形成的新字符串能够与字典中的任一字符串匹配。如果可以,返回 true ;否则,返回 false 。

示例:

输入

[“MagicDictionary”, “buildDict”, “search”, “search”, “search”,
“search”]

[[], [[“hello”, “leetcode”]], [“hello”], [“hhllo”], [“hell”],
[“leetcoded”]]

输出

[null, null, false, true, false, false]

解释

MagicDictionary magicDictionary = new MagicDictionary();

magicDictionary.buildDict([“hello”, “leetcode”]);

magicDictionary.search(“hello”); // 返回 False

magicDictionary.search(“hhllo”); // 将第二个 ‘h’ 替换为 ‘e’ 可以匹配 “hello”
,所以返回 True

magicDictionary.search(“hell”); // 返回 False

magicDictionary.search(“leetcoded”); // 返回 False

提示:

1 <= dictionary.length <= 100

1 <= dictionary[i].length <= 100

dictionary[i] 仅由小写英文字母组成

dictionary 中的所有字符串 互不相同

1 <= searchWord.length <= 100

searchWord 仅由小写英文字母组成

buildDict 仅在 search 之前调用一次

最多调用 100 次 search

题解:
方法:前缀树 + DFS
        

class Trie {
    private Trie[] children = new Trie[26];
    private boolean isEnd;

    public void insert(String w) {
        Trie node = this;
        for (char c : w.toCharArray()) {
            int i = c - 'a';
            if (node.children[i] == null) {
                node.children[i] = new Trie();
            }
            node = node.children[i];
        }
        node.isEnd = true;
    }

    public boolean search(String w) {
        return dfs(w, 0, this, 0);
    }

    private boolean dfs(String w, int i, Trie node, int diff) {
        if (i == w.length()) {
            return diff == 1 && node.isEnd;
        }
        int j = w.charAt(i) - 'a';
        if (node.children[j] != null) {
            if (dfs(w, i + 1, node.children[j], diff)) {
                return true;
            }
        }
        if (diff == 0) {
            for (int k = 0; k < 26; k++) {
                if (k != j && node.children[k] != null) {
                    if (dfs(w, i + 1, node.children[k], 1)) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
}

class MagicDictionary {
    private Trie trie = new Trie();

    public MagicDictionary() {
    }

    public void buildDict(String[] dictionary) {
        for (String w : dictionary) {
            trie.insert(w);
        }
    }

    public boolean search(String searchWord) {
        return trie.search(searchWord);
    }
}

/**
 * Your MagicDictionary object will be instantiated and called as such:
 * MagicDictionary obj = new MagicDictionary();
 * obj.buildDict(dictionary);
 * boolean param_2 = obj.search(searchWord);
 */

13.特殊数组 I

题目链接:3151. 特殊数组 I

如果数组的每一对相邻元素都是两个奇偶性不同的数字,则该数组被认为是一个 特殊数组 。

你有一个整数数组 nums。如果 nums 是一个 特殊数组 ,返回 true,否则返回 false。

示例 1:

输入:nums = [1]

输出:true

解释:

只有一个元素,所以答案为 true。

示例 2:

输入:nums = [2,1,4]

输出:true

解释:

只有两对相邻元素: (2,1) 和 (1,4),它们都包含了奇偶性不同的数字,因此答案为 true。

示例 3:

输入:nums = [4,3,1,6]

输出:false

解释:

nums[1] 和 nums[2] 都是奇数。因此答案为 false。

提示:

1 <= nums.length <= 100

1 <= nums[i] <= 100

题解:
方法:一次遍历
        

class Solution {
    public boolean isArraySpecial(int[] nums) {
        for (int i = 1; i < nums.length; ++i) {
            if (nums[i] % 2 == nums[i - 1] % 2) {
                return false;
            }
        }
        return true;
    }
}

14.特殊数组 II

题目链接:3152. 特殊数组 II

如果数组的每一对相邻元素都是两个奇偶性不同的数字,则该数组被认为是一个 特殊数组 。

你有一个整数数组 nums 和一个二维整数矩阵 queries,对于 queries[i] = [fromi, toi],请你帮助你检查
子数组
nums[fromi…toi] 是不是一个 特殊数组 。

返回布尔数组 answer,如果 nums[fromi…toi] 是特殊数组,则 answer[i] 为 true ,否则,answer[i] 为 false 。

示例 1:

输入:nums = [3,4,1,2,6], queries = [[0,4]]

输出:[false]

解释:

子数组是 [3,4,1,2,6]。2 和 6 都是偶数。

示例 2:

输入:nums = [4,3,1,6], queries = [[0,2],[2,3]]

输出:[false,true]

解释:

子数组是 [4,3,1]。3 和 1 都是奇数。因此这个查询的答案是 false。

子数组是 [1,6]。只有一对:(1,6),且包含了奇偶性不同的数字。因此这个查询的答案是 true。

提示:

1 <= nums.length <= 105

1 <= nums[i] <= 105

1 <= queries.length <= 105

queries[i].length == 2

0 <= queries[i][0] <= queries[i][1] <= nums.length - 1

题解:
方法:记录最近一次奇偶性相同的位置
        

class Solution {
    public boolean[] isArraySpecial(int[] nums, int[][] queries) {
        int n = nums.length;
        int[] lastSame = new int[n];
        for (int i = 1; i < n; i++) {
            lastSame[i] = nums[i - 1] % 2 == nums[i] % 2 ? i : lastSame[i - 1];
        }
        boolean[] ans = new boolean[queries.length];
        for (int i = 0; i < queries.length; i++) {
            int[] q = queries[i];
            ans[i] = lastSame[q[1]] <= q[0];
        }
        return ans;
    }
}


15.矩阵中的最大得分

题目链接:3148. 矩阵中的最大得分

给你一个由 正整数 组成、大小为 m x n 的矩阵 grid。你可以从矩阵中的任一单元格移动到另一个位于正下方或正右侧的任意单元格(不必相邻)。从值为 c1 的单元格移动到值为 c2 的单元格的得分为 c2 - c1 。

你可以从 任一 单元格开始,并且必须至少移动一次。

返回你能得到的 最大 总得分。

示例 1:

在这里插入图片描述

输入:grid = [[9,5,7,3],[8,9,6,1],[6,7,14,3],[2,5,3,1]]

输出:9

解释:从单元格 (0, 1) 开始,并执行以下移动:

  • 从单元格 (0, 1) 移动到 (2, 1),得分为 7 - 5 = 2 。
  • 从单元格 (2, 1) 移动到 (2, 2),得分为 14 - 7 = 7 。 总得分为 2 + 7 = 9 。

示例 2:

在这里插入图片描述

输入:grid = [[4,3,2],[3,2,1]]

输出:-1

解释:从单元格 (0, 0) 开始,执行一次移动:从 (0, 0) 到 (0, 1) 。得分为 3 - 4 = -1 。

提示:

m == grid.length

n == grid[i].length

2 <= m, n <= 1000

4 <= m * n <= 105

1 <= grid[i][j] <= 105

题解:
方法:动态规划
        

class Solution {
    public int maxScore(List<List<Integer>> grid) {
        int m = grid.size(), n = grid.get(0).size();
        final int inf = 1 << 30;
        int ans = -inf;
        int[][] f = new int[m][n];
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                int mi = inf;
                if (i > 0) {
                    mi = Math.min(mi, f[i - 1][j]);
                }
                if (j > 0) {
                    mi = Math.min(mi, f[i][j - 1]);
                }
                ans = Math.max(ans, grid.get(i).get(j) - mi);
                f[i][j] = Math.min(grid.get(i).get(j), mi);
            }
        }
        return ans;
    }
}

16.划分数组得到最小的值之和

题目链接:3117. 划分数组得到最小的值之和

给你两个数组 nums 和 andValues,长度分别为 n 和 m。

数组的 值 等于该数组的 最后一个 元素。

你需要将 nums 划分为 m 个 不相交的连续
子数组
,对于第 ith 个子数组 [li, ri],子数组元素的按位 AND 运算结果等于 andValues[i],换句话说,对所有的 1 <= i <= m,nums[li] & nums[li + 1] & … & nums[ri] == andValues[i] ,其中 & 表示按位 AND 运算符。

返回将 nums 划分为 m 个子数组所能得到的可能的 最小 子数组 值 之和。如果无法完成这样的划分,则返回 -1 。

示例 1:

输入: nums = [1,4,3,3,2], andValues = [0,3,3,2]

输出: 12

解释:

唯一可能的划分方法为:

[1,4] 因为 1 & 4 == 0

[3] 因为单元素子数组的按位 AND 结果就是该元素本身

[3] 因为单元素子数组的按位 AND 结果就是该元素本身

[2] 因为单元素子数组的按位 AND 结果就是该元素本身

这些子数组的值之和为 4 + 3 + 3 + 2 = 12

示例 2:

输入: nums = [2,3,5,7,7,7,5], andValues = [0,7,5]

输出: 17

解释:

划分 nums 的三种方式为:

[[2,3,5],[7,7,7],[5]] 其中子数组的值之和为 5 + 7 + 5 = 17

[[2,3,5,7],[7,7],[5]] 其中子数组的值之和为 7 + 7 + 5 = 19

[[2,3,5,7,7],[7],[5]] 其中子数组的值之和为 7 + 7 + 5 = 19

子数组值之和的最小可能值为 17

示例 3:

输入: nums = [1,2,3,4], andValues = [2]

输出: -1

解释:

整个数组 nums 的按位 AND 结果为 0。由于无法将 nums 划分为单个子数组使得元素的按位 AND 结果为 2,因此返回 -1。

提示:

1 <= n == nums.length <= 104

1 <= m == andValues.length <= min(n, 10)

1 <= nums[i] < 105

0 <= andValues[j] < 105

题解:
方法:记忆化搜索
        

class Solution {
    private int[] nums;
    private int[] andValues;
    private final int inf = 1 << 29;
    private Map<Long, Integer> f = new HashMap<>();

    public int minimumValueSum(int[] nums, int[] andValues) {
        this.nums = nums;
        this.andValues = andValues;
        int ans = dfs(0, 0, -1);
        return ans >= inf ? -1 : ans;
    }

    private int dfs(int i, int j, int a) {
        if (nums.length - i < andValues.length - j) {
            return inf;
        }
        if (j == andValues.length) {
            return i == nums.length ? 0 : inf;
        }
        a &= nums[i];
        if (a < andValues[j]) {
            return inf;
        }
        long key = (long) i << 36 | (long) j << 32 | a;
        if (f.containsKey(key)) {
            return f.get(key);
        }

        int ans = dfs(i + 1, j, a);
        if (a == andValues[j]) {
            ans = Math.min(ans, dfs(i + 1, j + 1, -1) + nums[i]);
        }
        f.put(key, ans);
        return ans;
    }
}

17.K 周期字符串需要的最少操作次数

题目链接:3137. K 周期字符串需要的最少操作次数

给你一个长度为 n 的字符串 word 和一个整数 k ,其中 k 是 n 的因数。

在一次操作中,你可以选择任意两个下标 i 和 j,其中 0 <= i, j < n ,且这两个下标都可以被 k 整除,然后用从 j 开始的长度为 k 的子串替换从 i 开始的长度为 k 的子串。也就是说,将子串 word[i…i + k - 1] 替换为子串 word[j…j + k - 1] 。

返回使 word 成为 K 周期字符串 所需的 最少 操作次数。

如果存在某个长度为 k 的字符串 s,使得 word 可以表示为任意次数连接 s ,则称字符串 word 是 K 周期字符串 。例如,如果 word == “ababab”,那么 word 就是 s = “ab” 时的 2 周期字符串 。

示例 1:

输入:word = “leetcodeleet”, k = 4

输出:1

解释:可以选择 i = 4 和 j = 0 获得一个 4 周期字符串。这次操作后,word 变为 “leetleetleet” 。

示例 2:

输入:word = “leetcoleet”, k = 2

输出:3

解释:可以执行以下操作获得一个 2 周期字符串。

ijword
02etetcoleet
40etetetleet
60etetetetet

提示:

1 <= n == word.length <= 105

1 <= k <= word.length

k 能整除 word.length 。

word 仅由小写英文字母组成。

题解:
方法:统计子串个数
        

class Solution {
    public int minimumOperationsToMakeKPeriodic(String word, int k) {
        int n = word.length();
        int mx = 0;
        HashMap<String, Integer> cnt = new HashMap<>();
        for (int i = k; i <= n; i += k) {
            String sub = word.substring(i - k, i);
            int c = cnt.merge(sub, 1, Integer::sum); // c = ++cnt[sub]
            mx = Math.max(mx, c);
        }
        return n / k - mx;
    }
}

18.学生出勤记录 I

题目链接:551. 学生出勤记录 I

给你一个字符串 s 表示一个学生的出勤记录,其中的每个字符用来标记当天的出勤情况(缺勤、迟到、到场)。记录中只含下面三种字符:

‘A’:Absent,缺勤
‘L’:Late,迟到
‘P’:Present,到场
如果学生能够 同时 满足下面两个条件,则可以获得出勤奖励:

按 总出勤 计,学生缺勤(‘A’)严格 少于两天。
学生 不会 存在 连续 3 天或 连续 3 天以上的迟到(‘L’)记录。
如果学生可以获得出勤奖励,返回 true ;否则,返回 false 。

示例 1:

输入:s = “PPALLP”

输出:true

解释:学生缺勤次数少于 2 次,且不存在 3 天或以上的连续迟到记录。

示例 2:

输入:s = “PPALLL”

输出:false

解释:学生最后三天连续迟到,所以不满足出勤奖励的条件。

提示:

1 <= s.length <= 1000

s[i] 为 ‘A’、‘L’ 或 ‘P’

题解:
方法:数学
        

class Solution {
    public boolean checkRecord(String s) {
        int cntA = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == 'A' && ++cntA > 1) {
                return false;
            }
        }
        return !s.contains("LLL");
    }
}

下接:【题解】—— LeetCode一周小结34


  • 18
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZShiJ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值