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. ...
随机推荐
- hdu 5755 2016 Multi-University Training Contest 3 Gambler Bo 高斯消元模3同余方程
http://acm.hdu.edu.cn/showproblem.php?pid=5755 题意:一个N*M的矩阵,改变一个格子,本身+2,四周+1.同时mod 3;问操作多少次,矩阵变为全0.输出 ...
- Zendframework application 引导过程
Applications 会期望用户提供一个配置好的ServiceManager.提供以下服务: 1.EventManager 2.ModuleManager 3.Request 4.Response ...
- PAT IO-03 整数均值
/* *PAT IO-02 整数四则运算 *2015.7.30 *作者:flx413 */ #include<stdio.h> int main() { ], sum; float ave ...
- 【python】 web开发入门
进入Web开发 现在你完成了Python忍者训练,准备深入Ptyhon的Web开发,但现在的问题是有很多的框架,从中选择最好的框架非常困难,但从初学者的角度出发,Flask基本Web框架将非常适合We ...
- entityframwork
entityframwork映射部分: public class NorthwindContext : DbContext { public DbSet<CATEGORIES> Categ ...
- String对象中常用的方法
String对象中常用的方法 1.charCodeAt方法返回一个整数,代表指定位置字符的Unicode编码.strObj.charCodeAt(index)说明:index将被处理字符的从零开始 ...
- [C#]Linq To Xml 介绍- 转
LINQ to XML 类概述 LINQ to XML 旨在使 XML 名称尽可能简单. XAttribute 类 XAttribute 表示一个 XML 属性. XCData 类 XCDat ...
- 1069: [SCOI2007]最大土地面积 - BZOJ
Description 在某块平面土地上有N个点,你可以选择其中的任意四个点,将这片土地围起来,当然,你希望这四个点围成的多边形面积最大.Input 第1行一个正整数N,接下来N行,每行2个数x,y, ...
- jQuery+css+div一些值得注意的常用语句
一.div页面布局 一个好的页面布局很重要,这会让你写代码的时候层次分明: 以2列左侧固定右侧自适应宽度为例子: 这是HTML代码: <!DOCTYPE html PUBLIC "-/ ...
- [设计模式] 14 命令模式 Command
Command 模式通过将请求封装到一个对象(Command)中,并将请求的接受者存放到具体的 ConcreteCommand 类中(Receiver)中,从而实现调用操作的对象和操作的具体实现者之间 ...