Topcoder Practice notes, Java and Python are implemented separately.

Source: Internet
Author: User

TopCoder on a topic, the title is described as follows:

Problem Statement    byteland is a city with many skyscrapers, so it's a perfect venue for BASE Jumpin G. Danilo is an enthusiastic BASE jumper. He plans to come to Byteland and to the jump off some of its buildings. Danilo wants to make M jumps in Byteland. However, he has some rules. First, he never jumps off the same building twice. Second, all buildings he selects for his jumps must has the same number of floors. (This are for safety reasons:it are hard-to-get the timing right if each jump starts at a different height.) Philipe is the mayor of Byteland. He welcomes Danilo ' s visit as he would like to use it as a publicity stunt. However, Philipe knows that Danilo would only come to Byteland if there is at least M buildings so each has the same nu Mber of floors. To ensure this, the mayor is willing to build additional floors on some of the skyscrapers in Byteland. You are given the int M and a int[] heights. Each element of heights is the number of floors in one ofByteland ' s skyscrapers. Compute and return the smallest number of additional floors the mayor have to build so that there'll be at least M Buildi NGS with the same number of floors. Definition    class:buildingheightseasymethod:minimumparameters:int, Int[]Returns:intMethod Signature:int minimum (int M, int[] heights) (Be sure your method was public) Limits    time limit (s): 2.  000Memory Limit (MB): 256constraints-heights'll contain between 1 and elements, inclusive.-m'll be between 1 and the Number of elements in heights, Inclusive.-each element in heights would be between 1 and inclusive. EXAMPLES0)     2{1, 2, 1, 4, 3}returns:0note that we already has a buildings that has the same num ber of floors. Hence, no additional floors need to being built.1)     3{1, 3, 5, 2, 1}returns:2we want to has at least Three buildings with the same number of floors. The best-of-reach this goal are to-build one floorOn building #0 and one floor on building #4 (0-based indices). After these changes, buildings #0, #3, and #4 would have both floors each.2)     1{43, 15}returns:03 )     3{19, 9, 12}returns:154)     12{25, 18, 38, 1, 42, 41, 14, 16, 19, 46, 42 ,,,,,,,,,,,,, and 4, 4}returns:47this problem statement is the ex--------- Clusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly p Rohibited. (c) 2003, TopCoder, Inc. All rights reserved.
View Code

The title means that given a pile of height of the List for the heights, need to choose the building of M building in these buildings, if not enough of the same height of the building, to raise some floors to meet the demand. The minimum number of floors to be raised.

The most direct solution to the problem:

Sort all floors, calculate the total number of layers needed to raise to a particular floor, and take the fewest numbers out.

Suppose all floors are: {19, 23, 9, 12} 4 buildings. In which to take m= 3 identical floors, the sorted sequence is:

{9, 12, 19, 23}

From the 3rd building to consider increasing the floor, will be 9, 12 increase to 19, total need to increase 19*3-(9 + 12 + 19) = 17 Layer

Starting at 4th Floor (23) added: Total Required increase 23*3-(12 + 19 + 23) = 15 Layer

The answer is 15.

Implementation of Java writes:

Importjava.util.ArrayList;Importjava.util.Collections;Importjava.util.List; Public classBuildingheightseasy { Public Static voidMain (string[] args) {buildingheightseasy bh=NewBuildingheightseasy (); intM = 3; int[] Heights = {19, 23, 9, 12} ; intTotal =Bh.minimum (M, heights);    SYSTEM.OUT.PRINTLN (total); }     Public intMinimumintMint[] Heights) {        if(M = = 1) {            return0; } List<Integer> list =NewArraylist<integer>();  for(Integer cost:heights) {list.add (cost);        } collections.sort (list); Integer Total=Integer.max_value;  for(inti = M-1; I < list.size (); i++) {            intsum =Getsumvalue (M, I, list); if(Sum <Total ) { Total=sum; }        }        returnTotal ; }     Public intGetsumvalue (intMintEnd, list<integer>list) {        intsum = 0;  for(inti = end-m + 1; I < end + 1; i++) {sum+=List.get (i); }        intValue =list.get (end); returnValue * M-sum; }}

It is cumbersome for Java to convert an int array to list and sum list values.

Implementation of Python:

classBuildingheightseasy (object):defminimum (self, M, heights):ifM = = 1:return0 Heights=list (heights) Heights.sort () Total= M * Heights[len (HEIGHTS)-1]         forIinchRange (M-1, Len (heights)): Sumvalue= M * Heights[i]-sum (heights[i-m+1:i+1]) Total= SumvalueifSumvalue < TotalElse TotalreturnTotalm= 3Heights= (19, 23, 9, 12) BH=Buildingheightseasy ()PrintBh.minimum (m,heights)

Python has built-in rich functions, especially the SUM function and the slice function of list.

The conclusion is empty.

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.