LeetCodeWeeklyContest-182

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待补

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值