LeetCode第238场周赛记录

在这里插入图片描述

1 1837. K 进制表示下的各位数字总和

在这里插入图片描述

思路:位运算

class Solution {
public:
    int sumBase(int n, int k) {
        int res = 0;
        while(n)
        {
            res += (n % k);
            n /= k;
        }
        return res;
    }
};



2 1838. 最高频元素的频数

在这里插入图片描述
在这里插入图片描述

思路:

  1. 贪心:排序后当前元素左边且离当前元素越近的元素改变成当前元素大小的代价越小。
  2. 遍历所有元素,通过二分找到修改区间(将区间内原数组的值都改为当前元素值)左端点位置。
  3. 利用前缀和将判断区间是否能修改的时间复杂度降低为o(1)。
typedef long long LL;
class Solution {
public:
    int maxFrequency(vector<int>& nums, int k) {
        int res = 0;
        int n = nums.size();
        sort(nums.begin(), nums.end());
        vector<LL>s(n + 1, 0);
        for(int i = 1; i <= n; i++)s[i] = s[i - 1] + nums[i - 1];
        for(int i = 0; i < n; i++)
        {
            while(i + 1 < n && nums[i] == nums[i + 1])i++;//连续个相同大小元素只要处理最右边的那个
            int cur = nums[i], p = i + 1;
            int l = 1, r = p;
            while(l < r)
            {
                int mid = l + r >> 1;
                if(s[p] - s[mid - 1] + k >= (LL)(p - mid + 1) * cur)r = mid;
                else l = mid + 1;
            }
            res = max(res, p - l + 1);
        }
        return res;
    }
};



3 1839. 所有元音按顺序排布的最长子字符串

在这里插入图片描述
在这里插入图片描述

思路:(本题不难,更像是一个模拟题,做这题的时候没看到要求必须连续)

  1. 用变量cnt维护合法区间字母个数,type维护区间字母种类,cur指向当前访问的字符,剩下的操作请看代码和注释。
  2. 用map来简化判断(判断当前字符是否能衔接到合法区间尾部)。
class Solution {
public:
    int longestBeautifulSubstring(string word) {
        map<char, int>has;
        has['a'] = 0, has['e'] = 1, has['i'] = 2, has['o'] = 3, has['u'] = 4;
        int n = word.size();
        int res = 0;
        int cnt = 0, type = 0;
        char cur;
        for(int i = 0; i < n; i++)
        {
        	//找到'a'作为合法区间起点
            while(type == 0 && word[i]!= 'a' && i < n)i++;
            if(type == 0)type++, cur= 'a';
            //当前字符与合法区间尾部相等,则将其附加到合法区间
            if(word[i] == cur)cnt++;
            else
            {
                if(has[word[i]] - has[cur] != 1)//无法衔接则在下次循环中重新找'a',合法区间消失
                {
                    cnt = type = 0;
                    i--;
                }
                else//能衔接则更新cur,种类和数量都要增加
                {
                    type++;
                    cnt++;
                    cur = word[i];
                }
            }
            if(type == 5)res = max(res, cnt);
        }
        return res;
    }
};



4 1840. 最高建筑高度

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
参考:题解

思路:

  1. 要认识到对一个楼的限制其实也是对所有楼的限制(因为有条件:相邻两楼的高度差不超过1)。
  2. 限制是可以传递的,楼层最大允许高度去其所有限制的最小值。
  3. 题目要求最高楼高度,则被限制楼层的高度就等于其最大允许高度
  4. 线性遍历所有楼会超时,所以只要遍历restrictions数组对应楼;答案在其他楼的情况可以用方程求出。
class Solution {
public:
    int maxBuilding(int n, vector<vector<int>>& rt) {
        rt.push_back({1, 0});
        sort(rt.begin(), rt.end());
        if(rt.back()[0] != n)rt.push_back({n, n - 1});
        int m = rt.size();
        //从左到右传递限制
        for(int i = 1; i < m; i++)
            rt[i][1] = min(rt[i][1], rt[i - 1][1] + (rt[i][0] - rt[i - 1][0]));
        //从右到左传递限制
        for(int i = m - 2; i >= 0; i--)
            rt[i][1] = min(rt[i][1], rt[i + 1][1] + (rt[i + 1][0] - rt[i][0]));
        //更新答案
        int res = 0;
        for(int i = 0; i < m - 1; i++)
            res = max(res, (rt[i][1] + rt[i + 1][1] + rt[i + 1][0] - rt[i][0]) / 2);
        return res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值