第285场周赛

6027. 统计数组中峰和谷的数量 - 力扣(LeetCode) (leetcode-cn.com)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
int countHillValley(vector<int>& nums) {
int n = nums.size();
vector<int> vec;
for (int i = 0; i < n; i ++) {
while (i + 1 < n && nums[i] == nums[i + 1]) i ++;
vec.push_back(nums[i]);
}
int res = 0;
// for (auto num: vec) cout << num << endl;
for (int i = 1; i < vec.size() - 1; i ++) {
if (vec[i - 1] > vec[i] && vec[i + 1] > vec[i]) res ++;
if (vec[i - 1] < vec[i] && vec[i] > vec[i + 1]) res ++;
}
return res;
}
};

6028. 统计道路上的碰撞次数 - 力扣(LeetCode) (leetcode-cn.com)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
int countCollisions(string directions) {
int ans = 0;
bool flag = false;
for (int i = 0; i < directions.size(); i ++) {
if (directions[i] == 'R' || directions[i] == 'S') flag = true;
else if (flag) ans ++;
}
flag = false;
for (int i = directions.size() - 1; i >= 0; i --) {
if (directions[i] == 'L' || directions[i] == 'S') flag = true;
else if (flag) ans ++;
}
return ans;
}
};

6029. 射箭比赛中的最大得分 - 力扣(LeetCode) (leetcode-cn.com)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
public:
vector<int> maximumBobPoints(int numArrows, vector<int>& aliceArrows) {
int n = aliceArrows.size();
vector<int> ans(n, 0);
int maxScore = 0;
for (int i = 0; i < 1 << n; i ++) {
int score = 0, arrows = 0;;
vector<int> bobArrows(n, 0);
for (int j = 0; j < n; j ++) {
if (((i >> j) & 1) == 1) {
score += j;
arrows += aliceArrows[j] + 1;
bobArrows[j] = aliceArrows[j] + 1;
}
}
if (arrows > numArrows) continue;
if (score > maxScore) {
maxScore = score;
bobArrows[0] += (numArrows - arrows);
ans = bobArrows;
}
}
return ans;
}
};

6030. 由单个字符重复的最长子字符串 - 力扣(LeetCode) (leetcode-cn.com)

要用线段树,不太会


第285场周赛
https://2w1nd.github.io/2022/03/20/LC周赛/第285场周赛/
作者
w1nd
发布于
2022年3月20日
许可协议