6921. 按分隔符拆分字符串 - 力扣(LeetCode)
思路:模拟
class Solution { public: vector<string> splitWordsBySeparator(vector<string>& words, char s) { vector<string> res; string tmp; for(auto t : words) { int i = 0; while(t[i] == s) i ++ ; for( ; i < t.size(); i ++ ) { while (t[i] == s && tmp.size() == 0) i ++ ; if(t[i] == s && tmp.size()) { res.push_back(tmp); tmp.erase(0, tmp.size()); continue ; } if(t[i] != s && i < t.size()) tmp += t[i]; } if(tmp.size()) { res.push_back(tmp); tmp.erase(0, tmp.size()); } } return res; } };
6915. 合并后数组中的最大元素 - 力扣(LeetCode)
思路:贪心,从后往前模拟
class Solution { public: long long maxArrayValue(vector<int>& nums) { int n = nums.size(); long long res = 0; if(n == 1) return nums[0]; if(n == 2) { if(nums[0] > nums[1]) return nums[0]; else return (long long)nums[0] + nums[1]; } long long tmp = nums[n - 1]; for(int i = n - 2; i >= 0; i -- ) { if(nums[i] <= tmp) { tmp += nums[i]; res = max(tmp, res); } else { res = max(res, (long long) nums[i]); tmp = nums[i]; } } return res; } };
6955. 长度递增组的最大数目 - 力扣(LeetCode)
思路:贪心
class Solution { public: int maxIncreasingGroups(vector<int>& usageLimits) { int n = usageLimits.size(); vector<long long> num(usageLimits.begin(), usageLimits.end()); sort(num.begin(), num.end()); int x = 1; for(int i = 0; i < n; i ++ ) { if(num[i] >= x) num[i] -= x ++ ; if(i < n - 1) num[i + 1] += num[i]; } return x - 1; } };