【leetcode】第182场周赛
做了3道,有进步,尽管第二道用暴力。。。
5368. 找出数组中的幸运数
/*
输入:arr = [2,2,3,4]
输出:2
解释:数组中唯一的幸运数是 2 ,因为数值 2 的出现频次也是 2 。
输入:arr = [1,2,2,3,3,3]
输出:3
解释:1、2 以及 3 都是幸运数,只需要返回其中最大的 3 。
*/
class Solution {
public:
int findLucky(vector<int>& arr) {
unordered_map<int, int> m;
for(auto & i : arr) m[i]++;
int maxval=-1;
for(auto & [val, num] : m){
if(val == num and val > maxval){
maxval = val;
}
}
return maxval;
}
};
5369. 统计作战单位数
直接三层循环暴力。
class Solution {
public:
int numTeams(vector<int>& rating) {
int len = rating.size();
if(len < 3) return 0;
int res = 0;
for(int i=0; i<len; ++i){
for(int j=i+1; j<len; ++j){
for(int k=j+1; k<len; ++k){
if((rating[i]<rating[j] and rating[j]<rating[k]) or (rating[i]>rating[j] and rating[j]>rating[k])) res++;
}
}
}
return res;
}
};
5370. 设计地铁系统
一开始用结构体,然后发现很累赘,unordered_map的value值也可以嵌套。上车用负数表示,下车用正数表示。然后就是卡在了unordered_map获取元素这里,一开始用.at(key)而不是[key],总是提示我terminate called after throwing an instance of 'std::out_of_range'’
,最后改成后者就通过了。这是因为成员函数 at() 会返回参数所关联对象的引用,如果键不存在,就会拋出一个 out_of_range 异常,而[key]使用不存在的键时,会以这个键为新键生成一个新的元素,新元素中对象的值是默认的。
/*
解释:
UndergroundSystem undergroundSystem = new UndergroundSystem();
undergroundSystem.checkIn(45, "Leyton", 3);
undergroundSystem.checkIn(32, "Paradise", 8);
undergroundSystem.checkIn(27, "Leyton", 10);
undergroundSystem.checkOut(45, "Waterloo", 15);
undergroundSystem.checkOut(27, "Waterloo", 20);
undergroundSystem.checkOut(32, "Cambridge", 22);
undergroundSystem.getAverageTime("Paradise", "Cambridge"); // 返回 14.0。从 "Paradise"(时刻 8)到 "Cambridge"(时刻 22)的行程只有一趟
undergroundSystem.getAverageTime("Leyton", "Waterloo"); // 返回 11.0。总共有 2 躺从 "Leyton" 到 "Waterloo" 的行程,编号为 id=45 的乘客出发于 time=3 到达于 time=15,编号为 id=27 的乘客于 time=10 出发于 time=20 到达。所以平均时间为 ( (15-3) + (20-10) ) / 2 = 11.0
undergroundSystem.checkIn(10, "Leyton", 24);
undergroundSystem.getAverageTime("Leyton", "Waterloo"); // 返回 11.0
undergroundSystem.checkOut(10, "Waterloo", 38);
undergroundSystem.getAverageTime("Leyton", "Waterloo"); // 返回 12.0
*/
class UndergroundSystem {
public:
unordered_map<string, unordered_map<int, int> > sys;
UndergroundSystem() {
}
void checkIn(int id, string stationName, int t) {
sys[stationName][id] = -1*t;
}
void checkOut(int id, string stationName, int t) {
sys[stationName][id] = t;
}
double getAverageTime(string startStation, string endStation) {
double res = 0;
double n = 0;
for(auto &[id, val] : sys[startStation]){
if(sys[endStation].count(id)>0 and val<0 and sys[endStation][id]>0){
n++;
res += sys[endStation][id] + val;
}
}
return res/n;
}
};
/**
* 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);
*/
5371. 找到所有好字符串
对字符串。。- -
题目:给你两个长度为 n 的字符串 s1 和 s2 ,以及一个字符串 evil 。请你返回 好字符串 的数目。好字符串 的定义为:它的长度为 n ,字典序大于等于 s1 ,字典序小于等于 s2 ,且不包含 evil 为子字符串。由于答案可能很大,请你返回答案对 10^9 + 7 取余的结果。
/*
输入:n = 2, s1 = "aa", s2 = "da", evil = "b"
输出:51
解释:总共有 25 个以 'a' 开头的好字符串:"aa","ac","ad",...,"az"。还有 25 个以 'c' 开头的好字符串:"ca","cc","cd",...,"cz"。最后,还有一个以 'd' 开头的好字符串:"da"。
*/