【LeetCode】力扣周赛题目总结


在这里插入图片描述

1. Leetcode 3085 成为 K 特殊字符串需要删除的最少字符数


本题思考的关键点:

① 在字符串处理放到 cnt[26] 数组中,枚举删除字母后,字母出现的最少次数
逆向思维,直接求最多保留多少个字母

  • 本题示例代码如下:
class Solution {
public:
    int minimumDeletions(string word, int k) {

    int cnt[26] = {0};  
    for (auto x : word)
        cnt[x - 'a'] ++;
        
    sort(cnt, cnt + 26);

    int max_sum = 0;   // 逆向思维,求出最多保留多少个字母的数量

    // 第一层枚举保留的字母  出现的最少次数
    for (int i = 0; i < 26; i ++)
    {
        int sum  = 0;
        // 比字母 i 对应的次数小的字母都要删除
        for (int j = i; j < 26; j ++)
        {
            sum += min(cnt[j], cnt[i] + k);
        }
        max_sum = max(max_sum, sum);
    }
    return word.length() - max_sum;
    }
};


2. Leetcode 846 一手顺子


本题思考的关键点:[双指针 + 枚举]

从小到大进行枚举,枚举当前组的第一个数

② 不断的维护当前的个数即可

  • 本题示例代码如下:
class Solution {
public:
    bool isNStraightHand(vector<int>& hand, int groupSize) {

    if (hand.size() % groupSize != 0)
        return false;

    sort(hand.begin(), hand.end());

    unordered_map<int, int> mymap;
    for (auto x : hand)
        mymap[x] ++;

		// 枚举当前组的第一个数
    for (int i = 0; i < hand.size(); i ++)
    {
        if (mymap[hand[i]] <= 0)  continue;

        int j = hand[i];
        int cnt = 0;
        while (cnt < groupSize)
        {
            cnt ++;
            if (mymap[j] > 0)
            {
                mymap[j] --;
                j ++;
            }
            else 
                return false;
        }
    }
    return true;
    }
};


3. Leetcode 2044 统计按位或能得到最大值的子集数目


本题思考的关键点:[回溯 + 暴搜]

如何枚举,将所有子集的情况都统计进去

② 先把最大的子集异或值求出来,然后想清楚剩下的有多少种

③ 这个暴搜考虑的是选或者不选的

  • 本题示例代码如下:
// 数据范围: n ≤ 30,指数级别, 所以用 dfs + 剪枝  

class Solution {
public:
    int ans = 0;
    int countMaxOrSubsets(vector<int>& nums) {
    
    // 首先算出子集按位或得出的最大值
    int mmax = 0;
    for (auto x : nums)
        mmax |= x;

    // 进行暴搜,注意:此时的 cur 为 0
    dfs(nums, mmax, 0, 0);
    return ans;
    }

    void dfs(vector<int>& nums, int mmax, int index, int cur)
    {
        // 剪枝
        if (cur == mmax)
        {
            ans += (1<<(nums.size() - index));
            return ;
        }

        if (index == nums.size())
            return ;

        dfs(nums, mmax, index + 1, cur | nums[index]);
        dfs(nums, mmax, index + 1, cur);
    }
};


相关推荐

  1. 375

    2024-03-18 19:18:02       54 阅读
  2. 376

    2024-03-18 19:18:02       55 阅读
  3. 381

    2024-03-18 19:18:02       54 阅读
  4. 382

    2024-03-18 19:18:02       42 阅读
  5. 119双

    2024-03-18 19:18:02       63 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-18 19:18:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-18 19:18:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-18 19:18:02       82 阅读
  4. Python语言-面向对象

    2024-03-18 19:18:02       91 阅读

热门阅读

  1. 【Python】继承会遇到的问题

    2024-03-18 19:18:02       40 阅读
  2. 大车error

    2024-03-18 19:18:02       41 阅读
  3. 数通-路由技术基础介绍

    2024-03-18 19:18:02       41 阅读
  4. React全家桶及原理解析-lesson4-Redux

    2024-03-18 19:18:02       42 阅读
  5. leetcode513找树左下角的值

    2024-03-18 19:18:02       41 阅读
  6. 【C语言】打印闰年

    2024-03-18 19:18:02       40 阅读
  7. 【工具篇】Unity翻书效果插件Book-Page Curl Pro教程

    2024-03-18 19:18:02       39 阅读
  8. unity学习笔记 Restsharp 使用心得

    2024-03-18 19:18:02       44 阅读
  9. 一种不需要客户端ip的命令行远程工具

    2024-03-18 19:18:02       42 阅读