681. Next Closest Time


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.

方法0: brute force

discussion: https://leetcode.com/problems/next-closest-time/discuss/107783/C%2B%2BJava-Clean-Code

思路:

首先这道题有特殊性,因为一天最多有1440分,暴力枚举并转化成时间,然后检查是否符合要求只需要O(1)时间。

class Solution {
    int mins[4] = { 600, 60, 10, 1 };
public:
    string nextClosestTime(string time) {
        size_t colon = time.find(':');
        int cur = stoi(time.substr(0, colon)) * 60 + stoi(time.substr(colon + 1));
        string next = "0000";
        for (int i = 1, d = 0; i <= 1440 && d < 4; i++) {
            int m = (cur + i) % 1440;
            for (d = 0; d < 4; d++) {
                next[d] = '0' + m / mins[d]; m %= mins[d];
                if (time.find(next[d]) == string::npos) break;
            }
        }
        return next.substr(0, 2) + ':' + next.substr(2, 2);
    }
};

方法1:

discussion: https://leetcode.com/problems/next-closest-time/discuss/171365/C%2B%2B-concise-solution-with-explanation-time-O(1)-space-O(1)-using-set-(100)
思路:

每一个数位上最多有4个可能的数字,一共也只有4 ^ 4种可能,可以暴力枚举。但是在选择顺序上是可以优化的:每一次我们都会从最末一位开始向上寻找可替代的数字,直到找到第一个符合要求的解就可以直接返回。那么就分为两种情况:1. 只要当前位向上转一下就可以找到合法解,条件有更大的数字可以选,且如果是小时位的话不超过24,如果是分钟位的话不超过60,2. 要不然就没有更大数字,要不然向上转一下就会超出某个限制范围,这个时候必须将本位回调到最小数字,这样当前面变成更大数字时才能保证后面最小。

Complexity

Time complexity: O(1)
Space complexity: O(1)

class Solution {
public:
    string nextClosestTime(string time) {
        set<int> digits;
        for (char c: time) if (isdigit(c)) digits.insert(c);
        for (int i = time.size() - 1; i >= 0; i--) {
            if (!isdigit(time[i])) continue;
            auto it = upper_bound(digits.begin(), digits.end(), time[i]);
            if (it != digits.end()) {
                time[i] = *it;
                if (i >= 3 && stoi(time.substr(3, 2)) < 60 || i < 2 && stoi(time.substr(0, 2)) < 24)
                    return time;
            }
            time[i] = *digits.begin();
        }
        return time;
    }
};
  • 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、付费专栏及课程。

余额充值