Leetcode周赛(266)

在这里插入图片描述

class Solution {
public:
    int countBalls(int lowLimit, int highLimit) {
         int res=-1;
         vector<int> s(50);
         for(int i=lowLimit;i<=highLimit;i++){
             int x = i;
             int sum=0;
             while(x){
                sum+=x%10;
                x/=10;
             }
             s[sum]++;
             res=max(res,s[sum]);
         } 

         return res;
    }
};

在这里插入图片描述
数组模拟邻接表,把链表看成一棵树,从父节点搜索的不要重复搜索父节点就可以了

const int N = 400010,B=100000;
int h[N],e[N],ne[N],idx=0;
int cnt[N];

class Solution {
public:

    vector<int> path;

    void add(int a,int b){
        e[idx]=b;
        ne[idx]=h[a];
        h[a]=idx++;
    }

    void dfs(int root,int father){
        path.push_back(root-B);
        for(int i=h[root];i!=-1;i=ne[i]){
            int j=e[i];
            if(j==father) continue;
            dfs(j,root);
        }
    }


    vector<int> restoreArray(vector<vector<int>>& adjacentPairs) {
        memset(cnt,0,sizeof cnt);
        memset(h,-1,sizeof h);
        for(auto& e:adjacentPairs){
            int a=e[0]+B;
            int b=e[1]+B;
            add(a,b),add(b,a);
            cnt[a]++,cnt[b]++;
        }

        int root=0;
        for(int i=0;i<N;i++){
            if(cnt[i]==1){
                root=i;
                break;
            }
        }

        dfs(root,-1);
        return path;

    }
};

在这里插入图片描述
前缀和预处理期望的种类的区间,由于每次可以吃1-c个苹果,所以可能的区间为d+1,(d+1)*c(加一的原因是因为d可以取0),然后判断两个区间是否有交集,有交集即可取到。

typedef long long LL;

class Solution {
public:

    bool check(LL a,LL b,LL c,LL d){
        if(b<c||d<a) return false;
        return true;
    }

    vector<bool> canEat(vector<int>& w, vector<vector<int>>& queries) {
        int n=w.size();
        vector<LL> s(n+1);
        for(int i=1;i<=n;i++){
            s[i]=s[i-1]+w[i-1];
        }
        vector<bool> res;
        for(auto& p : queries){
            int t = p[0],d=p[1],c=p[2];
            res.push_back(check(d+1,(LL)(d+1)*c,s[t]+1,s[t+1]));
        }

        return res;
    }
};

在这里插入图片描述

dp
三种可能 i==j 则必定是回文串
如果只有两个字符 则 s[i]==s[j] 才可以是回文串
如何判断[i,j]这个区间是否为回文串,只要保证[i+1][j-1]是回文串同s[i]==s[j]
最后注意i+1必须能推出i 所以i层循环需要倒着写.

class Solution {
public:
    bool checkPartitioning(string s) {
        int n = s.size();
        vector<vector<bool>> f(n,vector<bool>(n));

        for(int i=n-1;i>=0;i--){
            for(int j=i;j<n;j++){
                if(i==j) f[i][j]=true;
                else if(i+1==j) f[i][j]=s[i]==s[j];
                else f[i][j] = s[i]==s[j]&&f[i+1][j-1];
            }
        }

        for(int i=0;i<n;i++){
            for(int j=i+1;j+1<n;j++){
                if(f[0][i]&&f[i+1][j]&&f[j+1][n-1]) return true;
            }
        }
        return false;

    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

就是氧气c

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

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

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

打赏作者

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

抵扣说明:

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

余额充值