1126刷题
1.3206. 交替组 I - 力扣(LeetCode)
class Solution {
public:
int numberOfAlternatingGroups(vector<int>& colors) {
int n = colors.size();
int ans = 0;
for (int i = 0; i < n; i++) {
if (colors[i] != colors[(i - 1 + n) % n] &&
colors[i] != colors[(i + 1 ) % n])
ans++;
}
return ans;
}
};
2.小F的永久代币卡回本计划 - MarsCode
v
#include <iostream>
using namespace std;
int solution(int a, int b) {
// write code here
int tmp = 0;
if (a % b != 0)
tmp = 1;
return a / b + tmp;
}
int main() {
cout << (solution(10, 1) == 10) << endl;
cout << (solution(10, 2) == 5) << endl;
cout << (solution(10, 3) == 4) << endl;
return 0;
}
3.数组元素之和最小化 - MarsCode
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int solution(int n, int k) {
int ans = 0,begin = k;
while(n--){
ans+=begin;
begin+=k;
}
return ans;
}
int main() {
std::cout << (solution(3, 1) == 6) << std::endl;
std::cout << (solution(2, 2) == 6) << std::endl;
std::cout << (solution(4, 3) == 30) << std::endl;
}
4.最大矩形面积问题 - MarsCode
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
int solution(int n, std::vector<int> A) {
// Edit your code here
stack<int> s;
int ans = 0;
for (int i = 0; i < n; i++) {
while (!s.empty() && A[i] < A[s.top()]) {
int a = A[s.top()];
s.pop();
int b = s.empty() ? i : i - s.top() - 1;
ans = max(ans, a * b);
}
ans = max(ans, A[i]);
s.push(i);
}
while (!s.empty()) {
int a = A[s.top()];
s.pop();
int b = s.empty() ? n : n - s.top() - 1;
ans = max(ans, a * b);
}
// cout << ans << endl;
return ans;
}
// [7,8,19,10,6,22,10,5,2,14]
// 15,5,22,19,5,20,13,12,12
int main() {
// Add your test cases here
std::vector<int> A_case1 = std::vector<int>{1, 2, 3, 4, 5};
std::cout << (solution(5, A_case1) == 9) << std::endl;
std::vector<int> B_case1 = std::vector<int>{5, 4, 3, 2, 1, 6};
std::cout << (solution(6, B_case1)) << std::endl;
std::vector<int> C_case1 = std::vector<int>{4, 4, 4, 4};
std::cout << (solution(4, C_case1)) << std::endl;
std::vector<int> D_case1 = std::vector<int>{15, 5, 22, 19, 5, 20, 13, 12, 12};
std::cout << (solution(9, D_case1)) << std::endl;
return 0;
}
5.比赛配对问题 - MarsCode
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int solution(int n) {
// write code here
int ans = 0;
while(n != 1){
ans += n/2;
n = n -n/2;
}
return ans;
}
int main() {
cout << (solution(7) == 6) << endl;
cout << (solution(14) == 13) << endl;
cout << (solution(1) == 0) << endl;
return 0;
}
6.DNA序列编辑距离 - MarsCode
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int solution(std::string dna1, std::string dna2) {
// Please write your code here
int n1 = dna1.size(), n2 = dna2.size();
vector<vector<int>> dp(n1 + 1, vector<int>(n2 + 1, 1000000));
for (int i = 0; i <= n1; i++)
dp[i][0] = i;
for (int i = 0; i <= n2; i++)
dp[0][i] = i;
for (int i = 0; i < n1; i++) {
for (int j = 0; j < n2; j++) {
dp[i + 1][j + 1] = min(dp[i][j] + (dna1[i] != dna2[j]),
min(dp[i + 1][j] + 1, dp[i][j + 1] + 1));
// if(dna1[i] != dna2[j])
// dp[i + 1][j + 1]++;
// cout << dp[i + 1][j + 1] << ' ';
}
// cout << endl;
}
return dp[n1][n2];
}
int main() {
// You can add more test cases here
std::cout << (solution("AGT", "AGCT")) << std::endl;
// std::cout << (solution("AACCGGTT", "AACCTTGG")) << std::endl;
// std::cout << (solution("ACGT", "TGC")) << std::endl;
std::cout << (solution("CCCCACTGGAAGTCCTCCTAT", "T")) << std::endl; // 20
return 0;
}
7组成字符串ku的最大次数 - MarsCode
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int solution(const string& s) {
int k = 0,u = 0;
for(auto e : s){
if(e == 'k'|| e == 'K')
k++;
else if(e == 'u'|| e == 'U')
u++;
}
return min(k,u);
}
int main() {
cout << (solution("AUBTMKAxfuu") == 1) << endl;
cout << (solution("KKuuUuUuKKKKkkkkKK") == 6) << endl;
cout << (solution("abcdefgh") == 0) << endl;
}
8.游戏排名第三大的分数 - MarsCode
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
bool fun(int a,int b){
return a > b;
}
int solution(int n, std::vector<int> nums) {
sort(nums.begin(),nums.end(),fun);
int pos = 0;
// cout << nums[0] <<endl;
for(int i = 1;i < n;i++){
if(nums[i] != nums[i - 1])
pos++;
if(pos == 2)
return nums[i];
}
return nums[0];
}
int main() {
std::cout << (solution(3, {3, 2, 1})) << std::endl;
std::cout << (solution(2, {1, 2})) << std::endl;
std::cout << (solution(4, {2, 2, 3, 1})) << std::endl;
return 0;
}
9完美偶数计数 - MarsCode
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int solution(int n, int l, int r, vector<int>& a) {
int ans = 0;
for(auto e : a){
if(e%2 == 0 && e>= l &&e <= r)
ans++;
}
return ans;
}
int main() {
vector<int> a1 = {1, 2, 6, 8, 7};
cout << (solution(5, 3, 8, a1) == 2) << endl;
vector<int> a2 = {12, 15, 18, 9};
cout << (solution(4, 10, 20, a2) == 2) << endl;
vector<int> a3 = {2, 4, 6};
cout << (solution(3, 1, 10, a3) == 3) << endl;
}
10RGB色值转换为整数值 - MarsCode
#include <iostream>
#include <string>
using namespace std;
int solution(std::string rgb) {
// Please write your code here
auto pos0 = rgb.find('(');
auto pos1 = rgb.find(',',pos0);
auto pos2 = rgb.find(',',pos1 + 1);
auto pos3 = rgb.find(')',pos2);
int a = stoi(rgb.substr(pos0 + 1,pos1 - 1));
int b = stoi(rgb.substr(pos1 + 2,pos2 - 1));
int c = stoi(rgb.substr(pos2 + 2,pos3 - 1));
// cout <<a <<' '<<b <<' '<<c<<endl;
return a*256*256+b*256+c;
}
int main() {
// You can add more test cases here
std::cout << (solution("rgb(192, 192, 192)") == 12632256) << std::endl;
std::cout << (solution("rgb(100, 0, 252)") == 6553852) << std::endl;
std::cout << (solution("rgb(33, 44, 55)") == 2174007) << std::endl;
return 0;
}