1753. 移除石子的最大得分
# 1753. 移除石子的最大得分
class Solution {public: int maximumScore(int a, int b, int c) { int sum = a + b + c; int maxVal = max({a, b, c}); if (sum - maxVal < maxVal) { return sum - maxVal; } else { return sum / 2; } }};
复杂度分析
时间复杂度:$O (1)$
空间复杂度:$O (1)$
1760. 袋子里最少数目的球
# 1760. 袋子里最少数目的球
class Solution {public: int minimumSize(vector<int>& nums, int maxOperations) { int left = 1, right = *max_element(nums.begin(), nums.end()); int ans = 0; while (left <= right) { int y = (left + right) / 2; long long ops = 0; for (int x: nums) { ops += (x - 1) / y; } if (ops <= maxOperations) { ans = y; right = ...
1971. 寻找图中是否存在路径
# 1971. 寻找图中是否存在路径
# 题解
class Solution {public: bool dfs(int source, int destination, vector<vector<int>> &adj, vector<bool> &visited) { if (source == destination) { return true; } visited[source] = true; for (int next : adj[source]) { if (!visited[next] && dfs(next, destination, adj, visited)) { return true; } } return false; & ...
1703. 得到连续 K 个 1 的最少相邻交换次数
# 1703. 得到连续 K 个 1 的最少相邻交换次数
class Solution {public: int minMoves(vector<int>& nums, int k) { vector<int> g; vector<int> preSum(1, 0); for (int i = 0; i < nums.size(); i++) { if (nums[i] == 1) { g.emplace_back(i - g.size()); preSum.emplace_back(preSum.back() + g.back()); } } int m = g.size(), res = INT_MAX; for (int i = 0; i <= m - k; i++) ...
1764. 通过连接另一个数组的子数组得到一个数组
# 1764. 通过连接另一个数组的子数组得到一个数组
class Solution {public: bool canChoose(vector<vector<int>>& groups, vector<int>& nums) { int p = 0, q= 0; for (int i = 0; i < nums.size(); i++) { if (nums[i] == groups[p][q]) { int flag = 1; int j = 1; for (; j < groups[p].size(); j++) { if (i + j >= nums.size()) { flag = 0; ...
1785. 构成特定和需要添加的最少元素
# 1785. 构成特定和需要添加的最少元素
class Solution {public: int minElements(vector<int>& nums, int limit, int goal) { long long sum = 0; for (auto num : nums) sum += num; long long del = abs(sum - goal); return del%limit?del/limit + 1:del/limit; }};
复杂度分析
时间复杂度:$O (n)$
空间复杂度:$O (1)$
1945. 字符串转化后的各位数字之和
# 1945. 字符串转化后的各位数字之和
class Solution {public: int getLucky(string s, int k) { int ans = 0; for (auto str : s) { int t = str - 'a' + 1; while (t) { ans += t%10; t /= 10; } } while (k != 1) { int t = 0; while (ans) { t += ans%10; ans /= 10; } ans = t; k--; ...
1697. 检查边长度限制的路径是否存在
# 1697. 检查边长度限制的路径是否存在
// 并查集模板,包含路径压缩(参考 findset 函数)以及按秩合并(参考 sz 变量)class UF {public: vector<int> fa; vector<int> sz; int n; int comp_cnt; public: UF(int _n): n(_n), comp_cnt(_n), fa(_n), sz(_n, 1) { iota(fa.begin(), fa.end(), 0); } int findset(int x) { return fa[x] == x ? x : fa[x] = findset(fa[x]); } bool unite(int x, int y) { x = findset(x); y = findset(y); if (x == y) { ...
1832. 判断句子是否为全字母句
# 1832. 判断句子是否为全字母句
class Solution {public: bool checkIfPangram(string sentence) { unordered_set<char> set; for (auto s : sentence) set.insert(s); return set.size() == 26; }};
复杂度分析
时间复杂度:$O (n)$
空间复杂度:$O©$
1781. 所有子字符串美丽值之和
# 1781. 所有子字符串美丽值之和
class Solution {public: int beautySum(string s) { int res = 0; for (int i = 0; i < s.size(); i++) { vector<int> cnt(26); int maxFreq = 0; for (int j = i; j < s.size(); j++) { cnt[s[j] - 'a']++; maxFreq = max(maxFreq, cnt[s[j] - 'a']); int minFreq = s.size(); for (int k = 0; k < 26; k++) { ...