- 浏览: 79456 次
文章分类
最新评论
-
wodentt:
....为什么楼主都英文的博客
用java解决百度之星移动火柴的问题 part 1 -
wensonzhou86:
思路明确,代码清晰,通俗易懂
visit 淘宝面试题:如何充分利用多核CPU,计算很大的List中所有整数的和 -
carydeepbreathing:
代码比较干净
visit 淘宝面试题:如何充分利用多核CPU,计算很大的List中所有整数的和 -
meiowei:
yangguo 写道i don't think it is a ...
用java解决百度之星移动火柴的问题 part 2 -
yangguo:
i don't think it is a good way ...
用java解决百度之星移动火柴的问题 part 2
Here is the code following the logic from the previous post:
The generateSumArrays() does the forward sum-ups, e.g., (1), (2), ..., this is the main functionality. There is also a floors field that holds the results from backward sum-ups, (A), (B), .... However, this field is not really used in computation, because the
starting point is dynamic, not fixed like in this field. So this is merely for information and debug. A similar method to
compute this backward sum is in another class.
Another functionality of this class is to answer the questions posted in the above Chinese paragraph. For a given height of the build, i.e., the number of floors is fixed, how many balls are optimal, e.g., if we are given a building with 10 floors, 50 balls won't reduce the minimal trials, in fact maybe it takes the same number of trials as 10 balls. These are in the first 3 methods. The basic logic is that since the worst case trials are evenly distributed, we just find one worst path of floors to count the trials.
The test cases is
The method test4Balls() prints out the optimal number which are verified by test5Balls() and test6Balls().
java 代码
- package glassball;
- import java.util.List;
- import java.util.ArrayList;
- /**
- * Google interview question
- */
- public class GlassBallPuzzle
- {
- // first index is on glassballs, second is on sum.
- // The sum is used to compare with the total floors of the building and
- // then determine the last element in the previous sum, which is used
- // as the starting point of the floor array.
- private int[][] recursiveSums;
- // the top selection of floors to try. This is not essential, but
- // helpful for testing.
- private int[][] floors;
- private int numOfGlassballs;
- private int numOfFloors;
- public GlassBallPuzzle(int numOfGlassballs, int numOfFloors)
- {
- this.numOfGlassballs = numOfGlassballs;
- this.numOfFloors = numOfFloors;
- generateSumArrays();
- generateFloorArrays();
- }
- public int getMinimalTrials()
- {
- // since all worst paths should have the same number, we pick up any
- // worst path and should have the same number of trials. This path
- // has the broken floor at level 1.
- int len = recursiveSums[numOfGlassballs - 1].length;
- int minimalTrials = recursiveSums[0][len-1] - 1;
- // add trials.
- minimalTrials += getTrials(numOfGlassballs, len);
- return minimalTrials;
- }
- public int[] getOptimalTrialsAndBalls()
- {
- int minimalTrials = numOfFloors;
- int minimalBalls = numOfGlassballs;
- for (int nballs=1; nballs<=numOfGlassballs; nballs++)
- {
- int len = recursiveSums[nballs - 1].length;
- int newTrials = recursiveSums[0][len-1] - 1;
- // add trials.
- newTrials += getTrials(nballs, len);
- if (newTrials < minimalTrials)
- {
- minimalTrials = newTrials;
- minimalBalls = nballs;
- }
- }
- return new int[] { minimalTrials, minimalBalls};
- }
- private int getTrials(int balls, int index)
- {
- // to move across the arrays for balls.
- int ret=0;
- //
- for (int i=0; i<balls; i++)
- {
- // otherwise, there is no need to try.
- if (recursiveSums[i][index-1] < numOfFloors)
- ret++;
- }
- return ret;
- }
- public int[][] getRecursiveSums() { return recursiveSums; }
- private void generateSumArrays()
- {
- recursiveSums = new int[numOfGlassballs][];
- // initialize the first array with 1, 2, 3, 4, ...
- recursiveSums[0] = new int[this.numOfFloors];
- for (int j=0; j<numOfFloors; j++)
- {
- recursiveSums[0][j] = j + 1;
- }
- // now recursively generate the rest of arrays by summation
- for (int i=1; i<numOfGlassballs; i++)
- {
- // since the size is unknown, so we use a list.
- List sums = new ArrayList();
- for (int j=0; j<numOfFloors; j++)
- {
- int total = 0;
- for (int k=0; k<=j; k++) total += recursiveSums[i-1][k];
- sums.add(new Integer(total));
- if (total >= numOfFloors) break;
- }
- recursiveSums[i] = new int[sums.size()];
- for (int j=0; j<sums.size(); j++)
- {
- recursiveSums[i][j] = ((Integer)sums.get(j)).intValue();
- }
- }
- }
- private void generateFloorArrays()
- {
- floors = new int[numOfGlassballs][];
- // If there is one ball, we have to start from floor 1, 2, etc.
- floors[0] = recursiveSums[0];
- // The floors starts from the end of sums series.
- for (int i=1; i<numOfGlassballs; i++)
- {
- floors[i] = new int[recursiveSums[i].length];
- int total=0;
- for (int j=0; j<recursiveSums[i].length; j++)
- {
- total += recursiveSums[i-1][recursiveSums[i].length - 1 - j];
- floors[i][j] = total;
- }
- }
- }
- // could be moved to somewhere else.
- public void printAllResults()
- {
- for (int i=0; i<numOfGlassballs; i++)
- {
- printIntermediateSteps(i);
- }
- }
- private void printIntermediateSteps(int ballIndex)
- {
- System.out.print("sum series for " + (ballIndex + 1) + " balls=");
- int[] segments = recursiveSums[ballIndex];
- for (int i=0; i<segments.length; i++)
- {
- System.out.print(segments[i] + ", ");
- }
- System.out.println();
- System.out.print("floors series for " + (ballIndex + 1) + " balls=");
- segments = floors[ballIndex];
- for (int i=0; i<segments.length; i++)
- {
- System.out.print(segments[i] + ", ");
- }
- System.out.println();
- }
- }
The generateSumArrays() does the forward sum-ups, e.g., (1), (2), ..., this is the main functionality. There is also a floors field that holds the results from backward sum-ups, (A), (B), .... However, this field is not really used in computation, because the
starting point is dynamic, not fixed like in this field. So this is merely for information and debug. A similar method to
compute this backward sum is in another class.
Another functionality of this class is to answer the questions posted in the above Chinese paragraph. For a given height of the build, i.e., the number of floors is fixed, how many balls are optimal, e.g., if we are given a building with 10 floors, 50 balls won't reduce the minimal trials, in fact maybe it takes the same number of trials as 10 balls. These are in the first 3 methods. The basic logic is that since the worst case trials are evenly distributed, we just find one worst path of floors to count the trials.
The test cases is
java 代码
- package glassball;
- import junit.framework.TestCase;
- /**
- * Testcases for minimal trials.
- */
- public class GlassBallPuzzleTest extends TestCase
- {
- public void test2Balls()
- {
- GlassBallPuzzle puzzle = new GlassBallPuzzle(2, 100);
- puzzle.printAllResults();
- System.out.println("minimal trials=" + puzzle.getMinimalTrials());
- assertTrue(puzzle.getMinimalTrials() == 14);
- }
- public void test3Balls()
- {
- GlassBallPuzzle puzzle = new GlassBallPuzzle(3, 100);
- puzzle.printAllResults();
- System.out.println("minimal trials=" + puzzle.getMinimalTrials());
- assertTrue(puzzle.getMinimalTrials() == 9);
- }
- public void test4Balls()
- {
- GlassBallPuzzle puzzle = new GlassBallPuzzle(4, 100);
- puzzle.printAllResults();
- System.out.println("minimal trials=" + puzzle.getMinimalTrials());
- assertTrue(puzzle.getMinimalTrials() == 8);
- int[] optimals = puzzle.getOptimalTrialsAndBalls();
- System.out.println("optimal trials and balls=" + optimals[0] + ", " + optimals[1]);
- }
- public void test5Balls()
- {
- GlassBallPuzzle puzzle = new GlassBallPuzzle(5, 100);
- puzzle.printAllResults();
- System.out.println("minimal trials=" + puzzle.getMinimalTrials());
- assertTrue(puzzle.getMinimalTrials() == 8);
- }
- public void test6Balls()
- {
- GlassBallPuzzle puzzle = new GlassBallPuzzle(6, 100);
- puzzle.printAllResults();
- System.out.println("minimal trials=" + puzzle.getMinimalTrials());
- assertTrue(puzzle.getMinimalTrials() == 8);
- }
- public void test50Balls()
- {
- GlassBallPuzzle puzzle = new GlassBallPuzzle(50, 200);
- puzzle.printAllResults();
- System.out.println("minimal trials=" + puzzle.getMinimalTrials());
- assertTrue(puzzle.getMinimalTrials() == 20);
- }
- }
The method test4Balls() prints out the optimal number which are verified by test5Balls() and test6Balls().
发表评论
-
Revisit 一道应聘智力题的编程求解, Einstein puzzle
2010-03-09 00:36 1426Another brain teaser, titled 一道 ... -
Another Google question - drop glassballs from a building 5
2007-05-13 04:01 1077Here are some results are the t ... -
Another Google question - drop glassballs from a building 4
2007-05-13 03:42 1537The test cases are divided into ... -
Another Google question - drop glassballs from a building 3
2007-05-13 03:33 1092The next step is to find the ac ... -
Another Google question - drop glassballs from a building 1
2007-05-12 04:43 1203Here is the question: 原题: ... -
Given n coins and a pan balance,find the only counterfeit 4
2007-02-27 05:54 1116The Utils class is composed of ... -
Given n coins and a pan balance,find the only counterfeit 3
2007-02-27 05:50 1502In the 1-ball case, if the stat ... -
Given n coins and a pan balance,find the only counterfeit 2
2007-02-27 05:29 1107Now it comes to the Scale class ... -
Given n coins and a pan balance, find the only counterfeit 1
2007-02-27 04:49 1336The problem is: There is one ... -
A Google's Interview Question - GLAT #20 series 4
2007-02-08 02:36 1100The entire class is as follows: ... -
A Google's Interview Question - GLAT #20 series 3
2007-02-08 02:36 1067The roadmap to compute all fixe ... -
A Google's Interview Question - GLAT #20 series 2
2007-02-08 02:24 1072Now, we can deduct the recursio ... -
A Google's Interview Question - GLAT #20 series 1
2007-02-08 02:22 1159Question: Consider a function ...
相关推荐
《Redis桌面管理器Another-Redis-Desktop-Manager详解》 Redis,全称为Remote Dictionary Server,是一种高性能的键值存储系统,常被用作数据库、缓存和消息中间件。其简洁的数据结构和丰富的数据类型使其在分布式...
《Redis桌面视图工具Another-Redis-Desktop-Manager在Windows环境下的应用详解》 Redis,全称Remote Dictionary Server,是一款开源、高性能的键值存储系统,常被用作数据库、缓存和消息中间件。其丰富的数据结构和...
标题中的“Another-Redis-Desktop-Manager.1.5.6”指的是Another Redis Desktop Manager的1.5.6版本。这是一款专为Redis数据库设计的桌面管理工具,它提供了直观的图形用户界面(GUI),方便用户进行数据的查看、...
标题 "Another-Redis-Desktop-Manager.1.5.5" 指向的是一款名为 Another Redis Desktop Manager 的软件,其版本号为 1.5.5。这是一款图形用户界面(GUI)工具,专为管理和操作 Redis 数据库而设计。Redis 是一个开源...
2. **连接管理**:软件支持快速连接到本地或远程的Redis服务器,只需输入主机地址、端口、密码等基本信息,即可建立安全的连接。此外,它还可以保存和管理多个连接配置,方便切换不同的Redis环境。 3. **数据浏览与...
《Redis桌面管理器——Another-Redis-Desktop-Manager 1.3.9详解》 Redis,作为一款高性能的键值数据库,广泛应用于缓存、消息队列等多种场景。为了更好地管理和操作Redis数据库,开发者们设计了一系列的桌面管理...
**Redis桌面管理工具Another-Redis-Desktop-Manager 1.3.7版详解** Redis,全称为Remote Dictionary Server,是一款高性能的键值存储系统,常用于数据库、缓存和消息中间件等场景。其轻量级、速度快的特性使得它在...
Redis桌面(GUI)管理客户端
Redis桌面(GUI)管理客户端
免费神器,redis可视化客户端,支持5.0 stream数据结构
Redis桌面(GUI)管理客户端
Redis桌面(GUI)管理客户端
Another Redis Desktop Manager(简称ARDM)是一个开源的Redis桌面管理工具,它提供了一个图形用户界面(GUI)来方便用户对Redis数据库进行管理和操作。以下是对ARDM 1.6.6版本的简单介绍: 版本:1.6.6 发布日期:...
"Another-Redis-Desktop-Manager.1.4.8"就是这样一个适用于Windows的Redis桌面管理工具。 该工具提供了图形化的界面,使得Redis的日常管理和维护变得更加简单。它支持多种功能,包括连接到本地或远程的Redis服务器...
标题中的"Another-Redis-Desktop-Manager.1.3.8.rar"指的是一个名为"Another Redis Desktop Manager"的软件的版本1.3.8的压缩文件。这是一款针对Redis数据库的桌面管理工具,用于帮助用户在Windows操作系统上更加...
《Another Redis Desktop Manager 1.6.1:免费的Redis可视化管理工具》 Redis,一个高性能的键值数据库,广泛应用于缓存、消息队列、数据存储等多个领域。为了更方便地管理和操作Redis数据库,出现了许多桌面管理...
Redis可视化界面
Redis桌面(GUI)管理客户端
Another-Redis-Desktop-Manager.1.4.2
"Another-Redis-Desktop-Manager.1.5.5.7z" 是一个针对Redis的桌面图形用户界面(GUI)管理工具的压缩包,版本为1.5.5。 Redis Desktop Manager是一款跨平台的Redis管理软件,它提供了一个直观且功能丰富的界面,...