AcWing第58场周赛+力扣第300场周赛补题

 AcWing

一.寻找1

1.原题链接:4488. 寻找1 - AcWing题库

2.解题思路:

遍历数组,若数组中出现1,则将flag赋值为1,此时输出YES,否则输出NO。

3.参考代码:

#include<iostream>
#include<cstring>

using namespace std;
int main()
{
    int n,flag = 0;
    cin >> n;
    int b[100];
    for(int i = 0; i < n; i++)
    cin >> b[i];
    for(int i = 0; i < n ; i++)
    {
        if(b[i] == 1) flag = 1;
    }
    if(flag)cout << "YES";
    else cout << "NO";
    return 0;
}

二.最长子序列

1.原题链接:4489. 最长子序列 - AcWing题库

2.解题思路:

        如果数组a[i]中满足a[i + 1] <= 2 * a[i],则把b[i]赋值为1。最后求出b中连续1的个数中最大的值,+1就是答案。

3.参考代码:

#include <iostream>

using namespace std;
const int N = 200010;
int n, a[N];
bool b[N];
int main() {
    cin >> n;
    for (int i = 1;i <= n; i++) cin >> a[i];
    for (int i = 1; i < n; i++)
        if (a[i + 1] <= 2 * a[i]) b[i] = 1;
        else b[i] = 0;
    //标记是否可行
    int res = 0, ans = 0;
    for (int i = 1;i < n; i++) {
        if (b[i] == 0) continue;
        res = 1;
        while (b[i + 1] == 1) res++, i++;
        ans = max(ans, res);
    }
    cout << ans + 1;
    return 0;
}

力扣

一.解密消息

1.原题链接:力扣

2.解题思路:

        因为需要进行字母映射,所以使用hash表,首先遍历一次key,获得hash表,再遍历一遍message去获得答案

3.参考代码:

class Solution {
public:
    string decodeMessage(string key, string message) {
        string res;
        //hash 用来映射字母
        unordered_map<char, char>m;
        string s = "abcdefghijklmnopqrstuvwxyz"; //字母表
        int n = 0;
        for(auto& c : key)
        {
            //此字母没映射过,且不是空格
            if(m.count(c) == 0 && c != ' ')
            {
                m[c] = s[n];
                n++;
            }
        }
        //拼接答案
        for(auto& b : message)
        {
            if(b == ' ') res += ' ';
            else
                res += m[b];
        }
        return res;
    }
};

二.螺旋矩阵Ⅳ

1.原题链接:力扣

2.解题思路:

        边界压缩法。用up,down,left,right 分别表示上下左右4个边界,当我们完成一行后,就压缩对应的边界。

3.参考代码:

class Solution {
public:
    vector<vector<int>> spiralMatrix(int m, int n, ListNode* head) {
        //设置一个矩阵,默认值-1
        vector<vector<int>>res(m,vector<int>(n,-1));
        ListNode*p=head;
        //初始化上下左右4个边界
        int up=0,down=m-1;
        int left=0,right=n-1;
        int i=0;
        while(p)
        {
            for(i=left;i<=right;++i) //向右
            {
                res[up][i]=p->val;
                p=p->next;
                if(p==NULL) return res;
            }
            if(up<down) up++;

            for(i=up;i<=down;++i) //向下
            {
                res[i][right]=p->val;
                p=p->next;
                if(p==NULL) return res;
            }
            if(left<right) right--;

            for(i=right;i>=left;--i) //向左
            {
                res[down][i]=p->val;
                p=p->next;
                if(p==NULL) return res;
            }
            if(up<down) down--;

            for(i=down;i>=up;--i) //向上
            {
                res[i][left]=p->val;
                p=p->next;
                if(p==NULL) return res;
            }
            if(left<right) left++;
        }
        return res;
    }
};

三.知道秘密的人数

1.原题链接:力扣

2.解题思路:

        动态规划递推。记 f[n]为所求答案,那么对于第一个知道答案的人,他会在第 [1+delay, 1+forget)天内不断将秘密告诉新的人,而对于第 i 天知晓秘密的人,他产生的知道秘密的人即为f[n−i],因此得到递推公式(还要算上他自己是否还记得这个秘密):

3.参考代码:

const int mod = 1000000007;
class Solution {
public:
    int peopleAwareOfSecret(int n, int delay, int forget) {
        int f[1000];
        long long s = 0;
        for(int i = 0 ; i < delay ; i++) f[i] = 1;
        for(int i = delay ; i < forget ; i++)
        {
            s += f[i - delay];
            f[i] = (s + 1) % mod;
        }
        for(int i = forget ; i < n ; i++)
        {
            s += f[i - delay] - f[i - forget];
            f[i] = s % mod;
        }
        return f[n - 1];
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值