Leetcode第288场周赛-2022/4/10

1.题目链接6037. 按奇偶性交换后的最大数字

class Solution {
public:
    int largestInteger(int num) {
        vector<int> v;
        int t = 0;
        while(num){
            v.push_back(num % 10);
            num /= 10;
            t ++;
        }
        reverse(v.begin(), v.end());
        
        for(int i = 0; i < v.size() - 1; i ++){
            for(int j = i + 1; j < v.size(); j ++){
                if(v[i] % 2 == v[j] % 2){//奇偶相同
                    if(v[i] < v[j]) swap(v[i], v[j]);
                } 
            }
        }
        reverse(v.begin(), v.end());
        //for(auto x : v) cout << x << endl;
        int ans = 0;
        long long q = 1;
        for(auto x : v){
            ans += x * q;
            q *= 10;
        }

        return ans;
    }
};

2.题目链接2232. 向表达式添加括号后的最小结果

typedef long long LL;
class Solution {
public:
    string minimizeResult(string expression) {
        int n = expression.size();
        vector<int> v;
        string ans;
        int p = 0; // + 的下标
        for(int i = 0; i < n; i ++){
            if(expression[i] == '+'){
                v.push_back(0);
                p = i;
            }else v.push_back(expression[i] - '0');
        }
        //for(auto x : v) cout << x << endl;
        vector<LL> ep;
        LL t = 1;
        for(int i = 0; i < 10; i ++){
            ep.push_back(t);
            t *= 10;
        }
       // for(auto x : ep) cout << x << endl;
        LL res = 1e18;
        int p1 = 0, p2 = 0; //记录答案
        for(int l = 0; l < p; l ++){
            for(int r = p + 1; r < n; r ++){
                LL el = 0, em1 = 0, em2 = 0, em = 0, er = 0;
                for(int i = l - 1, j = 0; i >= 0; i --, j ++){
                    el += v[i] * ep[j];
                }
                for(int i = p - 1, j = 0; i >= l; i --, j ++){
                    em1 += v[i] * ep[j];
                }
                for(int i = r, j = 0; i >= p + 1; i --, j ++){
                    em2 += v[i] * ep[j];
                }
                em = em1 + em2;
                for(int i = n - 1, j = 0; i > r; i --, j ++){
                    er += v[i] * ep[j];
                }
                if(!er) er = 1;
                if(!el) el = 1;
                //cout << el << ' ' << em << ' ' << er << endl;
                LL t = el * em * er;
                if(t < res){
                    res = t;
                    p1 = l, p2 = r;
                }
                res = min(t, res);
            }
        }
        //cout << res << endl;
        //ans = to_string(res);
        //cout << p1 << ' ' << p2 << endl;
        for(int i = 0; i < n; i ++){
            if(i == p1) ans += '(';
            ans += expression[i];
            if(i == p2) ans += ')';
        }
        return ans;
    }
};

3.题目链接2233. K 次增加后的最大乘积

typedef long long LL;
const int mod = 1e9 + 7;

class Solution {
public:
    int maximumProduct(vector<int>& nums, int k) {
        LL ans = 1;
        int n = nums.size();
        sort(nums.begin(), nums.end());
        //for(auto x : nums) cout << x << endl;
        int l = 0, r = 0;
        while(r < n && nums[r] == nums[l]) r ++;  
        r --;//找到r
        while(k){
            for(int i = l; i <= r; i ++){
                if(k){
                    nums[i] ++;  // + 1
                    k --;
                }
            }
            if(!k) continue;  //跳出
            while(r < n && nums[r] == nums[l]) r ++; 
            r --;  //继续找r
        }
        for(auto x : nums){
            ans = ans * (LL)x % mod; 
        }

        return ans;
    }
};

4.题目链接2234. 花园的最大总美丽值

typedef long long LL;
class Solution {
public:
    long long maximumBeauty(vector<int>& flowers, long long newFlowers, int target, int full, int partial) {
        sort(flowers.begin(), flowers.end());
        LL n = flowers.size();
        if(flowers[0] >= target) return (LL)n * full; //一开始就是完善的

        LL leftFlowers = newFlowers - n * target; //枚举前缀可以种的花的个数
        for(int i = 0; i < n; i ++){
            flowers[i] = min(flowers[i], target);
            leftFlowers += flowers[i];
        }

        LL ans = 0, sumFlowers = 0;
        for(int i = 0, j = 0; i <= n; i ++){  //枚举后缀长度 n - i
            if(leftFlowers >= 0){
                //计算最长前缀的长度
                while(j < i && (LL)flowers[j] * j - sumFlowers <= leftFlowers)
                    sumFlowers += flowers[j ++];
                LL beauty = (n - i) * full;  //计算总的美丽值
                if(j > 0) beauty += min((leftFlowers + sumFlowers) / j, (LL)target - 1) * partial;
                ans = max(ans, beauty);
            }
            if(i < n) leftFlowers += target - flowers[i];
        }
        return ans;
    }
};

posted @ 2022-04-11 11:11  冯大善人  阅读(12)  评论(0编辑  收藏  举报