Leetcode 第 124 场双周赛题解
- Leetcode 第 124 场双周赛题解
- 题目1:3038. 相同分数的最大操作数目 I
- 思路
- 代码
- 复杂度分析
- 题目2:3039. 进行操作使字符串为空
- 思路
- 代码
- 复杂度分析
- 题目3:3040. 相同分数的最大操作数目 II
- 思路
- 代码
- 复杂度分析
- 题目4:3041. 修改数组后最大化数组中的连续元素数目
- 思路
- 代码
- 复杂度分析
Leetcode 第 124 场双周赛题解
题目1:3038. 相同分数的最大操作数目 I
思路
设 score = nums[0] + nums[1],count = 1。
从 i=2 开始遍历数组,如果 nums[i] + nums[i + 1] == score,count++;若不等或数字不够,跳出。
返回 count。
代码
/** @lc app=leetcode.cn id=3038 lang=cpp** [3038] 相同分数的最大操作数目 I*/// @lc code=start
class Solution
{
public:int maxOperations(vector<int> &nums){if (nums.size() < 2)return 0;int count = 1;int score = nums[0] + nums[1];int n = nums.size(), i = 2;while (i + 1 < n){if (nums[i] + nums[i + 1] == score){count++;i += 2;}elsebreak;}return count;}
};
// @lc code=end
复杂度分析
时间复杂度:O(n),其中 n 是数组 nums 的长度。
空间复杂度:O(1)。
题目2:3039. 进行操作使字符串为空
思路
操作的定义:每次操作依次遍历 ‘a’ 到 ‘z’,如果当前字符出现在 s 中,那么删除出现位置最早的该字符(如果存在的话)。
最后一次操作之前的字符串 s 是出现次数最多的字符(可能不止一个)按最后出现位置下标从小到大排列的结果。
我们用一个哈希表 cnt 记录各字符的出现次数,一个数组 lastIndex 记录每个字符最后一次出现的位置下标。
设 max_cnt = *max_element(cnt.begin(), cnt.end()),用一个数组 idx 记录 cnt 中元素值等于 max_cnt 的对应位置下标,则 idx 里存储的就是答案字符串的字符在原字符串的最后一次出现的位置下标。
答案 ans 的长度就是数组 idx 的元素个数。
对 idx 进行增序排序,遍历数组 idx,则 ans[i] 即为 s[idx[i]]。
最后返回 ans。
代码
/** @lc app=leetcode.cn id=3039 lang=cpp** [3039] 进行操作使字符串为空*/// @lc code=start// 哈希 + 排序class Solution
{
public:string lastNonEmptyString(string s){if (s.empty())return "";vector<int> cnt(26, 0); // 字符的出现次数vector<int> lastIndex(26, -1); // 字符的最后下标for (int i = 0; i < s.length(); i++){char c = s[i];cnt[c - 'a']++;lastIndex[c - 'a'] = i;}int max_cnt = *max_element(cnt.begin(), cnt.end());vector<int> idx;for (int i = 0; i < 26; i++){if (cnt[i] == max_cnt)idx.push_back(lastIndex[i]);}sort(idx.begin(), idx.end());string ans(idx.size(), 0);for (int i = 0; i < idx.size(); i++)ans[i] = s[idx[i]];return ans;}
};
// @lc code=end
复杂度分析
时间复杂度:O(n+∣Σ∣log∣Σ∣),其中 其中 n 为字符串s 的长度,∣Σ∣ 为字符集合的大小,本题中字符均为小写字母,所以 ∣Σ∣=26。
空间复杂度:O(∣Σ∣),其中 ∣Σ∣ 为字符集合的大小,本题中字符均为小写字母,所以 ∣Σ∣=26。
题目3:3040. 相同分数的最大操作数目 II
思路
区间 DP。
题解:区间 DP 的套路(Python/Java/C++/Go)
注意使用记忆化搜索,不然会超时。
代码
/** @lc app=leetcode.cn id=3040 lang=cpp** [3040] 相同分数的最大操作数目 II*/// @lc code=start// 区间 DPclass Solution
{
public:int maxOperations(vector<int> &nums){if (nums.size() < 2)return 0;int n = nums.size();int memo[n][n];auto helper = [&](int i, int j, int target) -> int{memset(memo, -1, sizeof(memo));function<int(int, int)> dfs = [&](int i, int j) -> int{if (i >= j)return 0;int &res = memo[i][j]; // 注意这里是引用if (res != -1)return res;res = 0;if (nums[i] + nums[i + 1] == target)res = max(res, dfs(i + 2, j) + 1);if (nums[j - 1] + nums[j] == target)res = max(res, dfs(i, j - 2) + 1);if (nums[i] + nums[j] == target)res = max(res, dfs(i + 1, j - 1) + 1);return res;};return dfs(i, j);};// 最前面两个int res1 = helper(2, n - 1, nums[0] + nums[1]);// 最后两个int res2 = helper(0, n - 3, nums[n - 2] + nums[n - 1]);// 第一个和最后一个int res3 = helper(1, n - 2, nums[0] + nums[n - 1]);int ans = max({res1, res2, res3}) + 1; // 加上第一次操作return ans;}
};
// @lc code=end
复杂度分析
时间复杂度:O(n2),其中 n 为数组 nums 的长度。
空间复杂度:O(n2),其中 n 为数组 nums 的长度。
题目4:3041. 修改数组后最大化数组中的连续元素数目
思路
子序列 DP。
题解:本题最简单写法(Python/Java/C++/Go)
代码
/** @lc app=leetcode.cn id=3041 lang=cpp** [3041] 修改数组后最大化数组中的连续元素数目*/// @lc code=start// 子序列 DPclass Solution
{
public:int maxSelectedElements(vector<int> &nums){if (nums.empty())return 0;// dp[i]: 以 i 结尾的连续递增序列的最大长度vector<int> dp(1e6 + 1, 0);sort(nums.begin(), nums.end());// 状态转移for (int &x : nums){dp[x + 1] = dp[x] + 1;dp[x] = dp[x - 1] + 1;}return *max_element(dp.begin(), dp.end());}
};
// @lc code=end
复杂度分析
时间复杂度:O(nlogn),其中 n 为数组 nums 的长度。
空间复杂度:O(N),数据范围 N=1e6。