返回题库

各位数字全不同的最近一天(MM/DD/YYYY)

Distinct Date II

专题
Algorithmic Programming / 算法编程
难度
L4

题目详情

找“最近(最晚)的一天”的日期,使得按 MM/DD/YYYYMM/DD/YYYY 表示时出现的所有数字都互不相同。

例如 01/23/456701/23/4567 是合法的。用 MMDDYYYYMMDDYYYY 的形式给出答案。

Find the most recent date where all of the digits, when expressed in the form MM/DD/YYYYMM/DD/YYYY are distinct. For example, 01/23/456701/23/4567 would be a valid date. Express your answer in the form MMDDYYYYMMDDYYYY.

解析

要尽量晚,年份应尽量大且仍在过去。

分析可知年份必须以 1 开头;为最大化年份,取 19871987

此时月份首位只能是 0,选最大的可用月份为 06;再选可行的最大日期为 25。

因此日期为 06/25/198706/25/1987,答案为

06251987.06251987.

Original Explanation

Our goal now is to maximize the year to make it as close as possible to our present date. The biggest thing to first note is that the year must start with a 11. Suppose that it started with a 22. We know that the first MM is either a 00 or 11. If it is a 00, then the day either starts with a 11 or is 3131. Note that the other MM can't be a 11 in this case; otherwise, that would eliminate the day possibilities. If the month starts with a 11, then the day either contains 00 or is 3030. However, since the 00 is already used in the MMDDMMDD part, regardless of what we start with, we can't get a year that is in the past.

Therefore, the year must start with a 11. To maximize the year, we can just fix the rest as the three largest integers. Namely, 987987, so our year is 19871987. Since the year starts with 11, the month must start with 00. Then, we choose the largest month integer remaining, which is 66. Then, we must pick the largest possible day among the integers remaining. We can see that 2525 is the largest possible day, as anything starting with 33 is not possible. Therefore, our date is 06/25/198706/25/1987.