返回介绍

solution / 0900-0999 / 0949.Largest Time for Given Digits / README_EN

发布于 2024-06-17 01:03:32 字数 4825 浏览 0 评论 0 收藏 0

949. Largest Time for Given Digits

中文文档

Description

Given an array arr of 4 digits, find the latest 24-hour time that can be made using each digit exactly once.

24-hour times are formatted as "HH:MM", where HH is between 00 and 23, and MM is between 00 and 59. The earliest 24-hour time is 00:00, and the latest is 23:59.

Return _the latest 24-hour time in "HH:MM" format_. If no valid time can be made, return an empty string.

 

Example 1:

Input: arr = [1,2,3,4]
Output: "23:41"
Explanation: The valid 24-hour times are "12:34", "12:43", "13:24", "13:42", "14:23", "14:32", "21:34", "21:43", "23:14", and "23:41". Of these times, "23:41" is the latest.

Example 2:

Input: arr = [5,5,5,5]
Output: ""
Explanation: There are no valid 24-hour times as "55:55" is not valid.

 

Constraints:

  • arr.length == 4
  • 0 <= arr[i] <= 9

Solutions

Solution 1

class Solution:
  def largestTimeFromDigits(self, arr: List[int]) -> str:
    cnt = [0] * 10
    for v in arr:
      cnt[v] += 1
    for h in range(23, -1, -1):
      for m in range(59, -1, -1):
        t = [0] * 10
        t[h // 10] += 1
        t[h % 10] += 1
        t[m // 10] += 1
        t[m % 10] += 1
        if cnt == t:
          return f'{h:02}:{m:02}'
    return ''
class Solution {
  public String largestTimeFromDigits(int[] arr) {
    int ans = -1;
    for (int i = 0; i < 4; ++i) {
      for (int j = 0; j < 4; ++j) {
        for (int k = 0; k < 4; ++k) {
          if (i != j && j != k && i != k) {
            int h = arr[i] * 10 + arr[j];
            int m = arr[k] * 10 + arr[6 - i - j - k];
            if (h < 24 && m < 60) {
              ans = Math.max(ans, h * 60 + m);
            }
          }
        }
      }
    }
    return ans < 0 ? "" : String.format("%02d:%02d", ans / 60, ans % 60);
  }
}
class Solution {
public:
  string largestTimeFromDigits(vector<int>& arr) {
    int ans = -1;
    for (int i = 0; i < 4; ++i) {
      for (int j = 0; j < 4; ++j) {
        for (int k = 0; k < 4; ++k) {
          if (i != j && j != k && i != k) {
            int h = arr[i] * 10 + arr[j];
            int m = arr[k] * 10 + arr[6 - i - j - k];
            if (h < 24 && m < 60) {
              ans = max(ans, h * 60 + m);
            }
          }
        }
      }
    }
    if (ans < 0) return "";
    int h = ans / 60, m = ans % 60;
    return to_string(h / 10) + to_string(h % 10) + ":" + to_string(m / 10) + to_string(m % 10);
  }
};
func largestTimeFromDigits(arr []int) string {
  ans := -1
  for i := 0; i < 4; i++ {
    for j := 0; j < 4; j++ {
      for k := 0; k < 4; k++ {
        if i != j && j != k && i != k {
          h := arr[i]*10 + arr[j]
          m := arr[k]*10 + arr[6-i-j-k]
          if h < 24 && m < 60 {
            ans = max(ans, h*60+m)
          }
        }
      }
    }
  }
  if ans < 0 {
    return ""
  }
  return fmt.Sprintf("%02d:%02d", ans/60, ans%60)
}

Solution 2

class Solution:
  def largestTimeFromDigits(self, arr: List[int]) -> str:
    ans = -1
    for i in range(4):
      for j in range(4):
        for k in range(4):
          if i != j and i != k and j != k:
            h = arr[i] * 10 + arr[j]
            m = arr[k] * 10 + arr[6 - i - j - k]
            if h < 24 and m < 60:
              ans = max(ans, h * 60 + m)
    return '' if ans < 0 else f'{ans // 60:02}:{ans % 60:02}'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文