Nine-chapter algorithm surface question 35 construction maximum number

Source: Internet
Author: User

Nine Chapters count judges Net-original website

http://www.jiuzhang.com/problem/35/


Topics

Given an array containing only positive integers, give a method to stitch together the numbers in the array, making the number of stitching the largest. For example, [1, 32, 212] After stitching, the maximum number obtained is 322121.


Answermethod One:

The key to this problem is how to determine the position of each number in the final result, everyone first thought is definitely the first number of the greater the more forward, such as 8 before 6, 5; If the first one is the same, then look at the second, for example, the following example [4,45,40], 45 should be before 40 The difficulty is that the number of digits varies, how is the relationship determined? Should 4 be placed before, after, or in the middle of 40 and 45? The result is that 4 is placed in the middle of 45 and 40, why? This is because the ten on the 4 is 0 larger than the single digit, so 4 before 40. In this way, although you can find a sequence, but also need to consider other situations, too complex.

Method Two:

Why don't we just compare the result sizes of different combinations? For example: You can compare the size of 445 and 454 to confirm the position of 4 and 45, because 454:445 is large, so 45 should be in the front. So you can sort all the elements in the array by comparing the two-digit method, and then stitch them together. Is this the way we can find the final answer?


Interviewer Angle

This is a greedy algorithm problem, in general, will think of the first number of large numbers in front (the total number of fixed, the higher the natural high number of the better), then how you may be able to describe a few words of the general meaning of the rules into a program can be achieved by the rules, is the problem of the test center. Usually job seekers can answer the general rules, but the specific implementation of the procedure, not all can be perfectly achieved.


Nine-chapter algorithm surface question 35 construction maximum number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.