Leetcode周赛279

考点:前缀和,dp,模拟

对奇偶下标进行排序

【传送门】:https://leetcode-cn.com/problems/sort-even-and-odd-indices-independently/

thinking:

暴力模拟即可

solution:

class Solution {
public:
    vector<int> sortEvenOdd(vector<int>& nums) {
        int n=nums.size();
        vector<int> a;vector<int> b;
        for(int i=0;i<n;++i) {
            if(i%2) {
                a.push_back(nums[i]);
            } else {
                b.push_back(nums[i]);//偶数
            }
        }
        sort(a.begin(),a.end());
        sort(b.begin(),b.end());
        int lena=a.size()-1;int lenb=0;
        for(int i=0;i<n;++i) {
            if(i%2) {
                 nums[i]=a[lena];
                lena--;
            } else {
               
                nums[i]=b[lenb];
                lenb++;
            }
        }
        return nums;
    }
};

重排数字的最小值

【传送们】:https://leetcode-cn.com/problems/smallest-value-of-the-rearranged-number/

thinking:

简单的贪心即可,当num小于0,数字从大到小排,反之,从小到大排,注意别把0放前面就行

solution:

class Solution {
public:
    long long smallestNumber(long long num) {
        int a[10]={0};
        bool judge=num<0;//是否大于0
        if(judge) num=-num;
        string s=to_string(num);
        int n=s.size();
        for(int i=0;i<n;++i) {
            ++a[s[i]-'0'];
        }
        long long ans=0;
        if(judge) {
            for(int i=9;i>=0;--i) {
                while(a[i]) {
                    ans+=i*pow(10,n-1);
                    --n;
                    --a[i];
                }
            }
            ans=-ans;
        } else {
            int count=a[0];
            bool add=false;
            for(int i=1;i<=9;++i) {
                while(a[i]) {
                    if(add&count>0) {
                        --n;
                        --count;
                        continue;
                    }
                    ans+=i*pow(10,n-1);
                    --n;
                    --a[i];
                    add=true;
                }
            }
        }
        return ans;
    }
};

设计位集

【传送门】;https://leetcode-cn.com/problems/design-bitset/

thinking:

朴素的做法是直接模拟每一步的操作,不用想肯定tle,从题目可以看出,只有flip会影响我们的操作,我们不妨用数组来存下这个位集,,cnt1记录1的个数变量ok记录反转的次数,偶数倍就是没有反转,反之也很清除。下面考虑改值问题,我们不妨这样操作,当ok偶数时,我们认为原数组的每一位都是正确的,反之都是错误的,我们需要取反,这样,我们自己只需要在fix与unfix中维护原数组就可以了

solution:

class Bitset {
public:
    int cnt1;
    vector<int> a;
    int n;
    int ok=0;
    Bitset(int size) {
        cnt1=0;
        n=size;
        a.resize(size);
        for(int i=0;i<n;++i) {
            a[i]=0;
        }
    }
    
    void fix(int idx) {
        if(ok%2==0) {//没反转
            if(a[idx]==0) {
                a[idx]=1;
                ++cnt1;
            }
        } else  {
            if(a[idx]==1) {
                a[idx]=0;
                ++cnt1;
            }
        }
    }
    
    void unfix(int idx) {
        if(ok%2==0) {//没反转
            if(a[idx]==1) {
                a[idx]=0;
                --cnt1;
            }
        } else {
            if(a[idx]==0) {
                a[idx]=1;
                --cnt1;
            }
        }
    }
    
    void flip() {
        ++ok;
        cnt1=n-cnt1;
    }
    
    bool all() {
        return cnt1==n;
    }
    
    bool one() {
        return cnt1>0;
    }
    
    int count() {
        return cnt1;
    }
    
    string toString() {
        string ans;
        bool judge=ok%2==0;//没反转
        if(judge) {
            for(auto val:a) {
                ans+=val==0?'0':'1';
            }
        } else {
            for(auto val:a) {
                ans+=val==0?'1':'0';
            }
        }
        return ans;
    }
};

移除所有载有违禁货物车厢所需的最少时间

【传送门】:https://leetcode-cn.com/problems/minimum-time-to-remove-all-cars-containing-illegal-goods/

thinking:

这题,还是不理解,原官方题解:https://leetcode-cn.com/problems/minimum-time-to-remove-all-cars-containing-illegal-goods/solution/yi-chu-suo-you-zai-you-wei-jin-huo-wu-ch-qinx/

solution:

class Solution {
public:
    int minimumTime(string s) {
        int n = s.size();
        int ans = INT_MAX;
        int presum = 0, prebest = 0;
        for (int j = 0; j < n; ++j) {
            prebest = min(prebest, j - 2 * presum);
            presum += (s[j] - '0');
            ans = min(ans, prebest + 2 * presum - j);
        }
        return min(ans + n - 1, n);
    }
};
posted @ 2022-02-09 17:35  圣道  阅读(30)  评论(0)    收藏  举报