力扣119双周赛

2023-12-13 21:52:06

第 119 场双周赛

找到两个数组中的公共元素

模拟

class Solution {
public:
    vector<int> findIntersectionValues(vector<int>& nums1, vector<int>& nums2) {
        unordered_map<int,int> mp1 , mp2;
        vector<int>res(2);
        for(auto x : nums1){
            mp1[x] ++;
        }
        for(auto x : nums2){
            mp2[x] ++;
        }
        int k = 0;
        for(auto x : nums1){
            if(mp2[x] > 0)k ++;
        }
        res[0] = k;
        k = 0;
        for(auto x : nums2){
            if(mp1[x] > 0)k ++;
        }
        res[1] = k;
        return res;
    }
};

消除相邻近似相等字符

贪心,一个变了下一个肯定不用变

class Solution {
public:
    int removeAlmostEqualCharacters(string word) {
        int n = word.size() , ans = 0;
        for(int i = 1 ; i < n ; i ++)if(abs(word[i] - word[i-1]) <= 1)ans ++,i ++;
        return ans;
    }
};

最多 K 个重复元素的最长子数组

滑动窗扣维持k个

class Solution {
public:
    int maxSubarrayLength(vector<int>& nums, int k) {
        int n = nums.size();
        unordered_map<int,int>m;
        queue<int>q;
        int ans = 0;
        for(int i = 0 ; i < n ; i ++){
            m[nums[i]] ++;
            q.push(nums[i]);
            while(m[nums[i]] > k){
                int t = q.front();
                q.pop();
                m[t] --;
            }
            int l = q.size();
            ans = max(ans , l);
        }
        return ans;
    }
};

找到最大非递减数组的长度

二进制枚举+Floyd

class Solution {
public:
    static const int N = 11;
    int d[N][N];
    void init(){
        for(int i = 0 ; i < N ; i ++){
            for(int j = 0 ; j < N ; j ++){
                if(i == j)d[i][j] = 0;
                else d[i][j] = 99999;
            }
        }
    }
    int numberOfSets(int n, int md, vector<vector<int>>& roads) {
        int ans = 1 , cnt = roads.size();
        for(int i = 1 ; i <= ((1 << n) - 1) ; i ++){ //枚举
            //建图
            init();
            vector<int> c;
            vector<int> nt(15);
            for(int j = 0 ; j <= 10 ; j ++){
                if(i >> j & 1)c.push_back(j);
            }
            for(auto x : c)nt[x] = 1;
            for(int j = 0 ; j < cnt ; j ++){
                if(nt[roads[j][0]] == 1 && nt[roads[j][1]] == 1){
                    d[roads[j][0]][roads[j][1]] = min(d[roads[j][0]][roads[j][1]],roads[j][2]);
                    d[roads[j][1]][roads[j][0]] = min(d[roads[j][1]][roads[j][0]],roads[j][2]);                    
                }
            }
            function<bool()> check = [&]() -> bool{
                for (int kk = 0; kk < n; kk ++ )
                for (int ii = 0; ii < n; ii ++ )
                for (int jj = 0; jj < n; jj ++ )
                d[ii][jj] = min(d[ii][jj], d[ii][kk] + d[kk][jj]);
                for(int a = 0 ; a < c.size() ; a ++){
                    for(int b = 0 ; b < c.size() ; b ++){
                        if(d[c[a]][c[b]] > md){
                            return false;
                        }
                    }
                }
                return true;
            };
            
            if(check())ans ++;
        } 
        return ans;
    }
};

文章来源:https://blog.csdn.net/qq_60755751/article/details/134910899
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。