Problem Statement
When files are stored on a hard disk, they often become fragmented. This means that the file is not stored in sequential sectors on the disk. The first half of a file might be stored in sector 243, while the second half of a file might be far away in sector 105. The goal of defragmenting a hard drive is to arrange the files so that each file is stored in order on sequential sectors of the disk. Thus, if a file required 4 sectors of storage space, it would end up in sectors N, N+1, N+2, and N+3, for some N. Typically, this is done to increase the overall speed of the computer. There are a number of programs that will defrag a hard disk as described above. However, many of them are painfully slow. You are trying to develop a new algorithm to defrag hard drives, but before you start, you would like to determine how fast you can defrag a very small drive without very many files on it. You will be given the locations of a number of files on a small hard disk, and are to determine the minimum number of sectors that must be moved before the entire drive is defragged. You have enough memory to hold two sectors worth of data at once, but that is all. You will be given a String[], disk, each of whose elements represents a single file. Each element of disk will be formatted as a single-space delimited list of integers which represent the locations of the parts of the file, in order. Hence, the String, "4 9 6 59 41" represents a file stored in 5 sectors where the first part of the file is in sector 4 of the disk. One way to defrag this file would be to move the contents of sector 9 to sector 5, the contents of sector 59 to sector 7, and the contents of sector 41 to sector 8. By doing this, the file would be stored sequentially in sectors 4-8. You will also be given an int, size, representing the total number of sectors on the disk (sectors 0 through size-1, inclusive, may contain data). You are to return the smallest number of sectors that must be moved to defragment the whole disk. Keep in mind that you can not move data to a sector until any data being stored there is moved.
Definition
Class:
DiskDefrag
Method:
minMoves
Parameters:
String[], int
Returns:
int
Method signature:
int minMoves(String[] disk, int size)
(be sure your method is public)
Constraints
-
size will be between 10 and 100, inclusive.
-
disk will contain between 1 and 12 elements, inclusive.
-
Each element of disk will contain between 1 and 50 characters, inclusive.
-
Each element of disk will be a single-space delimited list of integers, without extraneous leading zeros.
-
Each integer in disk will be between 0 and size-1, inclusive.
-
No integer will be appear more than once in disk.
Examples
0)
{"3 4 5 6 8 9 10","17 16 15"}
20
Returns: 5
We can defrag the first file by moving the contents of sector 8 to sector 7, then 9 to 8, and finally 10 to 9. The second file can be defragged in a number of ways by moving the contents of two sectors, for a total of 5.
1)
{"1 2 3 5 4 6 7 8"}
10
Returns: 2
Here we can take advantage of the fact that we have enough memory to hold two sectors worth of data. First, load the contents of sectors 4 and 5 into memory. Now, simply write the data back in the reverse order.
2)
{"1 3 5 7","0 2 4 8","6 9"}
100
Returns: 7
分享到:
相关推荐
预测柱状图内容走势:Problem4.zip 预测柱状图内容走势:Problem4.zip 预测柱状图内容走势:Problem4.zip 预测柱状图内容走势:Problem4.zip 预测柱状图内容走势:Problem4.zip 预测柱状图内容走势:Problem4.zip ...
Content is presented in the popular problem-solution format. Look up the programming problem that you want to solve. Read the solution. Apply the solution directly in your own code. Problem solved! ...
Android continues to be one of the leading mobile OS and development platforms driving today’s mobile innovations and the apps ecosystem. Android appears complex, but offers a variety of organized ...
ASP.NET MVC 4 Recipes: A Problem-Solution Approach 632 pages Publisher: Apress; 1 edition (February 20, 2013) Language: English ISBN-10: 1430247738 ISBN-13: 978-1430247739 What you’ll learn ...
This textbook explores the different aspects of data mining from the fundamentals to the complex data types and their applications, capturing the wide diversity of problem domains for data mining ...
W ELCOME TO THE F IFTH EDITION OF C++ Programming: From Problem Analysis to Program Design. Designed for a first Computer Science (CS1) C++ course, this text provides a breath of fresh air to you and ...
C++ Programming: From Problem Analysis to Program Design By 作者: D. S. Malik ISBN-10 书号: 1337102083 ISBN-13 书号: 9781337102087 Edition 版本: 8 出版日期: 2017-02-13 pages 页数: 1491 Contents ...
Spring Recipes: A Problem-Solution Approach, Second Edition continues upon the bestselling success of the previous edition but focuses on the latest Spring 3 features for building enterprise Java ...
MATLAB Machine Learning Recipes: A Problem-Solution Approach》是一本专注于使用MATLAB进行机器学习的书籍。该书提供了一系列关键的机器学习技术的示例,每个示例都采用问题解决的方法。以下是一些关于这本书的...
台湾国立大学林軒田在Coursera上的课程的第一章讲义The Learning Problem
Using a problem-solution approach, Spring Boot 2 Recipes quickly introduces you to Pivotal's Spring Boot 2 micro-framework, then dives into code snippets on how to apply and integrate Spring Boot 2 ...
This textbook explores the different aspects of data mining from the fundamentals to the complex data types and their applications, capturing the wide diversity of problem domains for data mining ...
"This is not just an important but an imperative project: to approach the problem of randomness and success using the state of the art scientific arsenal we have. Barabasi is the person." --Nassim ...
then goes to a discussion of the physical concepts, amplified by mathematics, and very good figures, and then ties it up by finishing with more observational applications, either solving the problem ...
Hacking is the art of creating problem solving, whether used to find an unconventional solution to a difficult problem or to exploit holes in sloppy programming. Many people call themselves hackers, ...
ASP.NET 2.0 Website Programming: Problem - Design - Solution 第二部分 Table of Contents ASP.NET 2.0 Website Programming—Problem - Design - Solution Foreword Introduction ...
总之,《Spring Recipes A Problem-Solution Approach》是一本非常实用的Spring框架学习资料,对于希望深入了解Spring 2.5及其在企业级Java应用开发中的应用的开发者来说,本书无疑是不可或缺的参考书目。