Topcoder 练习小记,Java 与 Python 分别实现。
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 分别实现。的更多相关文章
- Python学习---Java和Python的区别小记
Java和Python的区别小记 注意这里使用的是 and/or/not 非java中的&&,||,!Java中的true是小写 Python中函数就是对象,函数和我们之前的[1,2 ...
- Java集合-Python数据结构比较
Java list与Python list相比较 Java List:有序的,可重复的.(有序指的是集合中对象的顺序与添加顺序相同) Python list(列表)是有序的,可变的. Java Lis ...
- windows、ubuntu下eclipse搭建java、Python环境问题总结
前两篇博文分别讲述了如何在windows.ubuntu下用eclipse搭建java.python环境,下面就针对本人遇到的问题做一个总结. 一.windows下关于java环境变量JAVA_HOME ...
- ubuntu上用eclipse搭建java、python开发环境
上一篇文章讲到如何在windwos上用eclipse搭建java.python开发环境,这一讲将关注如何在ubuntu上实现搭建,本人使用虚拟机安装的ubuntu系统,系统版本为:14.04 lts ...
- windows 下用eclipse搭建java、python开发环境
本人只针对小白!本文只针对小白!本文只针对小白! 最近闲来无事,加上之前虽没有做过eclipse上java.python的开发工作,但一直想尝试一下.于是边查找资料边试验,花了一天时间在自己的机器上用 ...
- paip.元数据驱动的转换-读取文件行到个list理念 uapi java php python总结
paip.元数据驱动的转换-读取文件行到个list理念 uapi java php python总结 #两个思路 1.思路如下:使用file_get_contents()获取txt文件的内容,然后通过 ...
- paip.提升安全性----Des加密 java php python的实现总结
paip.提升安全性----Des加密 java php python的实现总结 /////////// uapi private static String decryptBy ...
- paip.抓取网页内容--java php python
paip.抓取网页内容--java php python.txt 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog ...
- paip.函数方法回调机制跟java php python c++的实现
paip.函数方法回调机制跟java php python c++的实现 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http:// ...
- paip. uapi 过滤器的java php python 实现aop filter
paip. uapi 过滤器的java php python 实现aop filter filter 是面向切面编程AOP.. 作者Attilax 艾龙, EMAIL:1466519819@qq. ...
随机推荐
- Java从入门到精通——数据库篇之OJDBC版本区别
classes12.jar,ojdbc14.jar,ojdbc5.jar和ojdbc6.jar的区别,之间的差异 在使用Oracle JDBC驱动时,有些问题你是不是通过替换不同版本的Oracle ...
- mysql 配置主从
1.选择2个ip,1个为主,1个为从:例:主:192.168.12.76 从:192.168.12.772.在192.168.12.76的my.cnf 配置master,添加如下:(红色为添加的内容) ...
- EditorWindow 和MenuItem
using UnityEngine; using System.Collections; using UnityEditor; public class ClipEventEditor : Edito ...
- Configure xterm Fonts and Colors for Your Eyeball
https://wiki.mpich.org/mpich/index.php/Configure_xterm_Fonts_and_Colors_for_Your_Eyeball Screenshot ...
- oracle中操作数据
使用特定格式插入日期值 insert into emp values (,', to_date('1988-11-11','yyyy-mm-dd'), ); ,); 使用子查询插入数据 create ...
- python学习小结3:函数
Python是对接口编程,而不是对数据类型编程.例如我们定义了一个函数,在函数里用到了in这个接口,那么只要传入的参数实现了这个接口就可以,我们不在乎它是list还是tuple. 简单的函数 使用de ...
- 《C++Primer》复习——with C++11 [4]
考虑到STL的掌握主要靠的是练习,所以对于STL这部分,我把书中的练习都做一遍,加深印象.这些练习是第9.10.11.17章的,分别是顺序容器.泛型算法和关联容器等. ——10月22日 /*----- ...
- 利用Vagrant搭建多平台环境
Vagrant 是一个创建和分发虚拟化开发环境的工具,使用ruby编写,本身并不包含虚拟机管理软件,因此我们需要配合Vagrant安装一个虚拟机软件.Vagrant支持VMware, Virtual ...
- MyEclipse默认标签TODO,XXX,FIXME和自定义标签的使用
MyEclipse默认标签TODO,XXX,FIXME和自定义标签的使用 MyEclipse中的一些特殊的注释技术包括:1. // TODO —— 表示尚未完成的待办事项.2. // XX ...
- watch your tone
老板要求邮件注意语气... 木想到混了这么久这种事情还要老板提醒