Minimum Time Difference不复杂

本文解析LeetCode 539题“Minimum Time Difference”,介绍了一种通过排序和选择性计算时间差的方法来高效求解最小时间差,并提供了C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 题目描述
    leecode medium题 “539. Minimum Time Difference”
    这里写图片描述

  • 分析
    别看该题是中等难度的题,只要缕清思路,就很简单,代码也很容易写。

    如题所述,我们要找最小的时间差。最暴力的一种方法就是让所给出的时间自由组合计算出各个的时间差,取最小的。

    但我们并没有必要计算出所有组合的,因为有些时间的组合我们是可以直接排除的。怎么排除?拿[00:20,12:45,20:13,23:11]举例,对所有的时间进行排序后,那么第一个时间和第二个时间的差肯定会小于(或等于)第一个时间和第三个(或第四个,第n个)的时间差,后者我们就没必要计算,可直接排除了。

    所以,我们只需计算排序好的时间,两两之间的时间差取最小。但还有一个问题我们还没有考虑进去,就是跨天的时间,也即排序好的时间的头和尾的插计算,比如23:11和00:20,它俩的时间差可以是23:11-00:20(已经被我们排除不计算),但也可以是00:20-23:11。所以我们需要把后者计算出来,与之前筛选出来的最小时间差比较,看谁更小,来得到最终过结果。

    可能你会有个疑问,为什么跨天的时间差你就只计算一个,而不计算诸如20:13和00:20的时间差呢?同样道理,因为我们已经排过序了,那么数组末尾和数组开头的这个跨天时间差就一定是所有以跨天模式计算中最小的时间差了,其他的就没必要再计算了。

  • 代码实现
    有时候,我们要善于站在巨人的肩膀上。所以,本次代码使用c++编写,直接使用了一些现成的函数,如排序,string转int等。

class Solution {
public:
    int findMinDifference(vector<string>& timePoints) {
        //排序
        sort(timePoints.begin(),timePoints.end());  

        int min=1500,d;
        //数组内两两之间的时间差
        for (int i = 0; i < timePoints.size()-1;i++){
            d=minutesDiffer(timePoints[i],timePoints[i+1],0);
            if(d<min)
                min=d;
        }

        //头和尾以跨天形式计算的时间差
        d=minutesDiffer(timePoints[timePoints.size()-1],timePoints[0],1);
        if(d<min)
            min=d; 

        return min;

    }

    int minutesDiffer(string &v1,string &v2,bool cross){
        int diff;
        string hour1=v1.substr(0,2);
        string hour2=v2.substr(0,2);
        string min1=v1.substr(3,2);
        string min2=v2.substr(3,2);
        if(cross){
            diff=atoi(min2.c_str())+(60-atoi(min1.c_str()))+60*(atoi(hour2.c_str())+(23-atoi(hour1.c_str())));
        }else{
            if(hour2.compare(hour1)==0){
                diff=atoi(min2.c_str())-atoi(min1.c_str());
            }else{
                diff=atoi(min2.c_str())+(60-atoi(min1.c_str()))+60*(atoi(hour2.c_str())-atoi(hour1.c_str())-1);
            }
        }
        return diff;
    }
};
Load balancing (负载均衡) refers to efficiently distributing incoming network traffic across a group of backend servers. A load balancing algorithm distributes loads in a specific way. If we can estimate the maximum incoming traffic load, here is an algorithm that works according to the following rule: The incoming traffic load of size S will first be partitioned into two parts, and each part may be again partitioned into two parts, and so on. Only one partition is made at a time. At any time, the size of the smallest load must be strictly greater than half of the size of the largest load. All the sizes are positive integers. This partition process goes on until it is impossible to make any further partition. For example, if S=7, then we can break it into 3+4 first, then continue as 4=2+2. The process stops at requiring three servers, holding loads 3, 2, and 2. Your job is to decide the maximum number of backend servers required by this algorithm. Since such kind of partitions may not be unique, find the best solution -- that is, the difference between the largest and the smallest sizes is minimized. Input Specification: Each input file contains one test case, which gives a positive integer S (2≤N≤200), the size of the incoming traffic load. Output Specification: For each case, print two numbers in a line, namely, M, the maximum number of backend servers required, and D, the minimum of the difference between the largest and the smallest sizes in a partition with M servers. The numbers in a line must be separated by one space, and there must be no extra space at the beginning or the end of the line. Sample Input: 22 Sample Output: 4 1 Hint: There are more than one way to partition the load. For example: 22 = 8 + 14 = 8 + 7 + 7 = 4 + 4 + 7 + 7 or 22 = 10 + 12 = 10 + 6 + 6 = 4 + 6 + 6 + 6 or 22 = 10 + 12 = 10 + 6 + 6 = 5 + 5 + 6 + 6 All requires 4 servers. The last partition has the smallest difference 6−5=1, hence 1 is printed out.请给这一到题设置测试用例,包含最大最小情况,和一些特殊的情况,以及对于回溯算法最好和最坏的情况
06-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值