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。

相关推荐
筠筠喵呜喵2 小时前
Linux软件开发性能优化
linux·c++·性能优化
Bruce_kaizy3 小时前
c++ linux环境编程——文件io介绍以及open 、write 、read 三剑客深度详解
linux·服务器·c++·ubuntu·操作系统·文件io
PAK向日葵5 小时前
我用 C++ 写了一个轻量级 Python 虚拟机,刚刚开源
c++·python·开源
玖釉-5 小时前
下一个排列:从字典序到原地算法的完整推导
数据结构·c++·windows·算法
IronMurphy5 小时前
【算法五十】62. 不同路径
算法
影寂ldy5 小时前
C#一维数组
算法
枕星而眠5 小时前
数据结构八大排序详解(一):四大简单排序
c语言·数据结构·c++·后端
过期动态6 小时前
【LeetCode 热题 100】移动零
java·数据结构·算法·leetcode·职场和发展·rabbitmq
努力努力再努力wz6 小时前
【Qt入门系列】:按钮组件全解析:从 QAbstractButton 到快捷键事件、单选与复选机制
c语言·开发语言·数据结构·c++·git·qt·github