leetcode 1396. Design Underground System(设计地铁系统)

LeetCode1166.设计文件系统 leecode题链接:LeetCode1166.设计文件系统 题目描述: 你需要设计一个能提供下面两个函数的文件系统: create(path, value): 创建一个新的路径,并尽可能将值 value 与路径 path 关联,然后返回 True。如果路径已经存在或者路径的父路径不存在,则返回 False。 get(path): 返回与路径关联的值。如果路径不存在,则返回 -1。 “路径” 是由一个或多个符合下述格式的字符串连接起来形成的:在 / 后跟着一个或多个小写英文字母。 例如 /leetcode 阅读详情

An underground railway system is keeping track of customer travel times between different stations. They are using this data to calculate the average time it takes to travel from one station to another.

Implement the UndergroundSystem class:

void checkIn(int id, string stationName, int t)
A customer with a card ID equal to id, checks in at the station stationName at time t.
A customer can only be checked into one place at a time.
void checkOut(int id, string stationName, int t)
A customer with a card ID equal to id, checks out from the station stationName at time t.
double getAverageTime(string startStation, string endStation)
Returns the average time it takes to travel from startStation to endStation.
The average time is computed from all the previous traveling times from startStation to endStation that happened directly, meaning a check in at startStation followed by a check out from endStation.
The time it takes to travel from startStation to endStation may be different from the time it takes to travel from endStation to startStation.
There will be at least one customer that has traveled from startStation to endStation before getAverageTime is called.
You may assume all calls to the checkIn and checkOut methods are consistent. If a customer checks in at time t1 then checks out at time t2, then t1 < t2. All events happen in chronological order.

Example 1:

Input
[“UndergroundSystem”,“checkIn”,“checkIn”,“checkIn”,“checkOut”,“checkOut”,“checkOut”,“getAverageTime”,“getAverageTime”,“checkIn”,“getAverageTime”,“checkOut”,“getAverageTime”]
[[],[45,“Leyton”,3],[32,“Paradise”,8],[27,“Leyton”,10],[45,“Waterloo”,15],[27,“Waterloo”,20],[32,“Cambridge”,22],[“Paradise”,“Cambridge”],[“Leyton”,“Waterloo”],[10,“Leyton”,24],[“Leyton”,“Waterloo”],[10,“Waterloo”,38],[“Leyton”,“Waterloo”]]

Output
[null,null,null,null,null,null,null,14.00000,11.00000,null,11.00000,null,12.00000]

Explanation
UndergroundSystem undergroundSystem = new UndergroundSystem();
undergroundSystem.checkIn(45, “Leyton”, 3);
undergroundSystem.checkIn(32, “Paradise”, 8);
undergroundSystem.checkIn(27, “Leyton”, 10);
undergroundSystem.checkOut(45, “Waterloo”, 15); // Customer 45 “Leyton” -> “Waterloo” in 15-3 = 12
undergroundSystem.checkOut(27, “Waterloo”, 20); // Customer 27 “Leyton” -> “Waterloo” in 20-10 = 10
undergroundSystem.checkOut(32, “Cambridge”, 22); // Customer 32 “Paradise” -> “Cambridge” in 22-8 = 14
undergroundSystem.getAverageTime(“Paradise”, “Cambridge”); // return 14.00000. One trip “Paradise” -> “Cambridge”, (14) / 1 = 14
undergroundSystem.getAverageTime(“Leyton”, “Waterloo”); // return 11.00000. Two trips “Leyton” -> “Waterloo”, (10 + 12) / 2 = 11
undergroundSystem.checkIn(10, “Leyton”, 24);
undergroundSystem.getAverageTime(“Leyton”, “Waterloo”); // return 11.00000
undergroundSystem.checkOut(10, “Waterloo”, 38); // Customer 10 “Leyton” -> “Waterloo” in 38-24 = 14
undergroundSystem.getAverageTime(“Leyton”, “Waterloo”); // return 12.00000. Three trips “Leyton” -> “Waterloo”, (10 + 12 + 14) / 3 = 12

这是一个系统设计题,要设计一个地铁检票系统,每个人对应不同的id,进站checkin,会有进站人id,站名,时间。出站是checkout, 有出站id,站名,时间。
出站时间 - 进站时间= 两站之间需要的时间。
getAverageTime就是不同的人到两站之间的时间,计算出平均时间。

思路:

进站时有人物的id和进站信息,存入hashMap,
方便后面出站时 找 这个id对应的进站信息。
把进站信息设计为一个结构CheckInInfo.
那么需要一个map(id, CheckInInfo)结构

出站信息和进站信息一样,人物id, 出站站名,出站时间。

下面通过计算平均时间的要求推出出站信息的数据结构,

要计算两站之间的平均时间,
需要知道进站站名,出站站名,两站之间的总时间,经过的总人数。

两站之间的总时间,总人数需要在每次checkOut时累加。

所以在checkOut时需要找到人物id对应的进站名,进站时间,这个可以通过map(id, CheckInInfo)得到。
把当前出站时间 - 进站时间 累加到这两站的总时间上,人数+1即可。
那么出站信息CheckOutInfo结构就是(出站名,总时间,总人数)

而计算平均时间的函数参数只有进站名和出站名,
需要通过进站名找到对应的(出站名,总时间,总人数),
而为了不每次都去比较出站名是不是当前出站名,
把CheckOutInfo结构(出站名,总时间,总人数)里面的出站名提出来作为hashMap的key.
这样就可以通过站名一次性找到(总时间,总人数)。

因此CheckOutInfo结构变为(总时间,总人数),需要一个map(出站名,CheckOutInfo).

加上进站名,需要这个结构:map(进站名,map(出站名,CheckOutInfo))

通过上面设计的数据结构,
在调用函数时:
checkIn: 进站时保存(id, CheckInInfo)

checkOut: 出站时通过id找到进站站名,进站时间,
更新两站之间的总时间,总人数。
把信息保存进CheckOutInfo.
再通过进站站名保存map(进站名,map(出站名,CheckOutInfo))

getAverageTime:
计算平均时间时,通过进站站名得到map(出站名,CheckOutInfo).
通过出站站名得到CheckOutInfo,也就是得到这两站之间总的时间,总的人数。
相除得到平均时间。

class UndergroundSystem {
    Map<Integer, CheckInInfo> checkInInfo;  //id和checkIn信息
    Map<String, Map<String, CheckOutInfo>> checkInOutInfo; //checkInStation对应 checkOutStation和信息的map

    public UndergroundSystem() {
        checkInInfo = new HashMap<>();
        checkInOutInfo = new HashMap<>();
    }
    
    public void checkIn(int id, String stationName, int t) {
        checkInInfo.put(id, new CheckInInfo(id, stationName, t));
    }
    
    public void checkOut(int id, String stationName, int t) {
        //通过id找到对应的checkInInfo,并移除
        CheckInInfo curCheckIn = checkInInfo.remove(id);
        
        //通过checkOutStationName找到对应的checkInStationName, time, id等
        //刚开始还没有对应的checkOut信息时为null,要新建hashMap
        Map<String, CheckOutInfo> checkOutInfo = checkInOutInfo.getOrDefault(curCheckIn.checkInStation, new HashMap<>());  //找到checkIn对应的checkOut map
        CheckOutInfo detail = checkOutInfo.getOrDefault(stationName, new CheckOutInfo());  //找到checkOutStation对应的信息

        detail.totalTime += t - curCheckIn.checkInTime;
        detail.totalTrips ++;

        checkOutInfo.put(stationName, detail);
        checkInOutInfo.put(curCheckIn.checkInStation, checkOutInfo);
    }
    
    public double getAverageTime(String startStation, String endStation) {
        CheckOutInfo detail = checkInOutInfo.get(startStation).get(endStation);
        long totalTime = detail.totalTime;
        long totalTrips = detail.totalTrips;
        return totalTrips == 0 ? 0 : (double)totalTime / totalTrips;
    }
}

class CheckInInfo{
    int id;
    String checkInStation;
    int checkInTime;
    public CheckInInfo(int id, String stationName, int time) {
        this.id = id;
        this.checkInStation = stationName;
        this.checkInTime = time;
    }
}

class CheckOutInfo{
    long totalTime;
    long totalTrips;
    public CheckOutInfo(){}
}

在这里插入图片描述
在这里插入图片描述

方法二:

上面在计算平均时间时,通过进站名,出站名和2个hashMap能得到总时间和总人数。
还可以把 “进站名-出站名” 这个String作为key, 一步得到总时间和总人数。

那么
checkIn: 保存map(id, 进站信息)

checkOut: 通过id取得进站名,进站时间,
更新两站之间的总时间,总人数,
保存map(“进站名-出站名”, (总时间,总人数))

getAverageTime:
key=“进站名-出站名"
通过key得到(总时间,总人数)
计算平均时间。

class UndergroundSystem {
    Map<Integer, CheckInInfo> checkInInfo;  //id和checkIn信息
    Map<String, CheckOutInfo> checkInOutInfo; //key为"checkInStation-checkOutStation"

    public UndergroundSystem() {
        checkInInfo = new HashMap<>();
        checkInOutInfo = new HashMap<>();
    }
    
    public void checkIn(int id, String stationName, int t) {
        checkInInfo.put(id, new CheckInInfo(id, stationName, t));
    }
    
    public void checkOut(int id, String stationName, int t) {
        //通过id找到对应的checkInInfo,并移除
        CheckInInfo curCheckIn = checkInInfo.remove(id);
        
        String key = curCheckIn.checkInStation + "-" + stationName;
        CheckOutInfo curCheckOut = checkInOutInfo.getOrDefault(key, new CheckOutInfo());

        curCheckOut.totalTime += t - curCheckIn.checkInTime;
        curCheckOut.totalTrips ++;

        checkInOutInfo.put(key, curCheckOut);
    }
    
    public double getAverageTime(String startStation, String endStation) {
        CheckOutInfo detail = checkInOutInfo.get(startStation+"-"+endStation);
        long totalTime = detail.totalTime;
        long totalTrips = detail.totalTrips;
        return totalTrips == 0 ? 0 : (double)totalTime / totalTrips;
    }
}

class CheckInInfo{
    int id;
    String checkInStation;
    int checkInTime;
    public CheckInInfo(int id, String stationName, int time) {
        this.id = id;
        this.checkInStation = stationName;
        this.checkInTime = time;
    }
}

class CheckOutInfo{
    long totalTime;
    long totalTrips;
    public CheckOutInfo(){}
}
LeetCode: 1396.设计地铁系统 Python实现(仅利用嵌套的数据结构) 解题思路 本方法主要使用了字典嵌套列表以及元组的数据结构 注意:字典的key值不能为list类型,原因为 TypeError: unhashable type: ‘list’ StoE字典结构如下: {(startStation,endStation):[time_total, times]} time_total: 所有人从相同的始发站到终点站花费的时间总和 times: 从相同的始发站到终点站出行的人数总和 start字典结构如下: {id:[stationName, t]} 额外操作:一个人可以多次乘 阅读详情

相关推荐

LeetCode系统设计题目解析与实战答案合集

htmltable {th, td {th {pre {简介:LeetCode是IT从业者提升算法与系统设计能力的重要平台,尤其在面试准备中具有关键作用。本压缩包“leetcode答案-LeetCode:系统设计”包含对LeetCode平台上系统设计类题目的详细解答与分析,涵盖负载均衡、分布式缓存、数据库设计、消息队列、网络通信、微服务架构等核心技术主题。通过真实场景的架构设计训练,帮助开发者掌握大型系统构建的核心方法论,提升工程实践与面试应对能力。

weixin_42181686的博客 1291

【LeetCode - 系统设计】

// 考虑用vector存储房间信息 // 自己思路编号放在vector中 - 编号作为vector的下标 vector<tuple<int, int ,int, int, int>> roomSytem; map<int, int> dict; bool AddRoom(int id, int area, int price, int rooms, const vector<int>& address) ....

qq_20386411的博客 1451

leetcode答案-LeetCode:系统设计

leetcode 答案 LeetCode System Design 整理LeetCode算法题,原题及优秀答案

LeetCode刷题之设计题(持续更新)

1. LRU缓存机制 146. LRU缓存机制 运用你所掌握的数据结构,设计和实现一个LRU (最近最少使用) 缓存机制。它应该支持以下操作: 获取数据 get 和 写入数据 put 。 获取数据 get(key) - 如果密钥 (key) 存在于缓存中,则获取密钥的值(总是正数),否则返回 -1。 写入数据 put(key, value) - 如果密钥不存在,则写入其数据值。当缓存容...

Fighting_初心的博客 1061

VCS的增量和分离编译

vcs的增量和分离编译

yunduor909的博客 3302

用Python手把手教你实现一个多层感知机(MLP)——从理论到代码实战

本文详细介绍了如何使用Python和Theano库从零开始实现多层感知机(MLP),涵盖从核心原理到代码实战的全过程。通过构建隐藏层、输出层和完整MLP模型,结合权重初始化、激活函数选择和训练技巧,帮助开发者深入理解神经网络工作机制,并应用于实际机器学习任务。

web39explorer的博客 970

leetcode刷题-系统设计工程题

874. 模拟行走机器人

迷途 582

基于STM32的蓝牙智能药盒设计

本研究设计了一种基于STM32F103C8T6单片机的蓝牙智能药盒系统,针对老年人用药管理需求,集成了定时提醒、药量检测、环境监测等功能。系统通过LCD1602显示屏实时显示时间与用药信息,支持每日3次定时提醒及3种药物用量设置,采用DHT11温湿度传感器、HX711药量检测模块和红外传感器实现环境监控,并通过蓝牙模块实现远程用药提醒。测试结果表明,系统定时误差≤30秒,温湿度监测精度±2℃,药量检测误差<1g,各项功能运行稳定。该设计为解决老年人用药依从性问题提供了智能化解决方案,具有实用价值和应用前景。

3254968421的博客 542

【leetcode】设计题目总结

【代码】【leetcode】设计题目总结。

I'm Zz. 495

ABAP 百科全书的阅读目录(2024版)

909 篇 ABAP 技术文章的目录

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 4242

Python之achatbot包语法、参数和实际应用案例

本书是一本PyTorch深度学习实战指南,分为基础(前6章)和进阶(后5章)两大部分。基础部分涵盖张量运算、神经网络、数据预处理及CNN等核心内容;进阶部分则深入图像、文本、音频建模技术,重点解析Transformer架构及大语言模型开发。全书采用"理论+实践"模式,适合从零基础到进阶的PyTorch学习者。

王国平的博客 631

LeetCode | 1396. Design Underground System设计地铁系统【Python】

LeetCode 1396. Design Underground System设计地铁系统【Medium】【Python】【设计】

Wonz 1233

零基础学习simulink-- 基于自适应模糊PID控制的直流电机速度控制系统

通过上述代码实现,您可以逐步创建和搭建一个基于自适应模糊PID控制的直流电机速度控制系统。该系统能够根据电机的状态和误差实时调整PID参数,确保电机速度稳定在设定范围内。自适应模糊PID控制具有较强的鲁棒性和适应性,能够在存在不确定性和扰动的情况下提供快速且精确的控制性能。Simulink提供了强大的建模和仿真工具,使得控制系统工程师能够快速构建复杂的控制算法,并将其应用于实际工程中。直流电机建模:建立直流电机的动态模型,包括电枢电压、电流、转速和负载扭矩。自适应模糊PID控制器设计。

amy_mhd的博客 413

python地铁查询系统_LeetCode | 1396. Design Underground System设计地铁系统【Python】

LeetCode 1396. Design Underground System设计地铁系统【Medium】【Python】【设计】ProblemImplement the class UndergroundSystem that supports three methods:1.checkIn(int id, string stationName, int t)A customer with i...

weixin_39631689的博客 420

2026年VIVO嵌入式岗位高频面试题(含参考答案)

摘要:VIVO嵌入式岗位考核涵盖智能手机、IoT设备、芯片适配等方向,重点考察硬件底层交互、RTOS/Linux开发、C/C++功底与问题排查能力。题目按优先级排序,分硬件架构、RTOS、C/C++、数据结构、Linux实践、行业认知6大模块,共40道题。硬件部分涉及中断、ARM架构、加速技术、BSP开发等;RTOS部分区分硬/软实时系统;C/C++部分侧重内存管理、位操作;Linux部分考察常用命令;行业部分关注鸿蒙等系统理念。题目设计贴合实际工作场景,全面评估嵌入式开发能力。

Open 52

第4章-07-将WebDriver获取的Cookie传递给Requests

在使用Selenium WebDriver获取了浏览器的Cookies之后,你可以将这些Cookies传递给requests库,以便在后续的HTTP请求中使用这些Cookies。这样做可以让你模拟浏览器会话,使得请求看起来像是从之前使用Selenium WebDriver控制的浏览器发出的。

黑夜开发者的博客 690

leetcode 1396. Design Underground System 设计地铁系统

Problem:结构体和列表存储入站信息,出站时查找对应的id,然后计算这趟的平均值和update总人数,删掉入站列表对应的记录。

邹九的个人博客 62

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

【Leetcode】1396. Design Underground System

题目地址: https://leetcode.com/problems/design-underground-system/ 要求设计一个UndergroundSystem的类,实现下面几个操作: 1、check in方法,参数是乘客id,起始车站名,和当前时间; 2、check out方法,参数是乘客id,出站车站名,和当前时间; 3、getAverageTime方法,参数是两个车站名,要求返回过去所有乘客从这两个车站进出(不考虑换车)花费的总时间,除以搭乘次数,返回double类型。 方法是用两个Has

数学、算法爱好者的博客 248

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

Leetcode1396. Design Underground System

Implement the classUndergroundSystemthat supports three methods: 1.checkIn(int id, string stationName, int t) A customer with id card equal toid, gets in the stationstationNameat timet. A customercan only be checked into one place at a time. 2....

weixin_49991368的博客 195

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

Leetcode 1396. Design Underground System (python)

题目 错误解法 先分享一种错误解法。下面的这种错误解法错在很多个点上。首先,他每个操作不是O(1)的,时间复杂度就错了。第二,如果某个id先进出某两个站,然后再进了某个站,这个时候计算他的travel time是错的,因为按照下面这种解法会是出站时间小于进站时间,也就是这种解法不满足同一个id多次进出的情况。第三,如果某个idA进B出,然后C进D出,这个时候按下面的解法,在query(A,D)这个pair的时候就会被错误的包括进去,所以start和end必须是以pair的形式储存的 class Under

qq_37821701的博客 325

个人练习-Leetcode-1396. Design Underground System

leetcode

Rstln的博客 328

LeetCode 2349. 设计数字容器系统

请你实现一个indexnumberindexnumbernumbernumber-1// 没有数字 10 ,所以返回 -1。// 容器中下标为 2 处填入数字 10。// 容器中下标为 1 处填入数字 10。// 容器中下标为 3 处填入数字 10。// 容器中下标为 5 处填入数字 10。// 数字 10 所在的下标为 1 ,2 ,3 和 5。因为最小下标为 1 ,所以返回 1。// 容器中下标为 1 处填入数字 20。注意,下标 1 处之前为 10 ,现在被替换为 20。

m0_56090828的博客 984

Leetcode各种题型题目+思路+代码(共176道题及答案)

文章目录 第一章:Leetcode 每日很多题 1、Leetcode-1047 删除字符串中的所有相邻重复项 2、剑指 Offer 53 - I. 在排序数组中查找数字 I 3、Leetcode704:二分查找 4、 Leetcode 227:基本计算器II 5、leetcode 224:基本计算器(带括号的计算) 6、Leetcode 15:三数之和:排序+双指针 7、剑指 offer 38.字符串的排列 8、Leetcode409:最长回文串 9、Leetcode 331:验证二叉树的前序序列化 10、

muli522的博客 4万+

【LeetCode-系统设计1】

【代码】【LeetCode-系统设计1】

qq_20386411的博客 670

Leetcode 1396:设计地铁系统(超详细的解法!!!)

请你实现一个类 UndergroundSystem ,它支持以下 3 种方法: 1.checkIn(int id, string stationName, int t) 编号为 id 的乘客在 t 时刻进入地铁站 stationName 。 一个乘客在同一时间只能在一个地铁站进入或者离开。 2.checkOut(int id, string stationName, int t) 编号为 i...

coordinate的博客 1943

LeetCode | 力扣周赛C题 5370. 设计地铁系统

请你实现一个类 UndergroundSystem ,它支持以下 3 种方法: checkIn(int id, string stationName, int t) 编号为 id 的乘客在 t 时刻进入地铁站 stationName 。 一个乘客在同一时间只能在一个地铁站进入或者离开。 checkOut(int id, string stationName, int t) ...

橘嘉禾 381
上一篇: leetcode 535. Encode and Decode TinyURL(对URL编解码)
下一篇: cpp unique_lock
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值