Leetcode第62场双周赛

5871. 将一维数组转变成二维数组

class Solution {
public:
    vector<vector<int>> res;
    vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {
        if (m * n != original.size())
            return res;
        for (int i = 0; i < m; i ++ ) {
            vector<int> path;
            for (int j = 0; j < n; j ++ ) {
                path.push_back(original[i * n + j]);
            }
            res.push_back(path);
        }
        return res;
    }
};

5872. 连接后等于目标字符串的字符串对

class Solution {
public:
    int numOfPairs(vector<string>& nums, string target) {
        int n = nums.size(), res = 0;
        for (int i = 0; i < n; i ++ )
            for (int j = 0; j < n; j ++ )
                if (i != j) {
                    string t = nums[i] + nums[j];
                    if (t == target)
                        res ++ ;
                }
        return res;
    }
};

5873. 考试的最大困扰度

class Solution {
public:
    int maxConsecutiveAnswers(string a, int k) {
        int l = 0, r = 0, tc = 0, fc = 0, n = a.size();
        int res = 0;
        while (r < n) {
            if (a[r] == 'T') tc ++ ;
            else fc ++ ;
            while (tc > k && fc > k) {
                if (a[l] == 'T') tc -- ;
                else fc -- ;
                l ++ ;
            }
            res = max(res, r - l + 1);
            r ++ ;
        }
        return res;
    }
};

5874. 分割数组的最多方案数

思路:哈希

循环枚举修改的值,哈希保存左边前缀和个数和右边前缀和个数

class Solution {
public:
typedef long long LL;
    int waysToPartition(vector<int>& nums, int k) {
        int n = nums.size();
        vector<LL> sum(n + 1);
        sum[0] = nums[0];
        unordered_map<LL, LL> lc, rc;
        for (int i = 1; i < n; i ++ ) {
            sum[i] = sum[i - 1] + nums[i];
            rc[sum[i - 1]] ++ ;
        }
        int res = 0;
        LL tot = sum[n - 1];
        if (tot % 2 == 0)
            res = rc[tot / 2];
        for (int i = 0; i < n; i ++ ) {
            int d = k - nums[i];
            if ((tot + d) % 2 == 0) {
                res = max(res, lc[(tot + d) / 2] + rc[(tot - d) / 2]);
            }
            lc[sum[i]] ++ ;
            rc[sum[i]] -- ;
        }
        return res;
    }

    LL max(LL a, LL b) {
        if (a > b) return a;
        else return b;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Shirandexiaowo

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

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

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

打赏作者

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

抵扣说明:

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

余额充值