classSolution: defcanBeTypedWords(self, text: str, brokenLetters: str) -> int: ans = 0 broken = set() for i in brokenLetters: broken.add(i) defok(word): for c in word: if c in broken: returnFalse returnTrue for word in text.split(' '): if ok(word): ans += 1 return ans
classSolution { public: intaddRungs(vector<int>& rungs, int dist){ int ans = 0; int last = 0; for (int idx = 0; idx < rungs.size(); ++idx) { constint i = rungs[idx]; if (i - last <= dist) { last = i; } else { ans += (i - last - 1) / dist; last = i; } } return ans; } };
classSolution: defcountTriples(self, n: int) -> int: ans = 0 for c inrange(1, n+1): for b inrange(1, c+1): for a inrange(1, b+1): if a * a + b * b == c * c: if a == b: ans += 1 else: ans += 2 return ans
1893. Check if All the Integers in a Range Are Covered
签到题。对于[right, right]中每一个数,判断是否被ranges中的某个区间包含。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { public: boolisCovered(vector<vector<int>>& ranges, int left, int right){ auto cover = [&](constint i) -> bool { for (constauto& range : ranges) { constint l = range[0], r = range[1]; if (l <= i && i <= r) returntrue; } returnfalse; }; for (int i = left; i <= right; ++i) { if (!cover(i)) returnfalse; } returntrue; } };
classSolution { using ll = longlong; public: intchalkReplacer(vector<int>& chalk, int k){ constint n = chalk.size(); vector<ll> presum(n + 1); presum[0] = 0; for (int i = 0; i < n; ++i) { presum[i+1] = presum[i] + chalk[i]; } if (k >= presum.back()) { k = k % presum.back(); } // the first index, presum[i] > k auto it = upper_bound(presum.begin(), presum.end(), k); returndistance(presum.begin(), it) - 1; } };
时间复杂度: O(N + log N), N = chalk.length,
空间复杂度: O(chalk.length).
零神大数据:
1897,Redistribute Characters to Make All Strings Equal,redistribute-characters-to-make-all-strings-equal,1309.1422268153
1898,Maximum Number of Removable Characters,maximum-number-of-removable-characters,1912.8440554296
1899,Merge Triplets to Form Target Triplet,merge-triplets-to-form-target-triplet,1635.6879273926
1900,The Earliest and Latest Rounds Where Players Compete,the-earliest-and-latest-rounds-where-players-compete,2454.7653333657
1897. Redistribute Characters to Make All Strings Equal
签到题。本质是判断所有的字符是否可以平均分配到n个单词中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { public: boolmakeEqual(vector<string>& words){ constint n = words.size(); vector<int> cnt(26, 0); for (constauto& word : words) { for (char c : word) { ++cnt[c - 'a']; } } for (int i : cnt) { if (i % n != 0) returnfalse; } returntrue; } };
零神大数据:
1880,Check if Word Equals Summation of Two Words,check-if-word-equals-summation-of-two-words,1187.1641565458
1881,Maximum Value after Insertion,maximum-value-after-insertion,1381.2168789318
1882,Process Tasks Using Servers,process-tasks-using-servers,1979.1112273597
1883,Minimum Skips to Arrive at Meeting On Time,minimum-skips-to-arrive-at-meeting-on-time,2587.8725248485
零神大数据
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