1396. Design Underground System M

Design Underground System (M)

Implement the class UndergroundSystem that supports three methods:

1.checkIn(int id, string stationName, int t)

  • A customer with id card equal to id, gets in the station stationName at time t.
  • A customer can only be checked into one place at a time.

2.checkOut(int id, string stationName, int t)

  • A customer with id card equal to id, gets out from the station stationName at time t.

3.getAverageTime(string startStation, string endStation)

  • Returns the average time to travel between the startStation and the endStation.
  • The average time is computed from all the previous traveling from startStation to endStation that happened directly.
  • Call to getAverageTime is always valid.

You can assume all calls to checkIn and checkOut methods are consistent. That is, if a customer gets in at time t1 at some station, then it gets out at time t2 with t2 > t1. 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.0,11.0,null,11.0,null,12.0]

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);
undergroundSystem.checkOut(27, "Waterloo", 20);
undergroundSystem.checkOut(32, "Cambridge", 22);
undergroundSystem.getAverageTime("Paradise", "Cambridge");       // return 14.0. There was only one travel from "Paradise" (at time 8) to "Cambridge" (at time 22)
undergroundSystem.getAverageTime("Leyton", "Waterloo");          // return 11.0. There were two travels from "Leyton" to "Waterloo", a customer with id=45 from time=3 to time=15 and a customer with id=27 from time=10 to time=20. So the average time is ( (15-3) + (20-10) ) / 2 = 11.0
undergroundSystem.checkIn(10, "Leyton", 24);
undergroundSystem.getAverageTime("Leyton", "Waterloo");          // return 11.0
undergroundSystem.checkOut(10, "Waterloo", 38);
undergroundSystem.getAverageTime("Leyton", "Waterloo");          // return 12.0

Constraints:

  • There will be at most 20000 operations.
  • 1 <= id, t <= 10^6
  • All strings consist of uppercase, lowercase English letters and digits.
  • 1 <= stationName.length <= 10
  • Answers within 10^-5 of the actual value will be accepted as correct.

题意

实现一个地铁系统类的三个方法。

思路

使用两个HashMap处理:

  1. 第一个map记录编号id的乘客的入站信息;
  2. 第二个map记录线路’A-B’对应的行驶总用时和行驶次数。

代码实现

class UndergroundSystem {
    private Map<Integer, Object[]> start;
    private Map<String, int[]> route;

    public UndergroundSystem() {
        start = new HashMap<>();
        route = new HashMap<>();
    }

    public void checkIn(int id, String stationName, int t) {
        start.putIfAbsent(id, new Object[2]);
        Object[] list = start.get(id);
        list[0] = stationName;
        list[1] = t;
    }

    public void checkOut(int id, String stationName, int t) {
        Object[] info = start.get(id);
        String routeName = (String) info[0] + '-' + stationName;
        int prev = (int) info[1];
        
        route.putIfAbsent(routeName, new int[2]);
        int[] list = route.get(routeName);
        list[0] += t - prev;
        list[1]++;
    }

    public double getAverageTime(String startStation, String endStation) {
        String routeName = startStation + '-' + endStation;
        int[] list = route.get(routeName);
        
        return 1.0 * list[0] / list[1];
    }
}
【源码免费下载链接】:https://renmaiwang.cn/s/mz20w ### 数字万用表二极管档测试原理与使用技巧详解#### 一、测试原理概览数字万用表的二极管档是专为检测半导体元件如二极管设计的功能之一,它能准确判断二极管的好坏以及测量其正向压降。与传统的模拟式万用表相比,数字万用表的这一功能更为精确和直观。其工作原理基于向二极管施加一定电压并测量流过的电流,从而判断二极管的导通状态。#### 二、数字万用表二极管档的工作原理在讨论具体工作原理之前,我们先了解数字万用表二极管档与普通电阻档的区别。普通电阻档提供的测试电流较小,不足以充分激活半导体元件的PN结,导致测量结果不准确。然而,二极管档设计时考虑了这一问题,提供了更大的测试电流,确保了对半导体元件特性的有效评估。以DT830型数字万用表为例,其二极管档电路设计巧妙,提供约2.8V的基准电压,通过精密的分压电路确保电流适中,既不会损坏二极管也不会导致测量失准。具体电路中,基准电压源通过一系列电阻(如R17、R18等)分压后,向被测二极管VD提供测试电流。在没有接入二极管的情况下,电路中的A、B两点电压会稳定在特定值;一旦接入二极管,A点电压会被箝位至二极管的正向压降,大约0.7V(硅管)或0.3V(锗管)。此时,流入二极管的电流约为1mA(硅管)或1.3mA(锗管),足以激活二极管而不致于损害它。#### 三、安全保护机制值得注意的是,数字万用表在设计二极管档时还融入了安全保护机制。例如,当误将二极管档用于测量市电电压(AC220V)时,电路中设置的保护元件(如热敏电阻PTC和晶体管Q1、Q2)会启动,限制电流,避免对仪表造成损害。这种设计不仅提升了仪表的可靠性,也增强了操作者的安全性。#### 四、二极管档的使用技巧掌握正确的使用方法是发挥数字万用表二极管档优势的关键。以下是使用步骤:1. **连接仪
内容概要:本文详细比较了9种智能优化算法——CGO(混沌引力优化)、SCA(正弦余弦算法)、GWO(灰狼优化)、CSA(冠状病毒优化)、SSA(麻雀搜索算法)、HHO(哈里斯鹰优化)、WOA(鲸鱼优化算法)、PSO(粒子群优化)和TSO(穿越沙漠优化),并通过Matlab代码实现对这些算法的性能进行了仿真对比。文章重点展示了各算法在不同测试函数下的收敛速度、寻优能力及稳定性,并提供了完整的代码资源,便于读者复现实验结果。此外,文档还附带多个相关科研方向的技术支持介绍,涵盖机器学习、路径规划、信号处理、电力系统优化等多个领域,突出了智能优化算法在实际工程问题中的广泛应用价值。; 【9种优化算法比较】CGO、SCA、GWO、CSA、SSA、HHO、WOA、PSO、TSO智能优化算法比较(Matlab代码实现) 适合人群:具备一定Matlab编程基础,从事智能优化算法研究或应用的研究生、科研人员及工程技术人员,尤其适合正在开展算法改进与仿真实验的相关从业者; 使用场景及目标:①对比分析主流智能优化算法的性能差异,选择最适合特定问题的优化方法;②学习如何在Matlab中实现并测试优化算法,用于解决调度、路径规划、参数优化等实际科研与工程问题; 阅读建议:建议读者结合提供的Matlab代码逐项运行实验,深入理解每种算法的迭代机制与参数设置,同时参考文中提到的其他资源扩展应用场景,强化算法设计与调参能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值