零神大数据
1876,Substrings of Size Three with Distinct Characters,substrings-of-size-three-with-distinct-characters,1248.7224675206
1877,Minimize Maximum Pair Sum in Array,minimize-maximum-pair-sum-in-array,1301.3817574010
1878,Get Biggest Three Rhombus Sums in a Grid,get-biggest-three-rhombus-sums-in-a-grid,1897.5516652727
1879,Minimum XOR Sum of Two Arrays,minimum-xor-sum-of-two-arrays,2145.1839952670
1876. Substrings of Size Three with Distinct Characters
classSolution { public: intcountGoodSubstrings(string s){ constint n = s.size(); vector<int> cnt(26, 0); auto ok = [&]() -> bool { for (int i : cnt) { if (i > 1) returnfalse; } returntrue; }; if (n < 3) return0; int ans = 0; ++cnt[s[0] - 'a']; ++cnt[s[1] - 'a']; ++cnt[s[2] - 'a']; if (ok()) ++ans; for (int i = 3; i < n; ++i) { ++cnt[s[i] - 'a']; --cnt[s[i - 3] - 'a']; if (ok()) ++ans; } return ans; } };
时间复杂度: O(n),
空间复杂度: O(1).
1877. Minimize Maximum Pair Sum in Array
贪心,让大的和小的组合。
可以让最大和最小,同时让最小和最大。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution { public: intminPairSum(vector<int>& nums){ // greedy: match largest and smallest sort(nums.begin(), nums.end()); constint n = nums.size(); int l = 0, r = n - 1; int ans = 0; while (l < r) { ans = max(ans, nums[l++] + nums[r--]); } return ans; } };