【2022/04/03-第287场单周赛】复盘

总结
一开始做的时候没看懂第四题。
Q1.转化时间需要的最少操作数
贪心。
class Solution {
public:
int convertTime(string cu, string co) {
string ah = {cu[0],cu[1]}, am = {cu[3], cu[4]};
string bh = {co[0], co[1]}, bm = {co[3], co[4]};
int x = (stoi(bh) - stoi(ah)) * 60 + stoi(bm) - stoi(am), ret = 0;
cout << x;
if(x >= 60){
ret += x /60;
x %= 60;
}
if(x >= 15){
ret += x /15;
x %= 15;
}
if(x >= 5){
ret += x / 5;
x %= 5;
}
ret += x;
return ret;
}
};
Q2.找出输掉零场或一场比赛的玩家
直接哈希。
class Solution {
public:
vector<vector<int>> findWinners(vector<vector<int>>& matches) {
int ls[100010] = {0}, w[100010] = {0};
for(auto i : matches){
++ls[i[1]];
w[i[0]] = 1;
}
vector<vector<int>> ret(2);
for(int i = 0; i < 100010; ++i){
if(ls[i] == 1) ret[1].push_back(i);
if(ls[i] == 0 && w[i]) ret[0].push_back(i);
}
return ret;
}
};
Q3.每个小孩最多能分到多少糖果
从0到最大堆二分。
class Solution {
public:
bool canD(vector<int>& candies, long long k, long long mid){
long long ret = 0;
for(auto i : candies) ret += i / mid;
// cout << ret << endl;
return ret >= k;
}
int maximumCandies(vector<int>& candies, long long k) {
long long total = 0;
// cout << canD(candies, k, 1);
for(auto i : candies) total += i;
cout << total;
if(total < k) return 0;
long long l = 0, r = total / k;
while(l < r){
if(r - l == 1){
if(canD(candies, k, r)){
l = r;
break;
}
else break;
}
// cout << l << ' ' << r << endl;
long long mid = (l + r) / 2;
if(canD(candies, k, mid)) l = mid;
else r = mid - 1;
}
return l;
}
};
Q4.加密解密字符串
第三个函数只要把dictionary中所有都加密一次,如果加密后等于需要解密的字符串且长度为待解密字符串长度一半,即算作一个。
class Encrypter {
public:
unordered_map<char, string> cs;
unordered_map<string, string> ss;
Encrypter(vector<char>& keys, vector<string>& values, vector<string>& dictionary) {
for(int i = 0; i < keys.size(); ++i) cs[keys[i]] = values[i];
for(auto s : dictionary) ss[s] = encrypt(s);
}
string encrypt(string word1) {
string ret;
for(auto c : word1) ret += cs[c];
return ret;
}
int decrypt(string word2) {
int ret = 0;
for(auto i : ss) if(i.second == word2 && i.first.size() == word2.size() / 2) ++ret;
return ret;
}
};
浙公网安备 33010602011771号