【每日一题】 1396. 设计地铁系统

避免每日太过咸鱼,一天搞定一道LeetCode算法题

一、题目描述

请你实现一个类 UndergroundSystem ,它支持以下 3 种方法:

  1. checkIn(int id, string stationName, int t)
  • 编号为 id 的乘客在 t 时刻进入地铁站 stationName 。
  • 一个乘客在同一时间只能在一个地铁站进入或者离开。
  1. checkOut(int id, string stationName, int t)
  • 编号为 id 的乘客在 t 时刻离开地铁站 stationName 。
  1. getAverageTime(string startStation, string endStation)
  • 返回从地铁站 startStation 到地铁站 endStation 的平均花费时间。
  • 平均时间计算的行程包括当前为止所有从 startStation 直接到达 endStation 的行程。
  • 调用 getAverageTime 时,询问的路线至少包含一趟行程。

你可以假设所有对 checkIn 和 checkOut 的调用都是符合逻辑的。也就是说,如果一个顾客在 t1 时刻到达某个地铁站,那么他离开的时间 t2 一定满足 t2 > t1 。所有的事件都按时间顺序给出。

提示:

  • 总共最多有 20000 次操作。

  • 1 <= id, t <= 10^6

  • 所有的字符串包含大写字母,小写字母和数字。

  • 1 <= stationName.length <= 10

  • 与标准答案误差在 10^-5 以内的结果都视为正确结果。

示例 :

输入:
["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"]]输出:
[null,null,null,null,null,null,null,14.0,11.0,null,11.0,null,12.0]解释:
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 UndergroundSystem() {}public void checkIn(int id, String stationName, int t) {}public void checkOut(int id, String stationName, int t) {}public double getAverageTime(String startStation, String endStation) {}
}

二、题解

1. 解法

解题思路:

这道题的重点在于一个乘客他进入地铁站后肯定是需要出站,且出站后,才能再次进站的,所以我就想的是他一进来我就用一个hash表存储这个用户进站的信息(站名和时间),然后当他出站的时候,就可以把他进站和出站作为key来直接算出这个进站出站所用的时间和次数。最后要获取平均时间直接取出来就行了。

static class UndergroundSystem {class Start {String station;int time;public Start(String station, int time) {this.station = station;this.time = time;}}class SumCount {int sum;int count;public SumCount(int sum, int count) {this.sum = sum;this.count = count;}}private Map<Integer, Start> checkInMap;private Map<String, SumCount> totalMap;public UndergroundSystem() {this.checkInMap = new HashMap<>(16);this.totalMap = new HashMap<>(16);}public void checkIn(int id, String stationName, int t) {checkInMap.put(id, new Start(stationName, t));}public void checkOut(int id, String stationName, int t) {Start start = checkInMap.get(id);String key = start.station + stationName;SumCount sumCount = totalMap.getOrDefault(key, new SumCount(0, 0));sumCount.sum += t - start.time;sumCount.count++;totalMap.put(key, sumCount);}public double getAverageTime(String startStation, String endStation) {String key = startStation + endStation;SumCount sumCount = totalMap.getOrDefault(key, new SumCount(0, 1));return 1.0 * sumCount.sum / sumCount.count;}}

题目来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/distance-between-bus-stops

--------------最后感谢大家的阅读,愿大家技术越来越流弊!--------------

--------------也希望大家给我点支持,谢谢各位大佬了!!!--------------

【每日一题】 1396. 设计地铁系统相关推荐

  1. LeetCode: 1396.设计地铁系统 Python实现(仅利用嵌套的数据结构)

    解题思路 本方法主要使用了字典嵌套列表以及元组的数据结构 注意:字典的key值不能为list类型,原因为 TypeError: unhashable type: 'list' StoE字典结构如下: ...

  2. LeetCode 1396. 设计地铁系统(map/unordered_map)

    1. 题目 请你实现一个类 UndergroundSystem ,它支持以下 3 种方法: checkIn(int id, string stationName, int t) 编号为 id 的乘客在 ...

  3. LeetCode 1396. 设计地铁系统 JAVA 回看

    请你实现一个类 UndergroundSystem ,它支持以下 3 种方法: checkIn(int id, string stationName, int t) 编号为 id 的乘客在 t 时刻进 ...

  4. LeetCode题解(1396):设计地铁系统(Python)

    题目:原题链接(中等) 标签:设计 解法 时间复杂度 空间复杂度 执行用时 Ans 1 (Python) checkIn = O(1)O(1)O(1) ; checkOut = O(1)O(1)O(1 ...

  5. LeetCode简单题之设计停车系统

    题目 请你给一个停车场设计一个停车系统.停车场总共有三种不同大小的车位:大,中和小,每种尺寸分别有固定数目的车位. 请你实现 ParkingSystem 类: ParkingSystem(int bi ...

  6. leetcode 5370. 设计地铁系统(C++)

    请你实现一个类 UndergroundSystem ,它支持以下 3 种方法: 1. checkIn(int id, string stationName, int t) 编号为 id 的乘客在 t  ...

  7. 5370. 设计地铁系统

    请你实现一个类 UndergroundSystem ,它支持以下 3 种方法: 1. checkIn(int id, string stationName, int t) 编号为 id 的乘客在 t  ...

  8. Leetcode 5370:设计地铁系统

    题目描述 请你实现一个类 UndergroundSystem ,它支持以下 3 种方法: 1. checkIn(int id, string stationName, int t) 编号为 id 的乘 ...

  9. Java岗大厂面试百日冲刺【Day50】— 秒杀系统2 (日积月累,每日三题)

      大家好,我是陈哈哈,北漂五年.相信大家和我一样,都有一个大厂梦,作为一名资深Java选手,深知面试重要性,接下来我准备用100天时间,基于Java岗面试中的高频面试题,以每日3题的形式,带你过一遍 ...

最新文章

  1. 大蕉蕉的三道 Java 私房菜 No.131
  2. PHP与SQL注入***(实战篇五)
  3. 分布式 RPC架构简单理解
  4. leetcode-36-有效的数独
  5. java运算符优先级举例_列举出java运算符的优先级
  6. js创建,删除,读取文件目录_note
  7. 海员可以饮用蒸馏海水吗?
  8. Python 爬取考研数据:所有 985 高校、六成 211 高校均可调剂!
  9. [转载] lstm时间序列预测_pytorch入门使用PyTorch进行LSTM时间序列预测
  10. Redis脚本插件之————执行Lua脚本示例
  11. 网页特殊符号(HTML字符实体)大全
  12. 承认吧!你不是不行,你是不敢!
  13. 【加密技术】Java加密算法
  14. 深入解析云原生网络抖动引起的性能问题 @龙蜥社区eBPF SIG
  15. 利用c语言编写一个时钟计时器(c语言基础练习)
  16. opencv + contrib windows下源码编译
  17. 数据结构严蔚敏代码合集 严书数据结构代码实现 可直接运行 持续更新by myself
  18. Java:Excel写入“合并单元格“
  19. 5G NR学习理解系列——时频结构及相关概念
  20. 真相了:大众创业葬送了多少人的前程?

热门文章

  1. 计算机系转行写不出论文,关于这么多年我为什么一篇论文都写不出来的原因
  2. hive动态分区,分区数据的几种插入方式,hive常用优化
  3. 踏浪点神:9.12恒指早盘分析及最新资讯
  4. 使用记事本编写运行Java程序
  5. prometheus使用 (二) 监控主机节点
  6. 深度学习与智能故障诊断学习笔记(一)——故障诊断体系介绍
  7. java 接口 属性_浅谈java接口中定义属性
  8. SurfaceView
  9. python 会议室预约系统_基于django的会议室预订系统
  10. 在浏览器中实现压缩图片