Đưa ra một loạt các số, sắp xếp chúng theo cách mang lại giá trị lớn nhất. Ví dụ: nếu các số đã cho là {54, 546, 548, 60}, sự sắp xếp 6054854654 cho giá trị lớn nhất. Và nếu các số đã cho là {1, 34, 3, 98, 9, 76, 45, 4}, thì sự sắp xếp 998764543431 cho giá trị lớn nhất.
Một giải pháp đơn giản xuất hiện trong tâm trí của chúng tôi là sắp xếp tất cả các con số theo thứ tự giảm dần, nhưng chỉ đơn giản là sắp xếp không hoạt động. Ví dụ, 548 lớn hơn 60, nhưng trong đầu ra 60 xuất hiện trước 548. Ví dụ thứ hai, 98 lớn hơn 9, nhưng 9 đến trước 98 trong đầu ra.simple solution that comes to our mind is to sort all numbers in descending order, but simply sorting doesn’t work. For example, 548 is greater than 60, but in output 60 comes before 548. As a second example, 98 is greater than 9, but 9 comes before 98 in output.
Vậy làm thế nào để chúng ta đi về nó? Ý tưởng là sử dụng bất kỳ thuật toán sắp xếp dựa trên so sánh nào. & Nbsp; trong thuật toán sắp xếp đã sử dụng, thay vì sử dụng so sánh mặc định, hãy viết một hàm so sánh mycompare [] và sử dụng nó để sắp xếp số. & NBSP;
In the used sorting algorithm, instead of using the default comparison, write a comparison function myCompare[] and use it to sort numbers.
Với hai số x và y, mycompare [] quyết định số nào sẽ đặt đầu tiên - chúng tôi so sánh hai số xy [y được nối ở cuối x] và yx [x được nối ở cuối y]. Nếu xy lớn hơn, thì x nên đến trước y trong đầu ra, nếu không y sẽ đến trước. Ví dụ: Đặt X và Y là 542 và 60. Để so sánh X và Y, chúng tôi so sánh 54260 và 60542. Vì 60542 lớn hơn 54260, & nbsp; chúng tôi đặt y đầu tiên.X and Y, how should myCompare[] decide which number to put first – we compare two numbers XY [Y appended at the end of X] and YX [X appended at the end of Y]. If XY is larger, then X should come before Y in output, else Y should come before. For example, let X and Y be 542 and 60. To compare X and Y, we compare 54260 and 60542. Since 60542 is greater than 54260, we put Y first.
Sau đây là việc thực hiện phương pháp trên. & Nbsp; Để giữ cho mã đơn giản, các số được coi là chuỗi, vectơ được sử dụng thay vì một mảng bình thường. & Nbsp;
To keep the code simple, numbers are considered as strings, the vector is used instead of a normal array.
Dưới đây là việc thực hiện phương pháp trên: & nbsp;
Python3
def
comparator[a, b]:
ab
=
str
[a]
+
str
60548546541
60548546543
=
str
60548546546
+
str
60548546549
def
1 def
2def
3def
4
def
5def
3def
7def
8
def
9comparator[a, b]:
0def
3comparator[a, b]:
2
def
5def
3comparator[a, b]:
5
def
comparator[a, b]:
7
comparator[a, b]:
9
0
1
2
3def
5
6
7
8
9
def
9
6ab
22 ab
4
3def
ab
7
6ab
9
def
9def
1 =
2
6=
4
=
5=
6=
7
3def
str
0
6ab
9
def
9def
1 =
2
6=
4
3def
str
0
6ab
9
=
5str
9=
7
3def
[a]
3
6ab
9
Các
def
9def
1 =
2
6=
4
3def
str
0
6ab
9
=
5str
9=
7
def
9def
1 =
2
6=
4
3def
str
0
6ab
9
=
5str
9=
7
3def
[a]
3
6ab
9
Các
3def
+
6
6ab
9
=
5=
6=
=
7
3def
605485465410
6ab
9=
5
605485465462
=
5str
9=
=
7
605485465474
605485465475
Output:
6054854654
3def
6054854654
24
6ab
9
O[nlogn] ,sorting is considered to have running time complexity of O[nlogn] and the for loop runs in O[n] time.
Auxiliary Space: O[1].
def
9def
1 =
2
66054854654
31=
=
7[using itertools]
def
1
605485465436
Python3
605485465437
605485465438
=
=
605485465441
605485465442
605485465444___
605485465456
=
________ 158 ________ 159 ________ 6 & nbsp;
605485465464
=
605485465466______
3
605485465494
605485465495
comparator[a, b]:
0str
605485465498
Độ phức tạp về thời gian: & nbsp; O [nlogn], phân loại được coi là có độ phức tạp thời gian chạy của O [nLoGn] và các vòng lặp cho các lần chạy trong thời gian O [n].
605485465474
def
04605485465447
605485465448
605485465449
605485465448
605485465451
605485465448
605485465453
def
12Output:
6054854654
Một cách tiếp cận khác: [sử dụng itertools] & nbsp;
O[nlogn]
Auxiliary Space: O[1].
Sử dụng thư viện sẵn có của Python, thư viện ITERTOOLS có thể được sử dụng để thực hiện nhiệm vụ này. & NBSP; & NBSP;