LeetCode第 407 场周赛

T2字符串元音游戏

如果r = 元音字母个数为0,则必输,否则就是赢。
解释:如果r = 偶数, r - 奇数 = 奇数 --> 奇数 - 偶数 = 奇数。所以如果r !=0 小明必输

class Solution:
    def doesAliceWin(self, s: str) -> bool:
        r = 0
        for i in s:
            if i in ['a','e','i','o','u']:
                r += 1
        if r == 0 :
            return False
        return True;

T3将1移动到末尾的最大操作次数
他要求最大次数,那就每次需要最浪费的处理
从前往后,将连续1连续0,分成两部分,
例如[1,1,1,0,0,1,1,0] —>[3,0,2,0]
如何处理呢?
第一步:[3, 0, 2,0] —>[0,3,2,0][0,5,0] res += 3
第二步[0,5,0] —>[0,0,5]完成✅ res += 5
res = 8
代码如何实现,
处理完数组以后,每次加前缀和即可

class Solution {
public:
    int maxOperations(string s) {
        int n = s.size();
        int i = 0;
        vector<int> v;
        while(i < n && s[i] == '0')
        {
            i ++;
        }
        while(i < n)
        {
            int cnt_1 = 0,cnt_0 = 0;
            while(i < n && s[i] == '1')
            {
                i ++;
                cnt_1 ++;
            }
            while(i < n && s[i] == '0')
            {
                i ++;
                cnt_0 ++;
            }
            if(cnt_1 && cnt_0)
            {
                v.push_back(cnt_1);
                v.push_back(0);
            }
        }
        int res = 0,sum = 0;
        for(auto c : v)
        {
            if(!c)continue;
            
            sum += c;
            res += sum;
        } 
        return res;
        }
};

T4使数组等于目标数组所需的最少操作次数
使两个数组相等,也等价于使他们的差分数组相等
构造两个数组的差分数组,c1,c2
然后从前往后,使两个数组相等
如果cur = c2[i] - c1[i] > 0,意味着c1[i]需要增加,但是对于差分数组,你如果在某个位置加了,那么就需要在后面某个位置减回来,但是位置在哪呢,我们不知道,所以先存起来,存到num_low里面。相当于一个存钱罐,哪里需要的时候从里面取.

class Solution {
public:
    long long minimumOperations(vector<int>& nums, vector<int>& target) {
        typedef long long LL;
        vector<LL> c1;
        vector<LL> c2;
        int n = nums.size();
        LL res = 0;
        for(int i = 0; i < n; i ++)
        {
            if(i == 0)c1.push_back(nums[0]);
            else c1.push_back(nums[i] - nums[i-1]);

            if(i == 0)c2.push_back(target[0]);
            else c2.push_back(target[i] - target[i-1]);
        }
        LL num_up = 0, num_low = 0;
        for(int i = 0; i < n; i ++)
        {
            LL cur = c2[i] - c1[i];
            if(cur > 0)
            {
                // 需要+数
                LL need = min(num_up, cur);
                num_up -= need;
                cur -= need;
                res += cur;
                //自己造的正数会产生负数
                num_low += cur;
            }
            else if(cur < 0)
            {
                cur *= -1;
                LL need = min(num_low, cur);
                num_low -= need;
                cur -= need;
                res += cur;
                num_up += cur;
            }
        }
        return res;

    }
};
  • 14
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值