简体   繁体   English

python 中 n 个整数中最大的 Integer

[英]The largest Integer from n integers in python

The program must accept a integer and the list of integers then the output should be the largest possible integer from the given array of integers程序必须接受 integer 和整数列表,然后 output 应该是给定整数数组中可能的最大 integer

from itertools import permutations as p
a=int(input())
print("".join(sorted(list(set(p(list(input().strip().split(" "))))))[-1]))

This is my code but it is not working since it takes too much of time to execute这是我的代码,但它不起作用,因为它需要太多时间来执行示例输出/输入

Execution Time Limit: 500ms执行时间限制:500ms

This was the first, naive approach:这是第一个幼稚的方法:

As others said, sorting is the only key here.正如其他人所说,排序是这里唯一的关键。 As a oneliner taking standard input:作为一个采用标准输入的单线器:

print("".join(sorted(input().split(), reverse=True)))

This works in some test cases, but fails miserably with the case 98 987 .这在某些测试用例中有效,但在98 987案例中惨遭失败。

This is a correct approach, at least I thought so:这是一个正确的方法,至少我是这么认为的:

 def compare(a, b): for i,j in zip_longest(a, b): if i is not None and j is not None: if i<j: return -1 elif i>j: return 1 if i is None: return int(a[0])-int(j) if j is None: return int(b[0])-int(i) return 0

Testing测试

>>> a = ['24', '26', '28', '987', '556', '214', '398', '476', '542', '192', '878', '566', '744', '232', '456', '98', '2', '4', '76', '78'] >>> print("".join(reversed(sorted(a, key=functools.cmp_to_key(compare))))) 98987878787674456655654247645643982826242322214192 >>> 98987878787674456655654247645643982826242322214192 == 98987878787674456655654247645643982826242322214192 True

So it seems to work.所以它似乎工作。

Explanation解释

zip_longest is the same as zip , but it pads for unequal lengths. zip_longestzip相同,但它填充的长度不相等。 Example zip_longest("1", "12") gives [("1", "1"), (None, "2")] .示例zip_longest("1", "12")给出[("1", "1"), (None, "2")]

As long as none of the elements in the tuple are None everything is supposed to work normally:只要元组中的所有元素都不是None一切都应该正常工作:

  • If i is smaller than j , then the number containing i -- here a -- must be smaller than the number containing j -- here b -- so we return -1 .如果i小于j ,那么包含i的数字——这里a a——必须小于包含j的数字——这里是b所以我们返回-1
  • The other way around, it's the same.反过来,也是一样的。

So far, this is the same as the alphanumeric ordering.到目前为止,这与字母数字排序相同。 But what if one number is None ?但是,如果一个数字是None怎么办? Example: Take a = 987 and b = 98 .示例:取a = 987b = 98 At one point, i is 7 and j is None .在某一时刻, i7jNone In alphanumeric ordering, 7 would be larger than None , a>b .在字母数字排序中, 7将大于Nonea>b But here, we have to take into account, that the numbers will be chained, so we have to in fact check, if 7 is greater than the first digit of the other number, which is 9 .但是在这里,我们必须考虑到这些数字会被链接起来,所以我们实际上必须检查7是否大于另一个数字的第一个数字,即9 7 is actually smaller, so b comes first. 7实际上更小,所以b先出现。

If none of the conditions are met, the numbers must be equal and 0 is returned.如果不满足任何条件,则数字必须相等并返回0

This worked in most test cases, but fails miserably with 98 989 .这在大多数测试用例中都有效,但在98 989中惨遭失败。

This is the correct solution:这是正确的解决方案:

def compare(a, b): 
    x = a+b 
    y = b+a 
    if x>y: 
        return 1 
    elif x<y: 
        return -1 
    else: 
        return 0

Testing测试

In [96]: a = ["98", "987"]                                                                                           
In [97]: print("".join(reversed(sorted(a, key=functools.cmp_to_key(compare)))))                                      
98987

In [98]: a = ["989", "98"]                                                                                           
In [99]: print("".join(reversed(sorted(a, key=functools.cmp_to_key(compare)))))                                      
98998

In [100]: a = ['24', '26', '28', '987', '556', '214', '398', '476', '542', '192', '878', '566', '744', '232', '456', 
     ...: '98', '2', '4', '76', '78']                                                                                
In [101]: print("".join(reversed(sorted(a, key=functools.cmp_to_key(compare)))))                                     
98987878787674456655654247645643982826242322214192

They all work他们都工作

Explanation解释

The very first approach was too easy, the second was overcomplex.第一种方法太简单了,第二种方法过于复杂。 This one is the midway and actually very straight forward.这是中途,实际上非常直截了当。 Combine each pair in both possible way.以两种可能的方式组合每一对。 Which combination yields the bigger number?哪个组合产生更大的数字? That will dictate the sorting.这将决定排序。

I hope everything is clear now and that this withstands any test case I maybe even never thought of.我希望现在一切都清楚了,并且这能经受住我可能从未想过的任何测试用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM