Rank:585/3910
\quad
AC: 3/4
上周的周赛,补一下题解
题目传送:182周赛
1394. 找出数组中的幸运数
class Solution {
public:
int findLucky(vector<int>& arr) {
int count[600];
memset(count,0,sizeof(count));
for(int i=0;i<arr.size();i++) count[arr[i]]++;
int res = -1;
for(int i=0;i<arr.size();i++){
if(arr[i]==count[arr[i]]){
res = max(res,arr[i]);
}
}
return res;
}
};
时间复杂度: O ( n ) O(n) O(n) 空间复杂度: O ( n ) O(n) O(n)
1395. 统计作战单位数
数据范围:n=200,可以进行暴力枚举, n 3 = 8000000 n^3=8000000 n3=8000000,是可以过掉的
class Solution {
public:
int numTeams(vector<int>& rate) {
int res = 0,n = rate.size();
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
for(int k=j+1;k<n;k++){
if(rate[i]>rate[j]&&rate[j]>rate[k]) {
res ++;
// cout << rate[i] << " " << rate[j] << " " <<rate[k] << endl;
}
else if(rate[i]<rate[j]&&rate[j]<rate[k]) {
res ++;
// cout << rate[i] << " " << rate[j] << " " <<rate[k] << endl;
}
}
}
}
return res;
}
};
时间复杂度: O ( n 3 ) O(n^3) O(n3) 空间复杂度: O ( 1 ) O(1) O(1)
1396. 设计地铁系统
这道题目主要是数据结构的设计问题
unordered_map<int,pair<string,int>> in; 保存了乘客的id和站名和进站时间
unordered_map<int,pair<string,int>> out; 保存了乘客id和站名和出站时间
unordered_map<pair<string,string>,pair<int,int>,hashfunc> st; 保存了进站名和出站名 以及 走这段路程的人数,以及所有走这段路程时间
struct hashfunc
{
template<typename T, typename U>
size_t operator() (const pair<T, U> &i) const
{
return hash<T>()(i.first) ^ hash<U>()(i.second);
}
};
class UndergroundSystem {
public:
unordered_map<int,pair<string,int>> in;
unordered_map<int,pair<string,int>> out;
unordered_map<pair<string,string>,pair<int,int>,hashfunc> st;
UndergroundSystem() {
}
void checkIn(int id, string stationName, int t) {
in[id] = make_pair(stationName,t);
}
void checkOut(int id, string stationName, int t) {
out[id] = make_pair(stationName,t);
string start = in[id].first;
int stime = in[id].second;
st[make_pair(start,stationName)].first ++;
st[make_pair(start,stationName)].second += t-stime;
}
double getAverageTime(string startStation, string endStation) {
return st[make_pair(startStation,endStation)].second*1.0 / st[make_pair(startStation,endStation)].first;
}
};
/**
* Your UndergroundSystem object will be instantiated and called as such:
* UndergroundSystem* obj = new UndergroundSystem();
* obj->checkIn(id,stationName,t);
* obj->checkOut(id,stationName,t);
* double param_3 = obj->getAverageTime(startStation,endStation);
*/
1397. 找到所有好字符串
数位DP+KMP待补