【LeetCode周赛】第 384 场周赛

目录

  • [3033. 修改矩阵 简单](#3033. 修改矩阵 简单)
  • [3034. 匹配模式数组的子数组数目 I 中等](#3034. 匹配模式数组的子数组数目 I 中等)
  • [3035. 回文字符串的最大数量 中等](#3035. 回文字符串的最大数量 中等)
  • [3036. 匹配模式数组的子数组数目 II 困难](#3036. 匹配模式数组的子数组数目 II 困难)

3033. 修改矩阵 简单

3033. 修改矩阵

分析:

获取每一列的最大元素,将矩阵中的 -1 替换成每一列的最大元素。

代码:

cpp 复制代码
class Solution {
public:
    vector<vector<int>> modifiedMatrix(vector<vector<int>>& matrix) {
        int m=matrix.size(), n=matrix[0].size();
        vector<int> x,y;
        for(int i=0;i<n;i++){
            int t = -1;
            x.clear(),y.clear();
            for(int j=0;j<m;j++){
                if(matrix[j][i]==-1) x.push_back(j),y.push_back(i);
                else t=max(t,matrix[j][i]);
            }
            for(int k=0;k<x.size();k++){
                matrix[x[k]][y[k]]=t;
            }
        }
        return matrix;
    }
};

3034. 匹配模式数组的子数组数目 I 中等

3034. 匹配模式数组的子数组数目 I

分析:

根据 nums 中各个元素的值,计算得到其对应的 匹配模式数组 nums_pattern

再在 nums_pattern 中寻找子数组 pattern 的数量。

代码:

cpp 复制代码
class Solution {
public:
    int countMatchingSubarrays(vector<int>& nums, vector<int>& pattern) {
        int n=nums.size(), m=pattern.size(), ans=0;
        vector<int> cnt;
        for(int i=1;i<n;i++){
            if(nums[i]>nums[i-1]) cnt.push_back(1);
            else if(nums[i]==nums[i-1]) cnt.push_back(0);
            else cnt.push_back(-1);
        }
        for(int i=0;i<cnt.size()-m+1;i++){
            for(int j=0;j<m;j++){
                if(cnt[i+j]!=pattern[j]) break;
                if(j==m-1) ans++;
            }
        }
        return ans;
    }
};

3035. 回文字符串的最大数量 中等

3035. 回文字符串的最大数量

分析:

题目求的是最终为回文字符串的数量,并非最小的交换次数,因此转换思维直接 构造 回文字符串。

  • 首先统计所有字符串中各个小写字母的出现次数,同时记录各个字符串的长度。
  • 对字符串长度数组进行升序排序(构造长的回文字符串时,可能会导致多个短的字符串构不成回文字符串)。
  • 利用贪心思维构造回文字符串
    • 正常情况下,一次直接填入偶数个字符保证字符串为回文字符串。
    • 如果字符串长度为奇数,不能填入偶数字符时,计数数组 cnt 中寻找是否有剩余次数为奇数的字符,否则返回还没使用完的字符。
  • 对于 能直接使用偶数字符对填充完毕 ,或者 剩一个字符的字符串,最终都能构成回文字符串,此时数量 +1。最终即为回文字符串的最大数量。

代码:

cpp 复制代码
class Solution {
public:
    int findOdd(int cnt[]){
        int ans=-1;
        for(int i=0;i<26;i++){
            if(cnt[i]%2==1) return i;
            else if(cnt[i]>0) ans=i;
        }
        return ans;
    }

    int maxPalindromesAfterOperations(vector<string>& words) {
        int cnt[26], ans=0;memset(cnt, 0, sizeof(cnt));
        vector<int> t;
        for(auto word : words){
            for(auto i : word){
                cnt[i - 'a']++;
            }
            t.push_back(word.length());
        }
        sort(t.begin(), t.end());
        for(int i : t){
            int tmp = i;
            for(int j=0;j<26&&tmp>1;j++){
                int tt = tmp/2,tc = cnt[j]/2;
                if(tt*2<=cnt[j]){
                    tmp-=tt*2;
                    cnt[j]-=tt*2;
                }else{
                    tmp-=tc*2;
                    cnt[j]-=tc*2;
                }
            }
            if(tmp==0){ans++;continue;}
            if(tmp==1){
                cnt[findOdd(cnt)]--;ans++;}
            else{
                int j=0;
                while(tmp){
                    if(cnt[j]>0){
                        if(cnt[j]>=tmp) cnt[j]-=tmp,tmp=0;
                        else tmp-=cnt[j],cnt[j]=0;
                    }
                    j++;
                }
            }
        }
        return ans;
    }
};

更加巧妙方法:利用位运算。

cpp 复制代码
class Solution {
public:
    int maxPalindromesAfterOperations(vector<string>& words) {
        int ans = 0, tot = 0, mask = 0;
        for (auto &w : words) {
            tot += w.length();
            for (char c : w) {
                mask ^= 1 << (c - 'a'); // 对于出现奇数次的字符,对应位为1,否则为0
            }
        }
        tot -= __builtin_popcount(mask); // 减去无法构成相同字符对的字符(单个的字符)
        cout<<tot<<endl;
        ranges::sort(words, [](const auto &a, const auto &b) {
            return a.length() < b.length();
        });
        for (auto &w : words) {
            tot -= w.length() / 2 * 2; // 长度为奇数的字符串,不考虑中间的单个字符
            if (tot < 0) break; // 如果相同字符对无法构建剩下的回文字符串,则跳出循环,此即为回文字符串的最大数量
            ans++;
        }
        return ans;
    }
};

3036. 匹配模式数组的子数组数目 II 困难

3036. 匹配模式数组的子数组数目 II

分析:

根据 nums 中各个元素的值,计算得到其对应的 匹配模式数组 nums_pattern

再在 nums_pattern 中寻找子数组 pattern 的数量,即数组的模式匹配,可以利用 KMP算法 解决。

代码:

cpp 复制代码
class Solution {
public:
    int countMatchingSubarrays(vector<int>& nums, vector<int>& pattern) {
        int n=nums.size(), m=pattern.size(), ans=0;
        vector<int> cnt, ne(m);
        for(int i=1;i<n;i++){
            if(nums[i]>nums[i-1]) cnt.push_back(1);
            else if(nums[i]==nums[i-1]) cnt.push_back(0);
            else cnt.push_back(-1);
        }
        ne[0]=-1;
        for(int i=1,j=-1;i<m;i++){
            while(j!=-1&&pattern[i]!=pattern[j+1]) j=ne[j];
            if(pattern[i]==pattern[j+1]) j++;
            ne[i]=j;
        }
        for(int i=0,j=-1;i<n-1;i++){
            while(j!=-1&&cnt[i]!=pattern[j+1]) j=ne[j];
            if(cnt[i]==pattern[j+1]) j++;
            if(j==m-1){
                ans++;
                j=ne[j];
            }
        }
        return ans;
    }
};
相关推荐
你撅嘴真丑13 分钟前
第九章-数字三角形
算法
uesowys23 分钟前
Apache Spark算法开发指导-One-vs-Rest classifier
人工智能·算法·spark
ValhallaCoder29 分钟前
hot100-二叉树I
数据结构·python·算法·二叉树
董董灿是个攻城狮32 分钟前
AI 视觉连载1:像素
算法
智驱力人工智能1 小时前
小区高空抛物AI实时预警方案 筑牢社区头顶安全的实践 高空抛物检测 高空抛物监控安装教程 高空抛物误报率优化方案 高空抛物监控案例分享
人工智能·深度学习·opencv·算法·安全·yolo·边缘计算
孞㐑¥2 小时前
算法——BFS
开发语言·c++·经验分享·笔记·算法
月挽清风2 小时前
代码随想录第十五天
数据结构·算法·leetcode
XX風2 小时前
8.1 PFH&&FPFH
图像处理·算法
NEXT062 小时前
前端算法:从 O(n²) 到 O(n),列表转树的极致优化
前端·数据结构·算法
代码游侠3 小时前
学习笔记——设备树基础
linux·运维·开发语言·单片机·算法