第 272 场周赛(模拟+模拟+dp+LIS二分解法)

1.模拟

class Solution {
public:
    string firstPalindrome(vector<string>& words) {
        for(auto& s:words){
            int l = 0, r = s.size() - 1;
            int flag = 1;
            while(l < r){
                if(s[l] != s[r]){
                    flag = 0;
                    break;
                }
                l++;
                r--;
            }
            if(flag == 1) return s;
        }
        return "";
    }
};

2.模拟

class Solution {
public:
    string addSpaces(string s, vector<int>& spaces) {
        string ans;
        int loc = 0;
        for(int i = 0; i < spaces.size(); i++){
            while(loc < spaces[i]){
                ans += s[loc++];
            }
            ans += ' ';
        }
        while(loc < s.size()){
            ans += s[loc++];
        }
        return ans;
    }
};

3.dp

class Solution {
public:
    long long getDescentPeriods(vector<int>& p) {
        long long ans = 0;
        int n = p.size();
        vector<int> dp(n, 1);
        for(int i = 1; i < n; i++){
            if(p[i - 1] - p[i] == 1){
                dp[i] = dp[i - 1] + 1;
            }
        }
        for(int i = 0; i < n; i++){
            ans += dp[i];
        }
        return ans;
    }
};

4.LIS二分解法

class Solution {
public:
    /*int lengthOfLIS(vector<int>& nums) {
        vector<int> minnums;
        for(int v : nums)
        {
            auto pos = upper_bound(minnums.begin(), minnums.end(), v);
            if(pos == minnums.end()) {
                minnums.push_back(v);
            } else {
                *pos = v;
            }
        }
        return minnums.size();
    }*/

    int kIncreasing(vector<int>& a, int k) {
        int n = a.size();
        //cout << n << endl;
        int ans = 0;
        //int nn = n / k;
        //if(n % k != 0) nn++;
        vector<int> dp(n, 1);
        for(int begin = 0; begin < k && begin + k < n; begin++){
            
            int cnt = 0;
            vector<int> minnums;
            for(int i = begin; i < n; i += k){
                cnt++;
                auto pos = upper_bound(minnums.begin(), minnums.end(), a[i]);
                if(pos == minnums.end()) {
                    minnums.push_back(a[i]);
                } else {
                    *pos = a[i];
                }
            }
            //cout << " " << mmax << endl;
            ans += (cnt - minnums.size());
        }
        return ans;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Prince_H_23

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

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

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

打赏作者

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

抵扣说明:

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

余额充值