从4个整数列表生成最大HH:MM [英] Generate maximum HH:MM from a list of 4 integers

查看:76
本文介绍了从4个整数列表生成最大HH:MM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:另一个问题是该问题的重复。如果我比这个问题早六个月发布了这个问题,那么我的副本怎么能重复呢?

Note: Another question is the duplicate of this one. If I posted this question 6 months earlier than that one, then how can mine be a duplicate?

我有4个非负整数a,b,c,d。它们都小于或等于9。我必须返回可以以24小时格式显示在时钟上的最大时间。

I have 4 non-negative integers a,b,c,d. All of them are less than or equal to 9. I have to return the maximum time that can be shown on the clock in 24-hour format.

例如: = 9,b = 4,c = 3,d = 1,将产生19:43

For example: a=9, b=4, c=3, d=1, would yield 19:43

我到目前为止只能提出暴力破解方法哪种测试所有24种可能的组合。尽管还算不错,但我想知道是否还有更优雅的方法。欢迎所有比蛮力强的想法。

I have so far only been able to come up with brute-force approach which kind of tests all 24-possible combinations. Although this isn't too bad, I was wondering if there are more elegant approaches. All ideas better than brute force are welcome.

注意:这不是作业问题。我是从面试准备站点获得的,但是那里没有解决方案。

Note: It is not a homework question. I got this from an interview prep site but has no solutions there.

推荐答案

生成所有24个排列都可以,但是您不这样做不必这样做。您对蛮力方法的验证将需要您验证每个数字,并且在验证每个数字时也可以建立排列。从大数字开始。

Generating all 24 permutations would work, but you don't have to do that. Your validation for the brute force method would require you to validate each digit, and you may as well build up the permutation as you validate each digit. Start with big numbers.


  1. 将数字按高->低

  2. 重复每个数字[i]并搜索列表,直到找到符合要求的最高数字。

  1. sort numbers in high -> low
  2. iterate over each digit [i] and search through the list until you find the highest number that fits the requirements.

[0]必须< = 2

[0] must be <=2

[1]必须< = 3,如果[ 0] == 2

[1] must be <=3 if [0]==2

[2]必须< = 5

[2] must be <=5

[3]可以是任意值

从列表中删除该数字,并将其放在[i]

Remove that number from the list, and place it in position [i]

对每个数字重复

这些条件中的每一个都可以在列表中表示为lambda函数,因此很容易将搜索循环与条件分开。

Each of those conditions could be expressed as lambda function in a list, making it easy to separate the search loop from the conditions.

这篇关于从4个整数列表生成最大HH:MM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆