365天挑战LeetCode1000题——Day 264 周赛336
第一题:遍历
class Solution {
public:int vowelStrings(vector& words, int left, int right) {unordered_set st = {'a', 'e', 'i', 'o','u'};int cnt = 0;for (int i = left; i <= right; i++) {if (st.count(words[i][0]) && st.count(words[i].back())) cnt++;}return cnt;}
};
第二题:贪心
class Solution {
public:int maxScore(vector& nums) {sort(nums.begin(), nums.end(), [](int a, int b) {return a > b;});long long cnt = 0;int score = 0;for (int x : nums) {cnt += x;if (cnt > 0) score++;}return score;}
};
第三题:前缀和 + 哈希表
class Solution {
public:long long beautifulSubarrays(vector& nums) {long long ans = 0;unordered_map mp;int cur = 0;mp[0] = 1;for (int x : nums) {cur ^= x;if (mp.count(cur)) ans += mp[cur];mp[cur]++;}return ans;}
};
第四题:贪心 + 暴力
class Solution {
public:int findMinimumTime(vector>& tasks) {int cnt = 0;vector run(2001, false);sort(tasks.begin(), tasks.end(), [](vector a, vector b) {return a[1] < b[1];});int s, e, d;for (auto task : tasks) {s = task[0], e = task[1], d = task[2];d -= accumulate(run.begin() + s, run.begin() + e + 1, 0);if (d > 0) {for (int i = e; i >= d; i--) {if (!run[i]) {run[i] = true;cnt++;d--;if (d == 0) break;}}}}return cnt;}
};
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!