6283. 正整数和负整数的最大计数
直接模拟即可。
class Solution {
public:
int maximumCount(vector<int>& nums) {
int n = nums.size();
int a = 0, b = 0;
for(int i = 0; i < n; i ++ ){
if(nums[i] < 0) a ++;
else if(nums[i] > 0) b ++;
}
return max(a, b);
}
};
6285. 执行 K 次操作后的最大分数
1.大根堆;2.上取整小tip
typedef long long ll;
class Solution {
public:
long long maxKelements(vector<int>& nums, int k) {
ll res = 0;
priority_queue<int> q;
int n = nums.size();
for(int i = 0; i < n; i ++ ){
q.push(nums[i]);
}
while(k -- ){
int t = q.top();
q.pop();
q.push((t + 2) / 3);
res += t;
}
return res;
}
};
6284. 使字符串总不同字符的数目相等
两个数组分别存下两个字符串中出现的字符个数,然后模拟交换过程,交换成功则return true,否则恢复现场。
class Solution {
public:
bool check(int a[], int b[]){
int n1 = 0, n2 = 0;
for(int i = 0; i < 26; i ++ ){
if(a[i]) n1 ++;
if(b[i]) n2 ++;
}
if(n1 == n2) return true;
else return false;
}
bool isItPossible(string word1, string word2) {
int a[26] = {0};
int b[26] = {0};
for(auto& c :word1){
a[c - 'a'] ++;
}
for(auto& c : word2){
b[c - 'a'] ++;
}
for(int i = 0; i < 26; i ++ ){
bool flag = false;
if(a[i]){
if(b[i]) flag = true;
a[i] --, b[i] ++;
for(int j = 0; j < 26; j ++ ){
if((b[j] > 0 && j != i) || (j == i && flag)){
b[j] --, a[j] ++;
if(check(a, b)) return true;
else{
b[j] ++, a[j] --;
}
}
}
a[i] ++, b[i] --;
}
}
return false;
}
};