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;
}
};