第287场周赛
第一题(模拟)
这道题取巧用python分割字符串更加方便
class Solution:
def convertTime(self, current: str, correct: str) -> int:
a = current.split(':')
b = correct.split(':')
a[0] = int(a[0])
a[1] = int(a[1])
b[0] = int(b[0])
b[1] = int(b[1])
ans = (b[0]-a[0]+24)%24
m = (b[1]-a[1]+60)%60
if(b[1]-a[1] < 0):
ans-=1
ans += int(m/15);
m -= 15*int(m/15)
ans += int(m/5);
m -= 5*int(m/5)
ans += m
return ans
第二题(哈希)
直接哈希统计每个人输的次数,同时用set统计一下所有的人的名字,方便遍历。
class Solution {
public:
vector<vector<int>> findWinners(vector<vector<int>>& matches) {
vector<int>all_win,one_lose;
vector<vector<int>>ans;
set<int>se;
map<int,int>mp;
for(auto match : matches)
{
mp[match[1]]++;
se.insert(match[0]);
se.insert(match[1]);
}
for(auto k : se)
{
if(mp[k] == 0)all_win.push_back(k);
if(mp[k] == 1)one_lose.push_back(k);
}
ans.push_back(all_win);
ans.push_back(one_lose);
return ans;
}
};
第三题(二分)
这题破天荒的第一眼看到就能想到用二分查找(可能题解抄多了也能产生条件反射了。。。)
二分查找每个小孩分到的糖果数,判断每个堆能按照当前糖果数能分成的份数ni,用总份数n与k进行对比。n<k则不能分到,说明糖果数太大,无法实现。n>=k则说明可以分到,但是能分到的最大值,所以需要继续查找。
class Solution {
public:
bool check(vector<int>& candies, int mid, long long k)
{
long long n = 0;
for(auto c : candies)
{
n += c/mid;
}
return n < k;
}
int maximumCandies(vector<int>& candies, long long k) {
long long sum = 0;
int l = 1, r = 10000005;
while(l < r)
{
int mid = l + (r-l)/2;
cout<<mid<<endl;
if(check(candies, mid, k))
{
r = mid;
}
else l = mid+1;
}
for(auto c : candies)
{
k -= c;
}
if(k > 0)l = 0;
return l == 0 ? 0 : l-1;
}
};
第四题(模拟)
第四题看题目特长,当时又还有报告没做完,就懒得读题目直接放弃了。后面听说这题很简单,回来一细看,果然很简单,血亏。直接模拟,然后保持题目中字典的映射后就行。
class Encrypter {
public:
unordered_map<char, string> mp;
unordered_map<string, int> dic;
Encrypter(vector<char>& keys, vector<string>& values, vector<string>& dictionary) {
int n = keys.size();
for (int i = 0; i < n; i++) {
mp[keys[i]] = values[i];
}
for (auto &d : dictionary) {
dic[encrypt(d)]++;
}
}
string encrypt(string word1) {
string ans;
for (auto &ch : word1) {
ans += mp[ch];
}
return ans;
}
int decrypt(string word2) {
return dic[word2];
}
};