Topcoder上的一道题目,题目描述如下:

Problem Statement

Byteland is a city with many skyscrapers, so it's a perfect venue for BASE jumping. Danilo is an enthusiastic BASE jumper. He plans to come to Byteland and to 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 have the same number of floors. (This is for safety reasons: It is 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 will only come to Byteland if there are at least M buildings that each have the same number of floors. To ensure that, 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 of Byteland's skyscrapers. Compute and return the smallest number of additional floors the mayor has to build so that there will be at least M buildings with the same number of floors.

Definition

Class:

BuildingHeightsEasy

Method:

minimum

Parameters:

int, int[]

Returns:

int

Method signature:

int minimum(int M, int[] heights)

(be sure your method is public)

Limits

Time limit (s):

2.000

Memory limit (MB):

256

Constraints

-

heights will contain between 1 and 50 elements, inclusive.

-

M will be between 1 and the number of elements in heights, inclusive.

-

Each element in heights will be between 1 and 50, inclusive.

Examples

0)

2

{1, 2, 1, 4, 3}

Returns: 0

Note that we already have two buildings that have the same number of floors. Hence, no additional floors need to be built.

1)

3

{1, 3, 5, 2, 1}

Returns: 2

We want to have at least three buildings with the same number of floors. The best way to reach this goal is to build one floor on building #0 and one floor on building #4 (0-based indices). After these changes, buildings #0, #3, and #4 will have two floors each.

2)

1

{43, 19, 15}

Returns: 0

3)

3

{19, 23, 9, 12}

Returns: 15

4)

12

{25, 18, 38, 1, 42, 41, 14, 16, 19, 46, 42, 39, 38, 31, 43, 37, 26, 41, 33, 37, 45, 27, 19, 24, 33, 11, 22, 20, 36, 4, 4}

Returns: 47

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

View Code

题目意思是给定一堆楼高度的 List 为 heights ,需要在这些楼里选择 M 栋高度相同的楼,如果没够足够相同高度的楼,就加高一些楼层使得满足需求。求至少需要加高多少层数楼。

最直接的解题思路:

排序所有楼层,分别计算加高到某一楼层需要的总层数,取出其中最少的数字。

假设所有楼层有:{19, 23, 9, 12} 4栋楼。要在其中取 M= 3 栋相同的楼层,排序后的序列为:

{9, 12, 19, 23}

则从第 3 栋开始考虑增加楼层,即将 9, 12 增加至 19 , 共需增加 19*3 - (9 + 12 + 19) = 17 层

在第 4 栋楼层(23)开始增加:共需增加 23*3 - (12 + 19 + 23) = 15 层

答案是 15

Java 写的实现:

importjava.util.ArrayList;importjava.util.Collections;importjava.util.List;public classBuildingHeightsEasy {public static voidmain(String[] args) {

BuildingHeightsEasy bh= newBuildingHeightsEasy();int M = 3;int[] heights = {19, 23, 9, 12} ;int total =bh.minimum(M, heights);

System.out.println(total);

}public int minimum(int M, int[] heights) {if (M == 1) {return 0;

}

List list = new ArrayList();for(Integer cost : heights) {

list.add(cost);

}

Collections.sort(list);

Integer total=Integer.MAX_VALUE;for (int i = M - 1; i < list.size(); i++) {int sum =getSumValue(M, i, list);if (sum

total=sum;

}

}returntotal;

}public int getSumValue(int M, int end, Listlist) {int sum = 0;for (int i = end - M + 1; i < end + 1; i++) {

sum+=list.get(i);

}int value =list.get(end);return value * M -sum;

}

}

Java 在 int 数组转换为 List 和 sum List 值等方便会比较繁琐。

Python 的实现:

classBuildingHeightsEasy(object):defminimum(self, M, heights):if M == 1 : return0

heights=list(heights)

heights.sort()

total= M * heights[len(heights)-1]for i in range(M-1,len(heights)):

sumValue= M * heights[i] - sum(heights[i-M+1:i+1])

total= sumValue if sumValue < total elsetotalreturntotal

M= 3heights= (19, 23, 9, 12)

bh=BuildingHeightsEasy()print bh.minimum(M,heights)

Python 内置了很丰富的函数,特别是 sum 函数和 list 的切片功能。

结语留空。

topcoder java_Topcoder 练习小记,Java 与 Python 分别实现。相关推荐

  1. c# typescript_在任何IDE中从C#,Java或Python代码获取TypeScript接口的简单方法

    c# typescript by Leonardo Carreiro 莱昂纳多·卡雷罗(Leonardo Carreiro) 在任何IDE中从C#,Java或Python代码获取TypeScript接 ...

  2. 学Java还是Python?一张图告诉你!

    Java 和 Python 一直都是两种很火很强大的编程语言,对于刚开始起步学习编程的同学来说,会迷惑且最经常问的问题是,我该学 Java 还是 Python,是不是 Python 容易学,或是应该先 ...

  3. 无监督方法实现C++、Java、Python 代码转换,程序员:出了bug怎么办,两种语言都要看吗?...

    点击上方"视学算法",选择加"星标" 重磅干货,第一时间送达 本文转载自:机器之心  |  参与:魔王 Facebook 提出的无监督代码转换方法 TransC ...

  4. 用AI实现C++、Java、Python代码互译,运行成功率最高达80.9%

    晓查 发自 凹非寺  量子位 报道 | 公众号 QbitAI 还记得美国前一阵要招聘60岁的老程序员吗?都怪编程语言发展太快! 因为新冠疫情的缘故,美国一些地区的失业救济系统不堪重负,而这些系统都是上 ...

  5. 理解java和python类变量以及类的成员变量

    最可怕的不是犯错而是一直都没发现错误,直到现在我才知道自己对类变量的理解有问题. 大概可能也许是因为不常用类变量的原因吧,一直没有发现这个问题**.最近在看C++时才知道了类变量到底是什么**? 以前 ...

  6. LeetCode 316. Remove Duplicate Letters--贪心--Java,C++,Python解法

    题目地址:Number of Longest Increasing Subsequence - LeetCode 做这道题目前建议先做:Longest Increasing Subsequence - ...

  7. LeetCode 673. Number of Longest Increasing Subsequence--O(N log N )--Java,C++,Python解法

    题目地址:Number of Longest Increasing Subsequence - LeetCode 做这道题目前建议先做:Longest Increasing Subsequence - ...

  8. 现在学java还是python好_该学Java还是Python?

    作为"常青树大佬"Java 和"新晋大佬"Python ,经常被人拿来对比,对于刚开始起步学习编程的同学来说,会迷惑且最经常问的问题是,我该学 Java 还是 ...

  9. 2018最具就业前景的7大编程语言,Java、Python和JavaScript?

    2018 年即将到来,Coding Dojo(编码道场)近期发布了 2018 最具就业前景的 7 大编程语言.该公司分析了来自 Indeed 的 25 门编程语言.栈和框架的数据,以找出雇主最需求的七 ...

  10. paip.元数据驱动的转换-读取文件行到个list理念 uapi java php python总结

    paip.元数据驱动的转换-读取文件行到个list理念 uapi java php python总结 #两个思路 1.思路如下:使用file_get_contents()获取txt文件的内容,然后通过 ...

最新文章

  1. 图灵访谈 | 微盟技术专家戴頔:永远行走在路上
  2. [moka同学笔记]PHP操作Redis收集
  3. [linux] SIGPIPE信号处理
  4. lec 3 of game design
  5. 2019年湘潭大学程序设计竞赛(重现赛)补题:F.Black White(尺取法)
  6. [阿里云]I+的一些探索
  7. mysql k,mysql事务有关概念-怀念K.Dures
  8. NETBEANS_RUBYROR shortcut
  9. javascript 常用类
  10. 每日一题(36)—— 什么是预编译 , 何时需要预编译?
  11. c语言变长参数 第一个参数必须吗,一种使用变长参数为C程序构造灵活回调函数的方法...
  12. iOS在地图上WGS84、GCJ-02、BD-09互转解决方案
  13. plsql 误删表,使用flashback query恢复被删除plsql
  14. Python3之max key参数学习记录
  15. 如何站在双11的肩膀上 详解阿里云企业级互联网架构
  16. 如何保障项目组写出高质量的代码
  17. Linux命令:md5sum
  18. 软文推广类的文章怎么写?
  19. 通过BACnet物联网关实现楼宇自动化的物联网解决方案
  20. gifcam使用缩小内存_GifCam怎么用?GifCam使用教程

热门文章

  1. hive之内表和外表
  2. FLV方式实现网页FFmpeg推流无插件播放
  3. python interface_面向对象编程语言中的接口(Interface)
  4. 论文 计算机教育教学能力,高校计算机教师应具备的能力和培养的方向
  5. TensorFlow中的通信机制——Rendezvous(一)本地传输
  6. c语言水解猴子吃桃问题
  7. 猴子吃桃问题的函数递归解决方案
  8. python counter怎么用_Counter的基本用法
  9. 【合成图片】——Graphics2D
  10. 延时delay1s程序 c语言,汇编语言软件延时1s的实现方法