681. Next Closest Time

5 篇文章 0 订阅
4 篇文章 0 订阅

Given a time represented in the format "HH:MM", form the next closest time by reusing the current digits. There is no limit on how many times a digit can be reused.

You may assume the given input string is always valid. For example, "01:34", "12:09" are all valid. "1:34", "12:9" are all invalid.

Example 1:

Input: "19:34"
Output: "19:39"
Explanation: The next closest time choosing from digits 1, 9, 3, 4, is 19:39, which occurs 5 minutes later.  It is not 19:33, because this occurs 23 hours and 59 minutes later.

Example 2:

Input: "23:59"
Output: "22:22"
Explanation: The next closest time choosing from digits 2, 3, 5, 9, is 22:22. It may be assumed that the returned time is next day's time since it is smaller than the input time numerically.

class Solution {
public:
    string nextClosestTime(string time) {
        vector<char> digit;
        for (int i = 0; i < 5; i++) {
            if (i ==  2) {
                continue;
            }
            bool flag = false;
            for(int j = 0; j < digit.size(); j++) {
                if(time[i] == digit[j]) {
                    flag = true;
                    break;
                }
            }
            if (!flag) {
                digit.push_back(time[i]);
            }
        }
        if (digit.size() == 1) {
            return time;
        }
        vector<string> times;
        generate_time(times, "", digit, 0, time);
        string close = "";
        int min= INT_MAX;
        for (int i = 0; i < times.size(); i++) {
            int diff = cacl_diff(time, times[i]);
            if (diff < min) {
                min = diff;
                close = times[i];
            }
        }
        return close;
    }
private:
    void generate_time(vector<string>& times, string tmp,
                       vector<char> digit, int ind, string time) {
        if (ind == 2) {
            generate_time(times, tmp + ":", digit, ind + 1, time);
        }
        if (ind == 5) {
            if (is_valid(tmp) && tmp != time) {
                times.push_back(tmp);
                return;
            }
        }
        if (ind != 2 && ind != 5) {
            for (int i = 0; i < digit.size(); i++) {
                generate_time(times, tmp + digit[i], digit, ind + 1, time);
            }
        }
    }
    bool is_valid(string str) {
        int num1 = stoi(str.substr(0, 2));
        int num2 = stoi(str.substr(3, 2));
        if (num1 <= 23 && num2 <= 59) {
            return true;
        }
        return false;
    }
    int cacl_diff(string time, string time1) {
        int hour1 = stoi(time.substr(0, 2));
        int hour2 = stoi(time1.substr(0, 2));
        int minute1 = stoi(time.substr(3, 2));
        int minute2 = stoi(time1.substr(3, 2));
        int diff = 0;
        if (hour2 >  hour1) {
            diff += 60 * (hour2 - hour1);
            diff += minute2 - minute1;
        }
        else if (hour2 == hour1) {
            if (minute2 >= minute1) {
                diff += minute2 - minute1;
            }
            else {
                diff += 60 * 24 + minute2 - minute1;
            }
        }
        else {
            hour2 += 24;
            diff += 60 * (hour2 - hour1);
            diff += minute2 - minute1;
        }
        return diff;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
def connect(self): s = self.get_slice() if self.connected: return # increment connect attempt self.stat_collector.incr_connect_attempt(self) if s.is_avaliable(): s.connected_users += 1 self.connected = True print(f'[{int(self.env.now)}] Client_{self.pk} [{self.x}, {self.y}] connected to slice={self.get_slice()} @ {self.base_station}') return True else: self.assign_closest_base_station(exclude=[self.base_station.pk]) if self.base_station is not None and self.get_slice().is_avaliable(): # handover self.stat_collector.incr_handover_count(self) elif self.base_station is not None: # block self.stat_collector.incr_block_count(self) else: pass # uncovered print(f'[{int(self.env.now)}] Client_{self.pk} [{self.x}, {self.y}] connection refused to slice={self.get_slice()} @ {self.base_station}') return False def disconnect(self): if self.connected == False: print(f'[{int(self.env.now)}] Client_{self.pk} [{self.x}, {self.y}] is already disconnected from slice={self.get_slice()} @ {self.base_station}') else: slice = self.get_slice() slice.connected_users -= 1 self.connected = False print(f'[{int(self.env.now)}] Client_{self.pk} [{self.x}, {self.y}] disconnected from slice={self.get_slice()} @ {self.base_station}') return not self.connected def start_consume(self): s = self.get_slice() amount = min(s.get_consumable_share(), self.usage_remaining) # Allocate resource and consume ongoing usage with given bandwidth s.capacity.get(amount) print(f'[{int(self.env.now)}] Client_{self.pk} [{self.x}, {self.y}] gets {amount} usage.') self.last_usage = amount def release_consume(self): s = self.get_slice() # Put the resource back if self.last_usage > 0: # note: s.capacity.put cannot take 0 s.capacity.put(self.last_usage) print(f'[{int(self.env.now)}] Client_{self.pk} [{self.x}, {self.y}] puts back {self.last_usage} usage.') self.total_consume_time += 1 self.total_usage += self.last_usage self.usage_remaining -= self.last_usage self.last_usage = 0中的资源分配
06-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值