diff --git a/2202/220211-CN.cpp b/2202/220211-CN.cpp new file mode 100644 index 0000000..19a0ed1 --- /dev/null +++ b/2202/220211-CN.cpp @@ -0,0 +1,25 @@ +#include +#include + +/** + * 1984. Minimum Difference Between Highest and Lowest of K Scores + * You are given a 0-indexed integer array nums, where nums[i] represents the score of the ith student. You are also given an integer k. + * Pick the scores of any k students from the array so that the difference between the highest and the lowest of the k scores is minimized. + * Return the minimum possible difference. + */ + +class Solution { +public: + int minimumDifference(std::vector& nums, int k) { + std::sort(nums.begin(), nums.end()); + int ans = 99999999; + for (int i = k; i <= nums.size(); ++i) + ans = std::min(ans, nums[i - 1] - nums[i - k]); + return ans; + } +}; + +int main() { + // Submitted by phone, no test provided. + return 0; +} diff --git a/2202/220211.cpp b/2202/220211.cpp new file mode 100644 index 0000000..6dd856f --- /dev/null +++ b/2202/220211.cpp @@ -0,0 +1,32 @@ +#include + +/** + * 567. Permutation in String + * Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise. + * In other words, return true if one of s1's permutations is the substring of s2. + */ + +class Solution { +private: + bool ok(int* c) { + for (int i = 0; i < 26; ++i) + if (c[i]) return false; + return true; + } +public: + bool checkInclusion(string s1, string s2) { + int n1 = s1.length(), n2 = s2.length(); + if (n1 > n2) return false; + int cnt[26]{}; + for (char c: s1)++cnt[c - 'a']; + for (int i = 0; i < n1; ++i) + --cnt[s2[i] - 'a']; + if (ok(cnt)) return true; + for (int i = n1; i < n2; ++i) { + --cnt[s2[i] - 'a']; + ++cnt[s2[i - n1] - 'a']; + if (ok(cnt)) return true; + } + return false; + } +}; \ No newline at end of file