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.

题目意思是给定一堆楼高度的 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 写的实现:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List; public class BuildingHeightsEasy { public static void main(String[] args) {
BuildingHeightsEasy bh = new BuildingHeightsEasy();
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<Integer> list = new ArrayList<Integer>();
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) {
total = sum;
}
}
return total;
} public int getSumValue(int M, int end, List<Integer> list) {
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 的实现:

class BuildingHeightsEasy(object):
def minimum(self, M, heights):
if M == 1 : return 0
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 else total return total M = 3
heights = (19, 23, 9, 12) bh = BuildingHeightsEasy()
print bh.minimum(M,heights)

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

结语留空。

Topcoder 练习小记,Java 与 Python 分别实现。的更多相关文章

  1. Python学习---Java和Python的区别小记

    Java和Python的区别小记 注意这里使用的是 and/or/not  非java中的&&,||,!Java中的true是小写 Python中函数就是对象,函数和我们之前的[1,2 ...

  2. Java集合-Python数据结构比较

    Java list与Python list相比较 Java List:有序的,可重复的.(有序指的是集合中对象的顺序与添加顺序相同) Python list(列表)是有序的,可变的. Java Lis ...

  3. windows、ubuntu下eclipse搭建java、Python环境问题总结

    前两篇博文分别讲述了如何在windows.ubuntu下用eclipse搭建java.python环境,下面就针对本人遇到的问题做一个总结. 一.windows下关于java环境变量JAVA_HOME ...

  4. ubuntu上用eclipse搭建java、python开发环境

    上一篇文章讲到如何在windwos上用eclipse搭建java.python开发环境,这一讲将关注如何在ubuntu上实现搭建,本人使用虚拟机安装的ubuntu系统,系统版本为:14.04 lts ...

  5. windows 下用eclipse搭建java、python开发环境

    本人只针对小白!本文只针对小白!本文只针对小白! 最近闲来无事,加上之前虽没有做过eclipse上java.python的开发工作,但一直想尝试一下.于是边查找资料边试验,花了一天时间在自己的机器上用 ...

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

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

  7. paip.提升安全性----Des加密 java php python的实现总结

    paip.提升安全性----Des加密 java php python的实现总结 ///////////    uapi         private static String decryptBy ...

  8. paip.抓取网页内容--java php python

    paip.抓取网页内容--java php python.txt 作者Attilax  艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog ...

  9. paip.函数方法回调机制跟java php python c++的实现

    paip.函数方法回调机制跟java php python c++的实现 作者Attilax 艾龙,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http:// ...

  10. paip. uapi 过滤器的java php python 实现aop filter

    paip. uapi 过滤器的java php python 实现aop filter filter 是面向切面编程AOP.. 作者Attilax  艾龙,  EMAIL:1466519819@qq. ...

随机推荐

  1. BootLoader作用

    BootLoader 是系统加电后运行的第一段代码.一般它只在系统启动时非常短的时间内运行. 由OS Loader负责将所要引导的操作系统的内核映象从硬盘上读到系统RAM中,然后跳转到内核的入口点上. ...

  2. GDB调试详解

    GDB是一个由GNU开源组织发布的.UNIX/LINUX操作系统下的.基于命令行的.功能强大的程序调试工具. GDB中的命令固然很多,但我们只需掌握其中十个左右的命令,就大致可以完成日常的基本的程序调 ...

  3. java中封装

    .什么是封装? 封装就是将属性私有化,提供公有的方法访问私有属性. 做法就是:修改属性的可见性来限制对属性的访问,并为每个属性创建一对取值(getter)方法和赋值(setter)方法,用于对这些属性 ...

  4. platform

    作者yuanlulu httpblogcsdnnetyuanlulu版权没有但是转载请保留此段声明 第1章platform驱动管理机制 platform_device 数据结构 注册流程 platfo ...

  5. javac。java版本切换

    如果安装有多个Java版本时(有时候有些软件自行安装),怎样方便的进行切换呢.除了常见的设置环境变量外,今天学到了一种新的切换方法: update-alternatives --config java ...

  6. substr,mb_substr,iconv_substr,array_slice

    通过一个例子来看其关系 /** +---------------------------------------------------------- * 字符串截取,支持中文和其他编码 +----- ...

  7. Entity Framework学习笔记(六)----使用Lambda查询Entity Framework(1)

    请注明转载地址:http://www.cnblogs.com/arhat 在前几章中,老魏一直使用Linq来查询Entity Framework.但是老魏感觉,如果使用Linq的话,那么Linq的返回 ...

  8. 二、有限状态机(FSM)

    1.状态机的作用?是什么? 状态机,顾名思义就是用来描述状态的.完善一点就是在同一的时钟下.更准确说是一种verilogHDL编程思想. 例如我们每一个系统都可以分为好几种状态,如:开始,初始化,运行 ...

  9. Notes of the scrum meeting(10/28)

    meeting time:4:00~6:00p.m.,October 28th,2013 meeting place:雕刻时光 attendees: 顾育豪                       ...

  10. javascript_22_for_js控制div每五个换一行

    2. 3. css: <style type="text/css"> div{height: 50px; width: 50px; background: #f1161 ...