add: 220211

This commit is contained in:
Lam Haoyin 2022-02-13 00:19:52 +08:00
parent 51a6234dc9
commit 90d06ec7ca
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
2 changed files with 57 additions and 0 deletions

25
2202/220211-CN.cpp Normal file
View File

@ -0,0 +1,25 @@
#include <algorithm>
#include <vector>
/**
* 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<int>& 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;
}

32
2202/220211.cpp Normal file
View File

@ -0,0 +1,32 @@
#include <string>
/**
* 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;
}
};