Leetcode100128. 高访问员工

Every day a Leetcode

题目来源:100128. 高访问员工

解法1:模拟

把名字相同的员工对应的访问时间(转成分钟数)分到同一组中。

对于每一组的访问时间 accessTime,排序后,判断是否有 accessTime[i] - accessTime[i - 2] < 60,如果有,那么把这一组的员工名字加到答案中。

代码:

c 复制代码
/*
 * @lc app=leetcode.cn id=100128 lang=cpp
 *
 * [100128] 高访问员工
 */

// @lc code=start
class Solution
{
private:
    static const int MINUTE = 60;

public:
    vector<string> findHighAccessEmployees(vector<vector<string>> &access_times)
    {
        map<string, vector<int>> employees;
        for (const vector<string> &access_time : access_times)
        {
            string name = access_time[0];
            string time = access_time[1];
            int accessTime = MINUTE * stoi(time.substr(0, 2)) + stoi(time.substr(2));
            employees[name].push_back(accessTime);
        }
        vector<string> highAccessEmployees;
        for (auto &[name, accessTime] : employees)
        {
            sort(accessTime.begin(), accessTime.end());
            for (int i = 2; i < accessTime.size(); i++)
                if (accessTime[i] - accessTime[i - 2] < 60)
                {
                    highAccessEmployees.push_back(name);
                    break;
                }
        }
        return highAccessEmployees;
    }
};
// @lc code=end

结果:

复杂度分析:

时间复杂度:O(Lnlogn),其中 n 为数组 access_times 的长度,L 为员工姓名的最大长度,本题不超过 10。

空间复杂度:O(Ln),其中 n 为数组 access_times 的长度,L 为员工姓名的最大长度,本题不超过 10。

相关推荐
虾..8 分钟前
C++ 哈希
开发语言·c++·哈希算法
循环过三天12 分钟前
3.1、Python-列表
python·算法
liu****18 分钟前
14.日志封装和线程池封装
linux·开发语言·c++
将编程培养成爱好24 分钟前
C++ 设计模式《统计辅助功能》
开发语言·c++·设计模式·访问者模式
dragoooon341 小时前
[优选算法专题六.模拟 ——NO.40~41 外观数列、数青蛙]
数据结构·算法·leetcode
徐新帅1 小时前
CCF-GESP 等级考试 2025年3月认证C++一级真题解析
算法
陌路202 小时前
S16 排序算法--堆排序
算法·排序算法
烛衔溟2 小时前
C语言算法:排序算法入门
c语言·算法·排序算法·插入排序·冒泡排序·选择排序·多关键字排序
一匹电信狗2 小时前
【C++】封装红黑树实现map和set容器(详解)
服务器·c++·算法·leetcode·小程序·stl·visual studio
Laity______2 小时前
指针(2)
c语言·开发语言·数据结构·算法