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. ...
随机推荐
- Microsoft Virtual Academy 介绍
Microsoft Virtual Academy 是微软的虚拟学院,会推出微软各个方面的一些教程 介绍一点有用的链接 http://www.microsoftvirtualacademy.com/e ...
- 为aps.net core项目加上全局异常捕捉和记录
在asp.net core中的方案在这里:http://stackoverflow.com/questions/30385246/can-asp-net-5-app-useerrorhandler-a ...
- NOSQL的应用,Redis/Mongo
NOSQL的应用,Redis/Mongo 1.心路历程 上年11月份来公司了,和另外一个同事一起,做了公司一个移动项目的微信公众号,然后为了推广微信公众号,策划那边需要我们做一些活动,包括抽奖,投票. ...
- 团队作业week2-软件分析和用户需求调查
我们的团队选择评定的软件是必应词典(iphone版)和使用较多的有道词典(iphone版) 类别 描述 评分(Bing) 评分(有道) 功能 核心功能1:词典 顾名思义,作为一款词典类 ...
- 如何用js获取当前url的参数值
<script language = javascript> function request(paras){ var url = location.href; var paraStrin ...
- 《C++Primer》复习——with C++11 [1]
1.头文件中不应包含using声明,因为头文件的内容会拷贝到所有引用到他的文件中去,如果头文件里有谋个using声明,那么每个使用了该头文件的文件就会有这个声明,由于不经意间包含了一些名字,反而可能产 ...
- android studio 智能提示忽略大小写
Step1: Step2:
- Android工具包
Eclipse + ADT +SDK,下载ADT Bundle全包含 adt-bundle-windows-x86_64-20140702 http://www.cnblogs.com/tc310/p ...
- ios开发之多线程资源争夺
上一篇介绍了常用的多线程技术,目前开发中比较常用的是GCD,其它的熟悉即可.多线程是为了同步完成多项任务,不是为了提高运行效率,而是为了提高资源使用率来提高系统的整体性能,但是会出现多个线程对同一资源 ...
- 将Windows上的文件上传到Linux上
下载一个SSH Secure Shell Client即可. SSHSecureShellClient-3.2.9下载地址: 免费下载地址在 http://linux.linuxidc.com/ 用户 ...