力扣第309场周赛

115 篇文章 3 订阅
35 篇文章 3 订阅

LeetCode 2399. 检查相同字母间的距离

class Solution {
public:
    bool checkDistances(string s, vector<int>& distance) {
        map<char,vector<int>>mp;
        for(int i=0;i<s.size();i++)
            mp[s[i]].push_back(i);
        for(int i=0;i<distance.size();i++)
            if(mp[i+'a'].size()>=2&&mp[i+'a'][1]-mp[i+'a'][0]-1!=distance[i])return false;
        return true;
    }
};

LeetCode 2400. 恰好移动 k 步到达某一位置的方法数目

方法一数学

typedef long long LL;
class Solution {
public:
    static const int mod=1e9+7;
    int qmi(int a,int b)
    {
        LL res=1;
        while(b)
        {
            if(b&1)res=res*a%mod;
            b>>=1;
            a=(LL)a*a%mod;
        }
        return res;
    }
    int numberOfWays(int startPos, int endPos, int k) {
        if((abs(startPos-endPos)-k)%2||abs(startPos-endPos)>k)return 0;
        int r=(k+abs(startPos-endPos))/2;
        int res=1;
        for(int i=k;i>k-r;i--)
            res=(LL)res*i%mod;
        for(int i=1;i<=r;i++)
            res=(LL)res*qmi(i,mod-2)%mod;
        return res;
    }
};

方法二DP

class Solution {
public:
    static const int mod=1e9+7;
    static const int N=1010,M=3010;
    int dp[N][M];
    int numberOfWays(int startPos, int endPos, int k) {
        startPos+=1010,endPos+=1010;
        dp[0][startPos]=1;
        for(int i=1;i<=k;i++)
        {
            for(int j=0;j<M;j++)
            {
                if(j>0)dp[i][j]=(dp[i][j]+dp[i-1][j-1])%mod;
                if(j+1<M)dp[i][j]=(dp[i][j]+dp[i-1][j+1])%mod;
            }
        }
        return dp[k][endPos];
    }
};

LeetCode 2401. 最长优雅子数组

class Solution {
public:
    int longestNiceSubarray(vector<int>& nums) {
        int state[35]={0};
        int res=0;
        for(int i=0,j=0,cnt=0;i<nums.size();i++)
        {
            for(int k=0;k<31;k++)
            {
                if(nums[i]>>k&1)
                {
                    if(++state[k]>1)
                        cnt++;
                }
            }
            while(cnt)
            {
                for(int k=0;k<31;k++)
                {
                    if(nums[j]>>k&1)
                    {
                        if(--state[k]==1)cnt--;
                    }
                }
                j++;
            }
            res=max(res,i-j+1);
        }
        return res;
    }
};

LeetCode 2402. 会议室 III

typedef long long LL;
typedef pair<LL,int>PLI;
#define x first
#define y second
class Solution {
public:
    int mostBooked(int n, vector<vector<int>>& meetings) {
        int m=meetings.size();
        priority_queue<int,vector<int>,greater<int>>heap;
        priority_queue<PLI,vector<PLI>,greater<PLI>>rooms;
        sort(meetings.begin(),meetings.end());
        for(int i=0;i<n;i++)heap.push(i);
        vector<int>cnt(n);
        for(auto p:meetings)
        {
            while(rooms.size()&&rooms.top().x<=p[0])
            {
                heap.push(rooms.top().y);
                rooms.pop();
            }
            if(heap.size())
            {
                auto t=heap.top();
                heap.pop();
                cnt[t]++;
                rooms.push({p[1],t});
            }
            else
            {
                auto t=rooms.top();
                rooms.pop();
                cnt[t.y]++;
                rooms.push({t.x+p[1]-p[0],t.y});
            }
        }
        int res=0;
        for(int i=1;i<n;i++)
            if(cnt[i]>cnt[res])res=i;
        return res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leimingzeOuO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值